Example: Adding a repeating timer behavior to a stub

In this example, you add a repeating timer event to a stub that starts after zero seconds and then repeats every 10 seconds.

About this task

To use repeating timers, you must add a Lifecycle and Timer to the stub behavior before you add lifecycle and timer events to the Events. The Lifecycle is used to initialize the timer when the stub starts and the Timer is used to trigger an action and retrigger it. For more information about stub behavior, see The Behavior tab.

Procedure

  1. Create an empty stub from an operation that has no transport that is configured. Right-click the operation in HCL DevOps Test Integrations and APIs (Test Integrations and APIs) Test Factory perspective and click New > Stubs > Stub.
  2. Next, add the stub behaviors. From the Stub Editor, click the Behavior tab and then click Add to open the Add Behavioral Entity dialog.
  3. Now, set a Lifecycle behavior instance so you can schedule a repeating timer event to trigger after 10 seconds and a Timer behavior instance so you can define a message to be sent when the timer event is triggered.
    1. With Lifecycle selected, accept the default name that is provided in the Instance field and then click Add.

      The Behavior tab opens with the name and description of the lifecycle behavior and a configuration setting.

    2. Click Add to return to the Add Behavior Entity dialog and select Timer. Accept the default name that is provided in the Instance field and then click Add.

      The Behavior tab opens with the name and description of the timer behavior. The timer behavior does not have any configuration settings.

      Added behavior types

  4. Next, set the events for the behavior types by clicking the Events tab.
    1. To add an event to the lifecycle behavior, click the Add Event (Add Event icon) icon. Double-click the Event box, select lifecycle then onStart from the list so that the stub runs an action when the stub starts.
    2. To add another event to the timer behavior, click the Add Event (Add Event icon) icon again. Double-click the Event box, select timer then onTimerEvent from the list so that the stub sends a message when the event occurs.

      Add event to timer behavior type

  5. To start a timer, you must add code to the lifecycle.onStart event.
    1. Go to the Activity tab of this event and add a Function action by right-clicking in the Activity pane and selecting New > General > Function.

      Add a function to the timer event

    2. Double-click the Function action to edit it. Make sure that the Function tab is selected and that the Script Language field is set to ECMAScript.

      Edit function for timer event

    3. Type in the following code to initialize the timer:
      timer.scheduleRepeatingTimerEvent("id","param",0, 10, java.util.concurrent.TimeUnit.SECONDS)

      In this example, the first parameter is the identifier that you are giving the timer. The second parameter is an object that is returned to the stub when the timer runs out. The third and fourth parameters specify how long the timer runs; in this case, the timer starts after zero seconds and uses a repeat interval of 10 seconds.

      Tip: For a description about each of the Timer function behaviors that you can use to schedule a single timer event, schedule a repeating timer event, or cancel a timer event, see The Behavior tab.
    4. Click OK to close the Function action.
  6. To add an action to run when the timer is triggered, you can add business logic or a send message to the timer.onTimerEvent event.
    1. Go to the Activity tab of this event and add a log action by right-clicking in the Activity pane and selecting New > Flow > Log.
    2. Type an output message and click OK to close the Log action.

    Log action added for timer event.

    Tip: The timer.onTimerEvent event has an Input tab that you can use to filter different timer events. In this example, since you have only one timer event, you do not need to configure a filter.
  7. Go to the Test Lab perspective and run the stub.

    In the Test Lab, you can see that the timer event repeats every 10 seconds, at which time the output message Callback triggered!!! was sent.

    Results of running the stub with a repeating timer