in disco-java-agent/disco-java-agent-core/src/integ/java/software/amazon/disco/agent/integtest/concurrent/ExecutorServiceTests.java [275:300]
public void testSubmitRunnableNestedWhenExecutorReusesThread() throws Exception {
ExecutorService e = Executors.newFixedThreadPool(1);
AbstractQueue<Future<?>> futures = new ConcurrentLinkedQueue<>();
long[] threadIdStack = new long[3];
threadIdStack[0] = Thread.currentThread().getId();
TransactionContext.putMetadata("foo", "bar");
futures.add(e.submit(() -> {
TransactionContext.putMetadata("foo2", "bar2");
threadIdStack[1] = Thread.currentThread().getId();
futures.add(e.submit(() -> {
threadIdStack[2] = Thread.currentThread().getId();
Assert.assertEquals("bar", TransactionContext.getMetadata("foo"));
Assert.assertEquals("bar2", TransactionContext.getMetadata("foo2"));
}));
}));
Future<?> f;
while ((f = futures.poll()) != null) {
f.get(1, TimeUnit.DAYS);
}
//need to retry test in the race condition case where a thread was not in fact reused. i.e. out of the 3 thread ids, 2 (or all 3) must be the same.
if(!(threadIdStack[0]==threadIdStack[1] || threadIdStack[1]==threadIdStack[2] || threadIdStack[0]==threadIdStack[2])) {
throw new ConcurrencyCanBeRetriedException();
}
}