-
Notifications
You must be signed in to change notification settings - Fork 37
/
DeploymentDescriptorServlet.java
327 lines (259 loc) · 16.4 KB
/
DeploymentDescriptorServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.concurrent.spec.Platform.dd;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ee.jakarta.tck.concurrent.common.context.IntContext;
import ee.jakarta.tck.concurrent.common.context.StringContext;
import ee.jakarta.tck.concurrent.common.qualifiers.CustomQualifier1;
import ee.jakarta.tck.concurrent.common.qualifiers.CustomQualifier2;
import ee.jakarta.tck.concurrent.common.qualifiers.InvalidQualifier3;
import ee.jakarta.tck.concurrent.framework.TestServlet;
import jakarta.annotation.Resource;
import jakarta.ejb.EJB;
import jakarta.enterprise.concurrent.ContextService;
import jakarta.enterprise.concurrent.ManagedExecutorService;
import jakarta.enterprise.concurrent.ManagedScheduledExecutorService;
import jakarta.enterprise.concurrent.ManagedThreadFactory;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.inject.Inject;
import jakarta.servlet.annotation.WebServlet;
import jakarta.transaction.Status;
import jakarta.transaction.UserTransaction;
@WebServlet("/DeploymentDescriptorServlet")
@Dependent
public class DeploymentDescriptorServlet extends TestServlet {
private static final long serialVersionUID = 1L;
private static final long MAX_WAIT_SECONDS = TimeUnit.MINUTES.toSeconds(2);
private static final Runnable NOOP_RUNNABLE = () -> { };
@EJB
private DeploymentDescriptorTestBeanInterface enterpriseBean;
@Resource
private UserTransaction tx;
private Integer executeCallableWithContext(final ContextService svc, final int value) throws Exception {
try {
IntContext.set(value);
Callable<Integer> result = svc.contextualCallable(() -> {
return IntContext.get();
});
return result.call();
} finally {
IntContext.set(0);
}
}
// Context Services
@Inject
private ContextService injectedDefContextSvc;
@Inject
@CustomQualifier1
private ContextService injectedContextD;
public void testDeploymentDescriptorDefinedContextServiceQualifiers() throws Throwable {
assertAll("Context Service Tests",
() -> assertNotNull(injectedDefContextSvc,
"Default contextService was not registered with default qualifier."),
() -> assertNotNull(injectedContextD,
"Deployment Descriptor defined contextService was not registered with required qualifier."),
() -> assertTrue(CDI.current().select(ContextService.class, InvalidQualifier3.Literal.get()).isUnsatisfied(),
"A contextService was satisfied with a qualifier which was not defined in it's deployment descriptior")
);
// Verify injected and lookup default context service are the same
ContextService lookupDefContextSvc = InitialContext.doLookup("java:comp/DefaultContextService");
Integer expected1 = executeCallableWithContext(lookupDefContextSvc, 95);
Integer actual1 = executeCallableWithContext(injectedDefContextSvc, 95);
assertEquals(expected1, actual1, "Default Context Service behavior differed between injection and lookup");
// Verify injected and lookup deployment descriptor defined context service are the same
ContextService lookupContextD = InitialContext.doLookup("java:app/concurrent/ContextD");
assertEquals(Integer.valueOf(0), executeCallableWithContext(lookupContextD, 65),
"Deployment descriptor defined Context Service that was looked up did not clear the IntContext as configured.");
assertEquals(Integer.valueOf(0), executeCallableWithContext(injectedContextD, 85),
"Deployment descriptor defined Context Service that was injected based on a qualifier did not clear the IntContext as configured.");
}
// Managed Executor Services
@Inject
private ManagedExecutorService injectedDefMES;
@Inject
@CustomQualifier2
private ManagedExecutorService injectedMESD;
public void testDeploymentDescriptorDefinedManagedExecutorSvcQualifiers() throws Throwable {
assertAll("Managed Executor Service Tests",
() -> assertNotNull(injectedDefMES,
"Default managedExecutorService was not registered with default qualifier."),
() -> assertNotNull(injectedMESD,
"Deployment Descriptor defined managedExecutorService was not registered with required qualifiers."),
() -> assertTrue(CDI.current().select(ManagedExecutorService.class, CustomQualifier2.Literal.get(), InvalidQualifier3.Literal.get()).isUnsatisfied(),
"A managedExecutorService was satisfied with both a required and non-required qualifier.")
);
// Verify injected and looked up default ManagedExecutorService behave the same
ManagedExecutorService lookupDefMES = InitialContext.doLookup("java:comp/DefaultManagedExecutorService");
ContextService lookupDefContextSvc = lookupDefMES.getContextService();
ContextService extractedDefContextSvc = injectedDefMES.getContextService();
Integer expected1 = executeCallableWithContext(lookupDefContextSvc, 95);
Integer actual1 = executeCallableWithContext(extractedDefContextSvc, 95);
assertEquals(expected1, actual1, "Default ManagedExecutorService behavior via context service differed between injection and lookup");
// Verify injected and lookup deployment descriptor defined ManagedExecutorService behave the same
ManagedExecutorService lookupMESD = InitialContext.doLookup("java:app/concurrent/ExecutorD");
ContextService lookupContextE = lookupMESD.getContextService();
ContextService extractedContextE = injectedMESD.getContextService();
assertEquals(Integer.valueOf(0), executeCallableWithContext(lookupContextE, 65),
"Deployment descriptor defined and looked up ManagedExecutorService was configured with a context service that did not clear the IntContext as configured.");
assertEquals(Integer.valueOf(0), executeCallableWithContext(extractedContextE, 85),
"Deployment descriptor defined and injected ManagedExecutorService was configured with a context service that did not clear the IntContext as configured.");
}
// Managed Scheduled Executor Services
@Inject
private ManagedScheduledExecutorService injectedDefMSES;
@Inject
@CustomQualifier1
@CustomQualifier2
private ManagedScheduledExecutorService injectedMSESD;
public void testDeploymentDescriptorDefinedManagedScheduledExecutorSvcQualifers() throws Throwable {
assertAll("Managed Scheduled Executor Service Tests",
() -> assertNotNull(injectedDefMSES,
"Default managedScheduledExecutorService was not registered with default qualifier."),
() -> assertNotNull(injectedMSESD,
"Deployment Descriptor defined managedScheduledExecutorService was not registered with required qualifiers."),
() -> assertTrue(CDI.current().select(ManagedScheduledExecutorService.class, CustomQualifier1.Literal.get()).isResolvable(),
"A managedScheduledExecutorService was not satisfied with one of two configured qualifiers.")
);
// Verify injected and looked up default ManagedScheduledExecutorService behave the same
ManagedScheduledExecutorService lookupDefMSES = InitialContext.doLookup("java:comp/DefaultManagedScheduledExecutorService");
ContextService lookupDefContextSvc = lookupDefMSES.getContextService();
ContextService extractedDefContextSvc = injectedDefMSES.getContextService();
Integer expected1 = executeCallableWithContext(lookupDefContextSvc, 95);
Integer actual1 = executeCallableWithContext(extractedDefContextSvc, 95);
assertEquals(expected1, actual1, "Default ManagedScheduledExecutorService behavior via context service differed between injection and lookup");
// Verify injected and looked up annotation defined ManagedExecutorService behave the same
ManagedScheduledExecutorService lookupMSESD = InitialContext.doLookup("java:app/concurrent/ScheduledExecutorD");
ContextService lookupContextE = lookupMSESD.getContextService();
ContextService extractedContextE = injectedMSESD.getContextService();
assertEquals(Integer.valueOf(0), executeCallableWithContext(lookupContextE, 65),
"Deployment descriptor defined and looked up ManagedScheduledExecutorService"
+ " was configured with a context service that did not clear the IntContext as configured.");
assertEquals(Integer.valueOf(0), executeCallableWithContext(extractedContextE, 85),
"Deployment descriptor defined and injected ManagedScheduledExecutorService"
+ " was configured with a context service that did not clear the IntContext as configured.");
}
// Managed Thread Factory
@Inject
private ManagedThreadFactory injectedDefMTF;
@Resource(lookup = "java:app/concurrent/ThreadFactoryD")
private ManagedThreadFactory resourceMTFD;
public void testDeploymentDescriptorDefinedManagedThreadFactoryQualifers() throws Throwable {
ManagedThreadFactory lookupDefMTF = InitialContext.doLookup("java:comp/DefaultManagedThreadFactory");
ManagedThreadFactory lookupMTFD = InitialContext.doLookup("java:app/concurrent/ThreadFactoryD");
assertAll("Thread Factory Tests",
() -> assertNotNull(injectedDefMTF,
"A managedThreadFactory was not registered with the default qualifier when the application included a producer."),
() -> assertEquals(5, lookupDefMTF.newThread(NOOP_RUNNABLE).getPriority(),
"Default managedThreadFactory from lookup did not have the default priority."),
() -> assertEquals(7, injectedDefMTF.newThread(NOOP_RUNNABLE).getPriority(),
"Default managedThreadFactory from injection did not have the expected priority."),
() -> assertNotNull(resourceMTFD,
"Deployment Descriptor defined managedThreadFactory with no qualifiers could not be found via @Resource."),
() -> assertEquals(lookupMTFD.newThread(NOOP_RUNNABLE).getPriority(), resourceMTFD.newThread(NOOP_RUNNABLE).getPriority(),
"The managedThreadFactory from resource injection and lookup did not have the same priority."),
() -> assertTrue(CDI.current().select(ManagedThreadFactory.class, CustomQualifier1.Literal.get()).isUnsatisfied(),
"A managedThreadFactory was satisfied with a required qualifier that was not configured on the deployment descriptor.")
);
}
public void testDeploymentDescriptorDefinesContextService() throws Throwable {
ContextService contextSvc = InitialContext.doLookup("java:app/concurrent/ContextD");
Callable<Integer> checkContextAndGetTransactionStatus;
tx.begin();
try {
IntContext.set(1001);
StringContext.set("testDeploymentDescriptorDefinesContextService-1");
checkContextAndGetTransactionStatus = contextSvc.contextualCallable(() -> {
assertEquals(0, IntContext.get()); // cleared
assertEquals("testDeploymentDescriptorDefinesContextService-1", StringContext.get()); // propagated
assertNotNull(InitialContext.doLookup("java:app/concurrent/ExecutorD")); // propagated
return tx.getStatus(); // unchanged
});
StringContext.set("testDeploymentDescriptorDefinesContextService-2");
int status = checkContextAndGetTransactionStatus.call();
assertEquals(Status.STATUS_ACTIVE, status);
assertEquals(1001, IntContext.get()); // restored
assertEquals("testDeploymentDescriptorDefinesContextService-2", StringContext.get()); // restored
} finally {
IntContext.set(0);
StringContext.set(null);
tx.rollback();
}
int status = checkContextAndGetTransactionStatus.call();
assertEquals(Status.STATUS_NO_TRANSACTION, status);
}
public void testDeploymentDescriptorDefinesManagedExecutor() throws Throwable {
LinkedBlockingQueue<Object> started = new LinkedBlockingQueue<Object>();
CountDownLatch taskCanEnd = new CountDownLatch(1);
Supplier<String> task = () -> {
try {
started.add(InitialContext.doLookup("java:app/concurrent/ExecutorD")); // requires Application context
assertTrue(taskCanEnd.await(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
} catch (InterruptedException | NamingException x) {
throw new CompletionException(x);
}
return StringContext.get();
};
ManagedExecutorService executor = InitialContext.doLookup("java:app/concurrent/ExecutorD");
try {
StringContext.set("testDeploymentDescriptorDefinesManagedExecutor-1");
CompletableFuture<String> future1 = executor.supplyAsync(task);
StringContext.set("testDeploymentDescriptorDefinesManagedExecutor-2");
CompletableFuture<String> future2 = executor.supplyAsync(task);
StringContext.set("testDeploymentDescriptorDefinesManagedExecutor-3");
CompletableFuture<String> future3 = executor.supplyAsync(task);
StringContext.set("testDeploymentDescriptorDefinesManagedExecutor-4");
CompletableFuture<String> future4 = executor.supplyAsync(task);
StringContext.set("testDeploymentDescriptorDefinesManagedExecutor-5");
// 3 can start per max-async
assertNotNull(started.poll(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
assertNotNull(started.poll(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
assertNotNull(started.poll(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
assertEquals(null, started.poll(1, TimeUnit.SECONDS));
taskCanEnd.countDown();
assertEquals("testDeploymentDescriptorDefinesManagedExecutor-1",
future1.get(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
assertEquals("testDeploymentDescriptorDefinesManagedExecutor-2",
future2.get(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
assertEquals("testDeploymentDescriptorDefinesManagedExecutor-3",
future3.get(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
assertEquals("testDeploymentDescriptorDefinesManagedExecutor-4",
future4.get(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
assertNotNull(started.poll(MAX_WAIT_SECONDS, TimeUnit.SECONDS));
} finally {
IntContext.set(0);
StringContext.set(null);
}
}
public void testDeploymentDescriptorDefinesManagedScheduledExecutor() throws Throwable {
enterpriseBean.testDeploymentDescriptorDefinesManagedScheduledExecutor();
}
public void testDeploymentDescriptorDefinesManagedThreadFactory() throws Throwable {
enterpriseBean.testDeploymentDescriptorDefinesManagedThreadFactory();
}
}