SoapUI – How to synchronize using threads during load test

I use soapUI to test webservices. I was facing this scenario when running load test. I had five threads. And when I ran these threads at the same time, each of the threads used to create its own local objects. I was reading a unique serialNo from a file and then processing it. Due to these threads , I was reading the serialNo five separate times.
In this situation I tried using FileLocking,plain synchronization..etc. It didnt work out as specified. So I used LoadTestContext. This can be called using the command context.LoadTestContext in the groovy script. You can use it as below :


// call the loadTestContext and store it in a local variable
def ltContext = context.LoadTestContext
try{
// synchronize the part of code in which you are accessing the variable.
synchronized(ltContext)
{
//serialNo is the code that I want to be synchronized for the threads.
//check for Null
if(ltContext.serialNo == null)
{
//get the serialNo from a file and write it to a file...
def writer1=new File(c:testFilescontentsContextError.txt).newWriter(true)
writer1.writeLine(Null Value+ltContext.serialNo)
writer1.close()
}else
{
//get the serialNo from the loadContext and store it
// in a local variable.
String serialNo = ltContext.serialNo;
log.info(set the value +ltContext.serialNo);
}
}
}catch(Exception e)
{
log.info(Error+e);
}

Remember the loadContext is null unless you run a load test.
In this way you can synchronize the threads in a load test in SOAPUI.

In

Leave a Reply

Your email address will not be published. Required fields are marked *