public void shouldSupportConcurrentlyCreatingNewKeys() throws InterruptedException {
// note. this test did never actually trigger the race condition so only limited confidence here.
// this is the best I've come up with so far for actually triggering the conditions this breaks
int factor = 666;
int startIndex = EfficientString.nextIndex();
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(20);
final JsonObject o = new JsonObject();
// this should create some potential for the race condition to trigger since it rapidly creates the same keys
int total = 100000;
for(int i=0;i<total;i++) {
// make sure we are actually creating new Strings with no overlap with the other tests
final String str="shouldSupportConcurrentlyCreatingNewKeys-"+ (i/factor);
executorService.execute(new Runnable() {
@Override
public void run() {
o.put(str, str); // this should never fail with null key because of the (hopefully) now fixed EfficientString
EfficientString e = EfficientString.fromString(str);
assertThat(EfficientString.fromString(str), is(e));
}
});
}
executorService.shutdown();
executorService.awaitTermination(2, TimeUnit.SECONDS);
// check greater than so that it won't clash with concurrent executions of other tests in surefire
assertThat(EfficientString.nextIndex()-startIndex, Matchers.greaterThan(total/factor));
}