richw
16 years agoNew Contributor
setting and retrieving variables within each thread
Hi, I'm trying to create a groovy script that will do the following inside a testcase thread.
Retrieve a variable set by this code:
Examine the variable and if null set to the current system time
if not null retrieve the current time and calculate the passage of time between the two times
If the passage of time is less than a predefined period then wait for that difference.
The main issue I think I'm having is to get the variables to persist within a test case loop.
Here's my code ... any help would be appreciated as this is my first attempt at Groovy script and I don't have a Java background.
// check if time is set
long startTime = context.getProperty( "startTime" )
if( startTime == null )
{
long startTime = System.nanoTime()
context.setProperty( "startTime", startTime )
}
long thisClock = System.nanoTime()
long timePassed = thisClock - startTime
// SET THIS TO DESIRED DELAY IN NANOSECONDS i.e. 999,999,999 +1 = 1 second
desiredDelay=333333333
//
// if delay hasn't happened then wait until delay up
//
if( timePassed < desiredDelay )
{
long sleepTime=(desiredDelay-timePassed)/1000000
Thread.sleep( sleepTime )
}
context.setProperty( "startTime", thisClock )
Retrieve a variable set by this code:
Examine the variable and if null set to the current system time
if not null retrieve the current time and calculate the passage of time between the two times
If the passage of time is less than a predefined period then wait for that difference.
The main issue I think I'm having is to get the variables to persist within a test case loop.
Here's my code ... any help would be appreciated as this is my first attempt at Groovy script and I don't have a Java background.
// check if time is set
long startTime = context.getProperty( "startTime" )
if( startTime == null )
{
long startTime = System.nanoTime()
context.setProperty( "startTime", startTime )
}
long thisClock = System.nanoTime()
long timePassed = thisClock - startTime
// SET THIS TO DESIRED DELAY IN NANOSECONDS i.e. 999,999,999 +1 = 1 second
desiredDelay=333333333
//
// if delay hasn't happened then wait until delay up
//
if( timePassed < desiredDelay )
{
long sleepTime=(desiredDelay-timePassed)/1000000
Thread.sleep( sleepTime )
}
context.setProperty( "startTime", thisClock )