The background scripts themselves are very similar to normal VU scripts. They are associated with a VU type and, therefore, inherit from the VirtualUserScript class.
Method calls to start and stop background scripts can be automatically inserted into a web script by using the Background Scripts Rule during script generation. This rule will also remove requests from the script that are expected to appear in the background script. However, a background script cannot itself be generated from a recording because it is likely to contain complex looping behavior.
Below you will find more information about background scripts.
Background scripts are designed to be started from within normal VU scripts. They cannot be added to a workflow or assigned to a VU group in a test. Conversely, only background scripts can be run in parallel with the main VU script using the methods described below.
VirtualUser and VirtualUserScript classes provide a variety of background script methods you can use in your own custom VU code and script code.
You can create background scripts using the createBackgroundScript() method. You must specify an ID string, which is used to refer to the background script instance when calling any of the other methods. You also must include the name of the background script class, preceded by the package/namespace (such as com.testplant.testing.MyBackgroundScript). The background script you want to start must be of the same VU type as the main script, or a parent of that VU type.
When you create background script, it automatically starts unless you specify ExecutionFlags.JustCreate. In that case, you can start the background script by calling the startBackgroundScript(ID) method.
A background script can stop automatically at the end of its script() method, or it can continue executing if you set ExecutionFlags.Loop. There is another set of flags, AutomaticStopCriteria, which determine whether the background script will stop if it is still executing at the end of the current script or iteration, or at the point where the VU has finished executing its workflow.
To stop the background script manually, you can use the methods requestToStopBackgroundScript(ID), stopBackgroundScriptImmediately(ID), or waitForBackgroundScriptToFinish(ID). See the API documentation for more details.
There are three methods which are particularly useful for synchronization:
Using these methods guarantees that a background script will not continue executing until the main script has reached a certain point (or vice-versa). This can be useful if the script behaviors are linked somehow.
There are also waitForX(key) methods to synchronize on values in the VU's data dictionary. For example, a background script might do some processing and store a value in the data dictionary. If the main script needs that value before it can continue, it may call waitForString(key) to pause until the background script has stored the value against the particular key in the data dictionary.