OpenTTCN/Training/TTCN-3 Basics/Exercises/3.2
From OpenTTCN
Exercise 3.2: Avoiding deadlock using timers
Goal: In this exercise we are learning how to avoid deadlocks by using a response timer and timeout exception of an execute statement.
Step 1: Avoiding deadlock using timers
In previous exercise we added alt and receive any statements to avoid deadlock but that is not a complete solution. In addition, a timer limiting the maximum time a certain operation can last is required. Together the receive any and timer supervision provide secure way to avoid deadlocks.
Step 2: Add timer supervision when expecting a response
A common practise is start a timer when sending a message to whom the test case expects a response.
Instructions 1: Add a definition of "TIMER_RESP" timer having two seconds default duration into the DNS test component.
Instructions 2: Enhance the "TC_receive" test case by adding start of "TIMER_RESP" timer when sending a message and and by adding timeout event of that timer to the list of alternatives in the alt statement.
Step 3: TTCN-3 timers in practise
Instructions: Analyze the following three examples of code with your instructor.
Example 1:
...
port_A.send(T_DNS_query);
TIMER_RESP.start;
alt
{
... // other alternatives
[] TIMER_RESP.timeout
{ ... }
}
...
Example 2:
...
TIMER_RESP.start;
port_A.send(T_DNS_query);
alt
{
... // other alternatives
[] TIMER_RESP.timeout
{ ... }
}
...
Example 3:
...
port_A.send(T_DNS_query);
TIMER_RESP.start;
alt
{
[] TIMER_RESP.timeout
{ ... }
... // other alternatives
}
...
Step 4: Specify maximum duration of test case execution
It is better to be safe than sorry when defining test cases. When it comes to deadlocks, it is common safety practise to specify also the maximum duration of test case execution using a TTCN-3 control part and execute statement.
Instructions 1: Using the following example as a guide, enhance your DNS TTCN-3 module by adding a control part. The control part shall be the very last top-level definition of the module. Add an execute statement to your control part that executes the "TC_receive" test case and specifies a maximum duration of 5.0 seconds for the test case execution.
module MyModule
{
... // other definitions
const float C_MAX_TESTCASE_TIME := 10.0; // 10 seconds
// control as the LAST definition of the module
control
{
execute(TC_MyTest(), C_MAX_TESTCASE_TIME);
}
}
Instructions 2: Run the control part using the following command:
tester run dnsc @control
Summary
Congratulations! You have completed your second TTCN-3 exercise of today. During this exercise you have learned how to avoid leadlock situations using timer supervision and timeout exception of execute statement. You are ready to proceed to the next exercise to learn how to use error handlers.
