Recently I came across one scenario in which we have to test one service in grails application
We want this to be done via multithreading so that multiple thread can execute the service method concurrently using grails integration test
So the very effective solution we found was GPARS
In GPars the GParsPool and GParsExecutorsPool classes give you access to low-level data parallelism techniques
This utility gives you facility of processing collections concurrently. So the size of collection will be the
No. of user for us and the collection data can be use as param pass to the service method
Below is the sample code chunk.
You need to import the below class in you class
import com.mongodb.gridfs.GridFS
also need to configure NO_OF_THREADS require for processing
suppose we have the list of size 1000 containing the user ID as string data
def userList = new ArrayList(1000);
userList.add(“1”)
Now in test Method we write the Closure which will process our collection as shown below.
GParsPool.withPool(NO_OF_THREADS){ final Closure processUser = {userId, index -> def response = service.processUser(userId) assertNotNull response
} assertNotNull(userList) userList.eachWithIndex(processUser.async()) } |
This will work as concurrent collection processor for our userList with given NO_OF_THREADS . we can pass the index and its value as parameter to the service method.
No comments:
Post a Comment