Skip to content

Commit

Permalink
Add TCK testing of ThreadContext for reusing Builder.build()
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Mittlestat <nmittles@us.ibm.com>
  • Loading branch information
nmittles committed Jan 18, 2019
1 parent e87a6a1 commit da5da08
Showing 1 changed file with 61 additions and 0 deletions.
Expand Up @@ -581,6 +581,67 @@ public void contextualSupplierRunsWithContext() throws InterruptedException {
Label.set(null);
}
}

/**
* Verify that the ThreadContext.Builder can be used to create multiple ThreadContexts with
* different configured contexts.
* @throws Exception
*/
@Test
public void reuseThreadContextBuilder() throws Exception {
ThreadContext.Builder builder = ThreadContext.builder()
.propagated()
.cleared(Buffer.CONTEXT_NAME, THREAD_PRIORITY);

ThreadContext clearingContext = builder.build();

ThreadContext propagatingContext = builder.propagated(Buffer.CONTEXT_NAME, THREAD_PRIORITY)
.cleared()
.build();

int originalPriority = Thread.currentThread().getPriority();
try {
// Set non-default values
int newPriority = originalPriority == 3 ? 2 : 3;
Thread.currentThread().setPriority(newPriority);
Buffer.set(new StringBuffer("reuseBuilder-test-buffer-A"));

Callable<Integer> clearedCallable = clearingContext.contextualCallable(() -> {
Assert.assertEquals(Buffer.get().toString(), "",
"Context type that is configured to be cleared was not cleared.");

Buffer.set(new StringBuffer("reuseBuilder-test-buffer-B"));

return Thread.currentThread().getPriority();
});

Callable<Integer> propagatedCallable = propagatingContext.contextualCallable(() -> {
Assert.assertEquals(Buffer.get().toString(), "reuseBuilder-test-buffer-A",
"Context type was not propagated to contextual action.");

Buffer.set(new StringBuffer("reuseBuilder-test-buffer-C"));

return Thread.currentThread().getPriority();

});

Buffer.set(new StringBuffer("reuseBuilder-test-buffer-D"));

Assert.assertEquals(propagatedCallable.call(), Integer.valueOf(newPriority),
"Context type was not propagated to contextual action.");

Assert.assertEquals(clearedCallable.call(), Integer.valueOf(Thread.NORM_PRIORITY),
"Context type that is configured to be cleared was not cleared.");

Assert.assertEquals(Buffer.get().toString(), "reuseBuilder-test-buffer-D",
"Previous context (Buffer) was not restored after context was propagated for contextual action.");
}
finally {
// Restore original values
Buffer.set(null);
Thread.currentThread().setPriority(originalPriority);
}
}

/**
* Verify that the MicroProfile Concurrency implementation finds third-party thread context providers
Expand Down

0 comments on commit da5da08

Please sign in to comment.