Adding a timer behavior to a stub

You can use a timer behavior to set a timer event in a stub. After a set time period, the timer event is triggered, the stub receives the event, which it can then respond to. For example, you might have a stub to manage user sessions and you want to add a timer behavior that automatically logs out a user after 30 seconds of inactivity.

Before you begin

You must have a stub before you can add a timer behavior to it. For information about stubs and what you can do with them, see Stubs: Overview and tasks.

About this task

You want to schedule a timer event to trigger after 30 seconds of elapsed time.

To add a timer behavior to a stub:

Procedure

  1. In Rational® Integration Tester Test Factory perspective, double-click the stub that you want to add a timer behavior to.

    The Stub Editor opens.

    Next, you want to add stub behaviors. You do that from the Behavior page.

  2. Click the Behavior tab and then click Add to open the Add Behavioral Entity dialog box.

    You can see the behavior-types that are provided with Rational® Integration Tester. You can also see a description of the Lifecycle behavior.

    Tip: To see the Timer behavior description, select it from the list. For more information about the behavior types, see The Behavior tab.

    Now, you want a Lifecycle behavior instance so you can schedule a timer event to trigger after 30 seconds. You also want a Timer behavior instance so you can define a message to be sent when the timer event runs.

  3. Set behavior types for the stub:
    1. With Lifecycle selected, give the lifecycle instance a name or 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 box and select Timer. Give the timer instance a name or 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

      Next, you set the events for the behavior types that you just added. You do this task from the Events page.

  4. Click the Events tab.
    1. To add an event, click the Add Event (Add Event icon) icon. Double-click the Event box, select lifecycle then onStart from the list.

      You select onStart because you want the stub to trigger an action when the stub starts. You define that action later. Next, you want to add another event and use that event for the Timer behavior such that the stub sends a message when the event occurs.

    2. Click the Add Event (Add Event icon) icon. Double-click the Event box, select timer then onTimerEvent from the list.

      onTimerEvent is a callback that provides an event to the stub when a timer runs out.

      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.scheduleTimerEvent("id","param", 30, java.util.concurrent.TimeUnit.SECONDS)

      In this example, the first parameter is the identifier that you are giving the timer, so that you can cancel it or reset it later, if required. 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 runs for 30 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.

  7. Go to the Test Lab perspective and run the stub.

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

    Results of running the stub with a timer

    Tip: You can use filtering to act on a stub only when a specific timer is triggered. In this example, the timer event is only triggered for the timer named myTimer. You can also tag the parameter object and reuse it in further actions.

    Stub filtering