Skip to content

Commit

Permalink
fix managed thread factory tests - context is saved before execution,…
Browse files Browse the repository at this point in the history
… not jndi lookup
  • Loading branch information
aubi authored and Petr Aubrecht committed May 12, 2022
1 parent 1acd002 commit c891da3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,14 @@ public void testManagedThreadFactoryDefinitionAllAttributesEJB() throws Throwabl

ManagedThreadFactory threadFactory = InitialContext.doLookup("java:app/concurrent/EJBThreadFactoryA");

IntContext.set(162);
StringContext.set("testManagedThreadFactoryDefinitionAllAttributesEJB-2");

Thread thread1 = threadFactory.newThread(() -> {});
assertEquals(thread1.getPriority(), 4,
"New threads must be created with the priority that is specified on " +
"ManagedThreadFactoryDefinition");

BlockingQueue<Object> results = new LinkedBlockingQueue<Object>();

threadFactory.newThread(() -> {
Thread thread2 = threadFactory.newThread(() -> {
results.add(Thread.currentThread().getPriority());
results.add(StringContext.get());
results.add(IntContext.get());
Expand All @@ -99,7 +96,12 @@ public void testManagedThreadFactoryDefinitionAllAttributesEJB() throws Throwabl
} catch (Throwable x) {
results.add(x);
}
}).start();
});

IntContext.set(162);
StringContext.set("testManagedThreadFactoryDefinitionAllAttributesEJB-2");

thread2.start();

assertEquals(results.poll(MAX_WAIT_SECONDS, TimeUnit.SECONDS), Integer.valueOf(4),
"ManagedThreadFactory must start threads with the configured priority.");
Expand Down Expand Up @@ -203,14 +205,8 @@ public void testParallelStreamBackedByManagedThreadFactoryEJB() throws Throwable

ManagedThreadFactory threadFactory = InitialContext.doLookup("java:app/concurrent/EJBThreadFactoryA");

IntContext.set(2000);
StringContext.set("testParallelStreamBackedByManagedThreadFactoryEJB-2");

fj = new ForkJoinPool(4, threadFactory, null, false);

IntContext.set(3000);
StringContext.set("testParallelStreamBackedByManagedThreadFactoryEJB-3");

ForkJoinTask<Optional<Integer>> task = fj.submit(() -> {
return Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)
.parallelStream()
Expand All @@ -229,6 +225,9 @@ public void testParallelStreamBackedByManagedThreadFactoryEJB() throws Throwable
.reduce(Integer::sum);
});

IntContext.set(3000);
StringContext.set("testParallelStreamBackedByManagedThreadFactoryEJB-3");

Optional<Integer> result = task.join();
assertEquals(result.get(), Integer.valueOf(9180),
"Third-party context type IntContext must propagated to ForkJoin threads " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,15 @@ public void testManagedThreadFactoryDefinitionAllAttributes() throws Throwable {

ManagedThreadFactory threadFactory = InitialContext.doLookup("java:app/concurrent/ThreadFactoryA");

IntContext.set(162);
StringContext.set("testManagedThreadFactoryDefinitionAllAttributes-2");

Thread thread1 = threadFactory.newThread(() -> {});
Thread thread1 = threadFactory.newThread(() -> {
});
assertEquals(thread1.getPriority(), 4,
"New threads must be created with the priority that is specified on " +
"ManagedThreadFactoryDefinition");

BlockingQueue<Object> results = new LinkedBlockingQueue<Object>();

threadFactory.newThread(() -> {
Thread thread2 = threadFactory.newThread(() -> {
results.add(Thread.currentThread().getPriority());
results.add(StringContext.get());
results.add(IntContext.get());
Expand All @@ -89,7 +87,12 @@ public void testManagedThreadFactoryDefinitionAllAttributes() throws Throwable {
} catch (Throwable x) {
results.add(x);
}
}).start();
});

thread2.start();

IntContext.set(162);
StringContext.set("testManagedThreadFactoryDefinitionAllAttributes-2");

assertEquals(results.poll(MAX_WAIT_SECONDS, TimeUnit.SECONDS), Integer.valueOf(4),
"ManagedThreadFactory must start threads with the configured priority.");
Expand Down

0 comments on commit c891da3

Please sign in to comment.