Concurrent Reports

Concurrent Reports

In most cases we would like that our automation projects will be kept as simple as possible. Nevertheless, there are times on which we need to use concurrency in our project. Maybe we want to test multiple mobile devices or monitor a system while performing various operations or any other scenario that requires parallel operations.

Since JSystem exposes all the power of Java, there is no real problem in opening a few threads and send them to perform some tasks. The problems usually pops up when we want those threads to use the HTML report; Since the threads are using the report in parallel the final result can be quite messy.

One good way of doing it is by saving all the report elements in a buffer as long the threads are working instead of writing them to the reports and after the threads are done, read all the report elements from the buffer, sort them by the originator threads and only then, send them to the reports.

Actually, it is quite simple to accomplish it, especially since version 6.0.01.

String[] threadNames = new String[10];
report.startBufferingReports();
//Start threads and keep the thread names
//Join threads
report.stopBufferingReports();
for (String threadName:threadNames){
 ReporterHelper.reportByOriginator(report, new String[]{threadName});
}

In the first line we create a new array or a list to hold all the thread names. Then we ask JSystem to buffer all the report elements instead of writing them. After all the threads are done we stop the buffering and use the reporter help method to report the elements by the originator. The helper method also deletes all the report elements that were reported

You may notice one problem with this approach. We will get no HTML reports until all the threads are done, which can be quite annoying when we are sitting next to the computer, executing our tests.

Another approach would be to implement our threads in a way that when each thread is done it is calling to a method that in her turn, reports all the report elements of the finished thread. Just don’t forget to start the buffering before starting the threads and stop buffering after joining them all together.

public void threadEnded(String threadName){
 synchronized (report) {
  report.stopBufferingReports();
  ReporterHelper.reportByOriginator(report, new String[] {threadName});
  report.startBufferingReports();
 }
}

Notice that we have to lock the report while reporting the elements to avoid sending elements from another thread.

 

View Itai Agmon's LinkedIn profileView Itai Agmon’s profile

 

Fork me on GitHub