{"id":1010,"date":"2012-01-17T17:09:15","date_gmt":"2012-01-17T22:09:15","guid":{"rendered":"http:\/\/mainlinetest.com\/?p=1010"},"modified":"2012-01-17T17:15:52","modified_gmt":"2012-01-17T22:15:52","slug":"beware-of-fileglobals","status":"publish","type":"post","link":"https:\/\/mainlinetest.com\/?p=1010","title":{"rendered":"Beware of FileGlobals"},"content":{"rendered":"<p>With any program you develop it is always considered best practice to limit the scope of your variables.\u00a0 In TestStand, I often come across sequences that violate this principle.\u00a0 Specifically, it is very common for developers to use FileGlobals where they should be using Local variables.\u00a0 In this blog post, I am going to demonstrate a pitfall of FileGlobals that one can encounter when executing a test sequence.<\/p>\n<p>To start out I am going to create two variables; a Local variable creatively called \u2018LocalVariable and a FileGlobal variable called \u2018FileGlobalVariable\u2019 as shown in the figure below.\u00a0 Note that both variables are strings defaulted to an empty string value (\u201c\u201d).<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb.png\" border=\"0\" alt=\"image\" width=\"244\" height=\"196\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 1:\u00a0 Variable Creation<\/strong><\/p>\n<p>Next I am going create a small sequence consisting of two Message Popup boxes and a statement setting the strings to a value other than the default empty string.\u00a0 The configured Message Popup box and simple sequence are shown in the figures below.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb1.png\" border=\"0\" alt=\"image\" width=\"431\" height=\"150\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 2: Message Box Configuration<\/strong><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb2.png\" border=\"0\" alt=\"image\" width=\"457\" height=\"174\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 3: Simple Sequence<\/strong><\/p>\n<p>As you can see, this script simply pops up a Message Box showing the values of my two variables, sets them to something else, and pops up a second Message Box to confirm that they have indeed changed.\u00a0 The desired expectation here is that I should expect my two variables to be set to empty strings every time the script reaches the first Message Box and set to something else whenever the second Message Box pops up.\u00a0 As in any script, prior to a\u00a0 sequence actually performing any task, it should be a reasonable assumption that my variables are all set to their initial default values.\u00a0 However, I am going to show you how there actually exists a case in which FileGlobals prevents this from happening.<\/p>\n<p>For this script I am using the SequentialModel.seq process model.\u00a0 As you may or may not be aware of, this process model comes with two entry points; \u2018Single Pass\u2019 and \u2018Test UUTs\u2019.\u00a0 The \u2018Single Pass\u2019 entry point allows the user to run a sequence all the way through one time, generating a test report at the end.\u00a0 This entry point is commonly used in initial script creation.\u00a0 I am first going to execute my script using this entry point and will execute the script two separate times.\u00a0 The figures below are my results.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image3.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb3.png\" border=\"0\" alt=\"image\" width=\"406\" height=\"183\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 4: Single Pass \u2013 First Run \u2013 Initial<\/strong><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb4.png\" border=\"0\" alt=\"image\" width=\"410\" height=\"178\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 5: Single Pass \u2013 First Run \u2013 Final<\/strong><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image5.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb5.png\" border=\"0\" alt=\"image\" width=\"410\" height=\"182\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 6: Single Pass \u2013 Second Run \u2013 Initial<\/strong><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image6.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb6.png\" border=\"0\" alt=\"image\" width=\"412\" height=\"182\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 7: Single Pass \u2013 Second Run \u2013 Final<\/strong><\/p>\n<p>Pretty anti-climatic considering this ran exactly as I had suspected.\u00a0 Whenever I start the second pass both variables were reinitialized and the script ran both times successfully giving me the expected results.\u00a0 Compare this to somebody developing a script in their office who now believes there script is ready to be released to the manufacturing floor.\u00a0 After all, you\u2019ve ran it multiple times and it appears to be very repeatable in behavior.<\/p>\n<p>One interesting twist however is that the manufacturing floor doesn\u2019t use the \u2018Single Pass\u2019 entry point.\u00a0 Instead it prefers to use the \u2018Test UUTs\u2019 entry point which provides test operators a prompt to enter a serial number of the UUT and doesn\u2019t output a test report until an entire batch has been tested.\u00a0 So let me rerun my script two more times using this entry point and see what I come up with.<\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb7.png\" border=\"0\" alt=\"image\" width=\"406\" height=\"181\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 8: Test UUTs \u2013 First Run \u2013 Initial<\/strong><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image8.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb8.png\" border=\"0\" alt=\"image\" width=\"407\" height=\"175\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 9: Test UUTs \u2013 First Run \u2013 Final<\/strong><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb9.png\" border=\"0\" alt=\"image\" width=\"407\" height=\"184\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 10: Test UUTs \u2013 Second Run \u2013 Initial<\/strong><\/p>\n<p><a href=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image10.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 0px; border: 0px;\" title=\"image\" src=\"http:\/\/mainlinetest.com\/wp-content\/uploads\/2012\/01\/image_thumb10.png\" border=\"0\" alt=\"image\" width=\"410\" height=\"178\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>Figure 11: Test UUTs \u2013 Second Run \u2013 Final<\/strong><\/p>\n<p>The big glaring issue here is shown in Figure 10.\u00a0 Whenever I go to execute my script for a second time the variables are not set to the empty strings as anticipated.\u00a0 In fact, the FileGlobals variable stored the last value it received from the previous run.\u00a0 Imagine the issues something like this could introduce into a script.\u00a0 On top of that, this issue is very hard to debug whenever one encounters it.\u00a0 The natural tendency is to troubleshoot it by taking it offline and executing it via the \u2018Single Pass\u2019 entry point; something that would make it very hard to reproduce.\u00a0 Now going back to the manufacturing example, imagine that you officially released your script and and the test operators are experiencing random failures.\u00a0 Mass quantities of donuts would now need to be purchased over the next few weeks in order to repair your status as a reputable engineer.<\/p>\n<p>In conclusion, limit the scope of your variables.\u00a0 FileGlobals should be used sparingly (i.e. for constants).\u00a0 The above example is something that could cause a lot of heartache and could have been easily avoided.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With any program you develop it is always considered best practice to limit the scope of your variables.\u00a0 In TestStand, I often come across sequences that violate this principle.\u00a0 Specifically, it is very common for developers to use FileGlobals where they should be using Local variables.\u00a0 In this blog post, I am going to demonstrate&nbsp;<a href=\"https:\/\/mainlinetest.com\/?p=1010\" class=\"read-more\">Continue Reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":741,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[36,24,39,37,38,23],"class_list":["post-1010","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-fileglobals","tag-national-instruments","tag-process-model","tag-single-pass","tag-test-uuts","tag-teststand","cat-1-id"],"_links":{"self":[{"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1010"}],"version-history":[{"count":6,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1016,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/posts\/1010\/revisions\/1016"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=\/wp\/v2\/media\/741"}],"wp:attachment":[{"href":"https:\/\/mainlinetest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mainlinetest.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}