Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix managed thread factory tests - context is saved before execution, not jndi lookup #212

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Expand Up @@ -193,14 +196,8 @@ public void testParallelStreamBackedByManagedThreadFactory() throws Throwable {

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

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

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

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

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

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

Optional<Integer> result = task.join();
assertEquals(result.get(), Integer.valueOf(9180),
"Third-party context type IntContext must propagated to ForkJoin threads " +
Expand Down