Forum Discussion
M_McDonald
14 years agoSuper Contributor
I don't know if you can live with the delay that this would introduce (at least 1 ms per call to the function) or if 2.5 supports this or if it is really correct (how's that for a disclaimer!), but you could try to put the following in your project Load Script:
What I think the getId method should do is wait for 1 ms before returning the current time in ms, and since it is synchronized, each thread should wait it's turn to go through the method thus ensuring a unique time. Then when you want a unique ID in a request, you can put in the following:
Of course if you had a thousand threads that could introduce at least a 1 second delay for a thread to get an id in the worst case.
Good luck.
class IdGen {
static def synchronized getId() {
sleep(1)
return new java.util.Date().time
}
}
project.metaClass.IdGen = new IdGen()What I think the getId method should do is wait for 1 ms before returning the current time in ms, and since it is synchronized, each thread should wait it's turn to go through the method thus ensuring a unique time. Then when you want a unique ID in a request, you can put in the following:
${=context.testCase.testSuite.project.IdGen.getId()}Of course if you had a thousand threads that could introduce at least a 1 second delay for a thread to get an id in the worst case.
Good luck.