Forum Discussion

Alcatel-Lucent__10's avatar
Alcatel-Lucent__10
Occasional Contributor
13 years ago

mockservice onRequest script : parallel execution ?

Hi,

I have defined a simple mockservice. The onRequest script of the mockservice, contains some code to analyse the incoming request, update some counters and write some logging to a file depending on the value of a log variable.

This is working fine when request are sent sequential. As soon as incoming requests are in parallel, the counters are no longer correct. I know from the "application under test" logfiles, that the request was received in the mockservice and that the response was sent back. Only the counters do not contain that correct numbers.

The code that I'm using is the following :

//Retrieving counters
totalIncMsg = mockRunner.mockService.getPropertyValue("totalIncMsg").toInteger()
incOmsMsg = mockRunner.mockService.getPropertyValue("incOmsMsg").toInteger()
nbrMeters = mockRunner.mockService.getPropertyValue("nbrMeters").toInteger()
nbrOutage = mockRunner.mockService.getPropertyValue("nbrOutage").toInteger()
nbrRestoration = mockRunner.mockService.getPropertyValue("nbrRestoration").toInteger()
nbrRestorationAMI = mockRunner.mockService.getPropertyValue("nbrRestorationAMI").toInteger()
nbrRestorationSGMS = mockRunner.mockService.getPropertyValue("nbrRestorationSGMS").toInteger()
repType = mockRunner.mockService.project.getPropertyValue("OMSReporting")

//Getting the request.
def req = new XmlSlurper().parseText(mockRequest.requestContent);

//Incrementing global counters
totalIncMsg++

if ( req.Body.inMeterAlertReadOutResponse.children().size() != 0 ) {
incOmsMsg++

nbrMetersInMsg = req.Body.inMeterAlertReadOutResponse.children().size() - 3
nbrMeters = nbrMeters + nbrMetersInMsg
for(def i=0; i<nbrMetersInMsg; i++){
alertName = req.Body.inMeterAlertReadOutResponse.Alert_Read_Out_Output_Specification_List[i].Meter_Alert_Read_Out_List.Alert_Name.text()
if ( alertName.toString() == "Outage Restoration AMI" ) {
nbrRestoration++
nbrRestorationAMI++
} else if ( alertName.toString() == "Outage AMI" ) {
nbrOutage++
} else if ( alertName.toString() == "Outage Restoration SGMS" ) {
nbrRestoration++
nbrRestorationSGMS++
}
if ( repType.toString() == "partial" ) {
checkFileRolling()
meterName = req.Body.inMeterAlertReadOutResponse.Alert_Read_Out_Output_Specification_List[i].Entity_Specification.Entity_Name.text()
dateTime = req.Body.inMeterAlertReadOutResponse.Alert_Read_Out_Output_Specification_List[i].Meter_Alert_Read_Out_List.Date_Time.text()
def out = new PrintWriter( new FileWriter( new File(mockRunner.mockService.project.getPropertyValue("Ds_GenLog") + "IncOmsMsg.log"), true ))
reqDate = mockRunner.mockService.getPropertyValue("Date")
if ( mockRunner.mockService.getPropertyValue("nbrEntriesInFile").toInteger() == 0 ) {
out.println("Date msg recvd;entity_name;alert_name;alert_date_time")
}
out.println(reqDate + ";" + meterName + ";" + alertName + ";" + dateTime)
out.close()
nbrEntriesInFile = mockRunner.mockService.getPropertyValue("nbrEntriesInFile").toInteger()
nbrEntriesInFile++
mockRunner.mockService.setPropertyValue("nbrEntriesInFile",nbrEntriesInFile.toString())
}
}
}

//Logging the msg according to repType
if ( repType.toString() == "full" ) {
checkFileRolling()
def out = new PrintWriter( new FileWriter( new File(mockRunner.mockService.project.getPropertyValue("Ds_GenLog") + "IncOmsMsg.log"), true ))
reqDate = mockRunner.mockService.getPropertyValue("Date")
out.println(reqDate + " : request received in mockOMSSimulator.")
out.println(mockRequest.requestContent)
out.close()
nbrEntriesInFile = mockRunner.mockService.getPropertyValue("nbrEntriesInFile").toInteger()
nbrEntriesInFile++
mockRunner.mockService.setPropertyValue("nbrEntriesInFile",nbrEntriesInFile.toString())
}

//Storing the counters again.
mockRunner.mockService.setPropertyValue("totalIncMsg",totalIncMsg.toString())
mockRunner.mockService.setPropertyValue("incOmsMsg",incOmsMsg.toString())
mockRunner.mockService.setPropertyValue("nbrMeters",nbrMeters.toString())
mockRunner.mockService.setPropertyValue("nbrOutage",nbrOutage.toString())
mockRunner.mockService.setPropertyValue("nbrRestoration",nbrRestoration.toString())
mockRunner.mockService.setPropertyValue("nbrRestorationAMI",nbrRestorationAMI.toString())
mockRunner.mockService.setPropertyValue("nbrRestorationSGMS",nbrRestorationSGMS.toString())


How does soapui onRequest script work in case of parallel requests ? Are there parallel executions for this script ? If that is the case, it is likely that the counters, updated by an execution of the onRequest script, are overwritten by another parallel instance of the onRequest script. This would explain the fact that my counters are not correct in case of parallel requests.

How can I solve this issue ? Is the behaviour the same for the afterRequest script ? Can we use synchonized in either onRequest or afterRequest script, without affecting the parallel handling of requests ? Can we use the mockRequest.requestContent also in the afterRequest script ?

Thanks in advance for your help.
  • Hi!

    If you try to synchronize the entire block on perhaps the mockService object. This should prevent thread interference in your script.

    --
    Regards

    Erik
    SmartBear Sweden