Forum Discussion

an_moto's avatar
an_moto
New Contributor
9 years ago

How to get a separate context in different testcase instances running in parallel?

I have a test case with groovy script test step which uses some variables received in context, e.g. counter value, processes it and returns some other value, e.g. returnId

 

def counter = context.getProperty("counter").toInteger() 
def threadId = context.getProperty("threadId")
...
testRunner.testCase.setPropertyValue("returnId", returnId)

 

 

And this test case is called from another test case groovy script, which creates several threads with numerous test case executions

...
def counter = new AtomicInteger()
...

// multiple threads
1.upto(2) { 
  threads << new Thread({ 
    def tc = testRunner.testCase.testSuite.getTestCaseByName("create entity")
    def txInstanceContext = new com.eviware.soapui.support.types.StringToObjectMap() 
    // thread specific parameter
    def threadId = ...
    txInstanceContext.put("threadId", threadId)

    // multiple loops
    1.upto(2) {
      def number = counter.getAndIncrement().toString()    	
      // execution specific variable
      txInstanceContext.put("counter", number)
      log.info "Started uploading " + number + " at " + new Date().getTime() 
      def runner = tc.run( txInstanceContext, false )      
      while(runner.status == Status.RUNNING) {
      	this.sleep(50)
      }      
      log.info "Status: " + runner.status + ", time taken for upload was: " + runner.timeTaken + " ms"     
      ...
      assert runner.status != Status.FAILED : runner.reason  
      def returnId = tc.getPropertyValue("returnId")
      log.info "Finished uploading " + number + " at " + new Date().getTime() 
      log.info "Returned id: " + returnId 
      ...
    }
  })
}

threads.each { 
	it.start()
}
threads.each { 
	it.join()
}

How to get an isolated scope for each execution, to avoid overriding/setting test case variables from other thread executions?