-
Notifications
You must be signed in to change notification settings - Fork 138
/
AppServerStartupTest.java
595 lines (487 loc) · 23.1 KB
/
AppServerStartupTest.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
* Copyright (c) 2021, 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
*
* 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 com.sun.enterprise.v3.server;
import com.sun.appserv.server.util.Version;
import com.sun.enterprise.module.ModulesRegistry;
import com.sun.enterprise.module.bootstrap.StartupContext;
import com.sun.enterprise.module.single.StaticModulesRegistry;
import com.sun.enterprise.util.Result;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.glassfish.api.FutureProvider;
import org.glassfish.api.StartupRunLevel;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.event.EventListener;
import org.glassfish.api.event.EventTypes;
import org.glassfish.hk2.api.Context;
import org.glassfish.hk2.api.DynamicConfiguration;
import org.glassfish.hk2.api.DynamicConfigurationService;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.PreDestroy;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.glassfish.hk2.runlevel.RunLevel;
import org.glassfish.hk2.runlevel.RunLevelContext;
import org.glassfish.hk2.runlevel.RunLevelController;
import org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext;
import org.glassfish.hk2.runlevel.internal.RunLevelControllerImpl;
import org.glassfish.hk2.utilities.AbstractActiveDescriptor;
import org.glassfish.hk2.utilities.BuilderHelper;
import org.glassfish.hk2.utilities.DescriptorBuilder;
import org.glassfish.internal.api.InitRunLevel;
import org.glassfish.internal.api.PostStartupRunLevel;
import org.glassfish.kernel.event.EventsImpl;
import org.glassfish.main.core.apiexporter.APIExporterImpl;
import org.glassfish.server.ServerEnvironmentImpl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hk2.annotations.Service;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import static java.util.Collections.singletonList;
import static org.glassfish.api.admin.ServerEnvironment.Status.starting;
import static org.glassfish.api.event.EventTypes.PREPARE_SHUTDOWN;
import static org.glassfish.api.event.EventTypes.SERVER_READY;
import static org.glassfish.api.event.EventTypes.SERVER_SHUTDOWN;
import static org.glassfish.api.event.EventTypes.SERVER_STARTUP;
import static org.glassfish.hk2.runlevel.RunLevel.RUNLEVEL_MODE_META_TAG;
import static org.glassfish.hk2.runlevel.RunLevel.RUNLEVEL_VAL_META_TAG;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* AppServerStartup tests.
* <p>
* If test failed, look into logs, because exceptions are not thrown outside the {@link AppServerStartup}
* implementation.
*
* @author Tom Beerbower
*/
public class AppServerStartupTest {
/**
* The AppServerStartup instance to test.
*/
private AppServerStartup appServerStartup;
/**
* The test results.
*/
private static Results results;
/**
* Map of exceptions to be thrown from the postConstruct.
*/
private static Map<Class<?>, RuntimeException> mapPostConstructExceptions;
/**
* List of {@link Future}s returned from {@link FutureProvider#getFutures()} by the Startup services during progression
* to the start up run level.
*/
private static List<TestFuture> listFutures;
private ServiceLocator testLocator;
/**
* Reset the results prior to each test.
*/
@BeforeEach
public void beforeTest() {
testLocator = ServiceLocatorFactory.getInstance().create("AppServerStartupTest");
DynamicConfigurationService dcs = testLocator.getService(DynamicConfigurationService.class);
DynamicConfiguration config = dcs.createDynamicConfiguration();
config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(new TestSystemTasks()));
// These are services that would normally be started by hk2 core
config.addActiveDescriptor(AppServerStartup.AppInstanceListener.class);
AbstractActiveDescriptor<?> descriptor = BuilderHelper.createConstantDescriptor(new TestModulesRegistry());
descriptor.addContractType(ModulesRegistry.class);
config.addActiveDescriptor(descriptor);
descriptor = BuilderHelper.createConstantDescriptor(new ExecutorServiceFactory().provide());
descriptor.addContractType(ExecutorService.class);
config.addActiveDescriptor(descriptor);
config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(new ServerEnvironmentImpl()));
config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(new EventsImpl()));
config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(new Version()));
config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(new StartupContext()));
config.bind(BuilderHelper.link(RunLevelControllerImpl.class).to(RunLevelController.class).build());
config.addUnbindFilter(BuilderHelper.createContractFilter(RunLevelContext.class.getName()));
config.bind(BuilderHelper.link(RunLevelContext.class).named(RunLevelContext.CONTEXT_NAME).to(Context.class).in(Singleton.class)
.build());
config.addUnbindFilter(BuilderHelper.createContractFilter(AsyncRunLevelContext.class.getName()));
config.bind(BuilderHelper.link(AsyncRunLevelContext.class).in(Singleton.class).build());
config.bind(BuilderHelper.link(AppServerStartup.class).build());
descriptor = BuilderHelper.createConstantDescriptor(testLocator);
descriptor.addContractType(ServiceLocator.class);
config.addActiveDescriptor(descriptor);
bindService(config, TestInitRunLevelService.class);
bindService(config, TestStartupService.class);
bindService(config, TestStartupRunLevelService.class);
bindService(config, TestPostStartupRunLevelService.class);
bindService(config, CommonClassLoaderServiceImpl.class);
bindService(config, APIClassLoaderServiceImpl.class);
bindService(config, APIExporterImpl.class);
config.commit();
appServerStartup = testLocator.getService(AppServerStartup.class);
assertNotNull(appServerStartup);
mapPostConstructExceptions = new HashMap<>();
listFutures = new LinkedList<>();
results = new Results(appServerStartup.runLevelController);
appServerStartup.events.register(results);
assertEquals(starting, appServerStartup.serverEnvironment.getStatus());
}
/**
* Ensure that things are stopped after the test... if not then call stop.
*/
@AfterEach
public void afterTest() {
if (appServerStartup != null) {
// Force a stop to ensure that the services are released
if (appServerStartup.runLevelController.getCurrentRunLevel() > 0) {
appServerStartup.serverEnvironment.setStatus(ServerEnvironment.Status.started);
appServerStartup.stop();
}
appServerStartup.events.unregister(results);
}
results = null;
listFutures = null;
mapPostConstructExceptions = null;
ServiceLocatorFactory.getInstance().destroy(testLocator);
testLocator = null;
}
/**
* Call the {@link AppServerStartup#run} method and make sure that the run level services are constructed and destroyed
* at the proper run levels.
*/
@Test
public void testRunLevelServices() {
// Create the list of Futures returned from TestStartupService
listFutures.add(new TestFuture());
listFutures.add(new TestFuture());
listFutures.add(new TestFuture());
testRunAppServerStartup();
assertEquals(ServerEnvironment.Status.started, appServerStartup.serverEnvironment.getStatus());
assertThat(results.getListEvents(), hasSize(2));
assertEquals(SERVER_STARTUP, results.getListEvents().get(0));
assertEquals(SERVER_READY, results.getListEvents().get(1));
// assert that the run level services have been constructed
assertTrue(results.isConstructed(TestInitRunLevelService.class, InitRunLevel.VAL));
assertTrue(results.isConstructed(TestStartupService.class, StartupRunLevel.VAL));
assertTrue(results.isConstructed(TestStartupRunLevelService.class, StartupRunLevel.VAL));
assertTrue(results.isConstructed(TestPostStartupRunLevelService.class, PostStartupRunLevel.VAL));
appServerStartup.stop();
assertEquals(ServerEnvironment.Status.stopped, appServerStartup.serverEnvironment.getStatus());
assertThat(results.getListEvents(), hasSize(4));
assertEquals(PREPARE_SHUTDOWN, results.getListEvents().get(2));
assertEquals(SERVER_SHUTDOWN, results.getListEvents().get(3));
// assert that the run level services have been destroyed
assertTrue(results.isDestroyed(TestPostStartupRunLevelService.class, PostStartupRunLevel.VAL));
assertTrue(results.isDestroyed(TestStartupService.class, StartupRunLevel.VAL));
assertTrue(results.isDestroyed(TestStartupRunLevelService.class, StartupRunLevel.VAL));
assertTrue(results.isDestroyed(TestInitRunLevelService.class, InitRunLevel.VAL));
}
/**
* Test the {@link AppServerStartup#run} method with an exception thrown from an init service that should cause a
* failure during init. Make sure that the init run level services are constructed at the proper run levels.
*/
@Test
public void testRunLevelServicesWithInitException() {
testRunLevelServicesWithException(TestInitRunLevelService.class);
// make sure that the server has not been started
assertEquals(starting, appServerStartup.serverEnvironment.getStatus());
// assert that the run level services have been constructed
assertTrue(results.isConstructed(TestInitRunLevelService.class, InitRunLevel.VAL));
// assert that startup & post-startup services are not constructed since the failure occurs during init
assertFalse(results.isConstructed(TestStartupService.class));
assertFalse(results.isConstructed(TestStartupRunLevelService.class));
assertFalse(results.isConstructed(TestPostStartupRunLevelService.class));
}
/**
* Test the {@link AppServerStartup#run} method with an exception thrown from a startup service that should cause a
* failure during startup. Make sure that the init and startup run level services are constructed at the proper run
* levels.
*/
@Test
public void testRunLevelServicesWithStartupException() {
testRunLevelServicesWithException(TestStartupService.class);
// make sure that the server has not been started
assertEquals(starting, appServerStartup.serverEnvironment.getStatus());
assertTrue(results.getListEvents().contains(SERVER_SHUTDOWN));
// assert that the run level services have been constructed
assertTrue(results.isConstructed(TestInitRunLevelService.class, InitRunLevel.VAL));
assertTrue(results.isConstructed(TestStartupService.class, StartupRunLevel.VAL));
assertTrue(results.isConstructed(TestStartupRunLevelService.class, StartupRunLevel.VAL));
// assert that the post-startup service is not constructed since shutdown occurs during startup
assertFalse(results.isConstructed(TestPostStartupRunLevelService.class));
}
/**
* Test the {@link AppServerStartup#run} method with an exception thrown from a post-startup service that should cause a
* failure during post-startup. Make sure that the run level services are constructed at the proper run levels.
*/
@Test
public void testRunLevelServicesWithPostStartupException() {
testRunLevelServicesWithException(TestPostStartupRunLevelService.class);
assertEquals(ServerEnvironment.Status.started, appServerStartup.serverEnvironment.getStatus());
// assert that the run level services have been constructed
assertTrue(results.isConstructed(TestInitRunLevelService.class, InitRunLevel.VAL));
assertTrue(results.isConstructed(TestStartupService.class, StartupRunLevel.VAL));
assertTrue(results.isConstructed(TestStartupRunLevelService.class, StartupRunLevel.VAL));
assertTrue(results.isConstructed(TestPostStartupRunLevelService.class, PostStartupRunLevel.VAL));
}
/**
* Test the {@link AppServerStartup#run} method with an exception thrown from a {@link Future} should cause a failed
* result during startup. Make sure that the init and startup run level services are constructed at the proper run
* levels. Also ensure that the failed {@link Future} causes a shutdown.
*/
@Test
public void testRunLevelServicesWithFuturesException() {
// create the list of Futures returned from TestStartupService
listFutures.add(new TestFuture());
listFutures.add(new TestFuture(new Exception("Exception from Future.")));
listFutures.add(new TestFuture());
testRunAppServerStartup();
// make sure that the server has not been started
assertEquals(starting, appServerStartup.serverEnvironment.getStatus());
assertThat(results.getListEvents(), hasItem(SERVER_SHUTDOWN));
// assert that the run level services have been constructed
assertTrue(results.isConstructed(TestInitRunLevelService.class, InitRunLevel.VAL));
assertTrue(results.isConstructed(TestStartupService.class, StartupRunLevel.VAL));
assertTrue(results.isConstructed(TestStartupRunLevelService.class, StartupRunLevel.VAL));
// assert that the post-startup service is not constructed since shutdown occurs during startup
assertFalse(results.isConstructed(TestPostStartupRunLevelService.class));
}
private void bindService(DynamicConfiguration configurator, Class<?> service) {
final DescriptorBuilder descriptorBuilder = BuilderHelper.link(service);
final RunLevel runLevel = service.getAnnotation(RunLevel.class);
if (runLevel != null) {
descriptorBuilder.to(RunLevel.class)
.has(RUNLEVEL_VAL_META_TAG, singletonList(((Integer) runLevel.value()).toString()))
.has(RUNLEVEL_MODE_META_TAG, singletonList(((Integer) runLevel.mode()).toString()));
descriptorBuilder.in(RunLevel.class);
}
Class<?> clazz = service;
while (clazz != null) {
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> element : interfaces) {
descriptorBuilder.to(element);
}
clazz = clazz.getSuperclass();
}
final Named named = service.getAnnotation(Named.class);
if (named != null) {
descriptorBuilder.named(named.value());
}
configurator.bind(descriptorBuilder.build());
}
/**
* Helper method to call {@link AppServerStartup#run()}. Sets up an exception to be thrown from
* {@link PostConstruct#postConstruct()} of the given class.
*
* @param badServiceClass the service class that the exception will be thrown from
*/
private void testRunLevelServicesWithException(Class badServiceClass) {
// set an exception to be thrown from TestStartupService.postConstruct()
mapPostConstructExceptions.put(badServiceClass,
new RuntimeException("Exception from " + badServiceClass.getSimpleName() + ".postConstruct"));
// create the list of Futures returned from TestStartupService
listFutures.add(new TestFuture());
testRunAppServerStartup();
}
/**
* Helper method to run the app server after asserting that the results are clean.
*/
private void testRunAppServerStartup() {
// assert that we have clean results to start
assertFalse(results.isConstructed(TestInitRunLevelService.class));
assertFalse(results.isConstructed(TestStartupService.class));
assertFalse(results.isConstructed(TestStartupRunLevelService.class));
assertFalse(results.isConstructed(TestPostStartupRunLevelService.class));
appServerStartup.run();
}
/**
* Test results
*/
private static class Results implements EventListener {
/**
* Map of constructed run level services to run levels.
*/
private final Map<Class<?>, Integer> mapConstructedLevels = new HashMap<>();
/**
* Map of destroyed run level services to run levels.
*/
private final Map<Class<?>, Integer> mapDestroyedLevels = new HashMap<>();
/**
* List of server events.
*/
private final List<EventTypes<?>> listEvents = new LinkedList<>();
/**
* The run level service.
*/
private final RunLevelController RunLevelController
;
public Results(RunLevelController rls) {
this.RunLevelController = rls;
}
public void recordConstruction(Class<?> cl) {
mapConstructedLevels.put(cl, RunLevelController.getCurrentProceeding().getProposedLevel());
}
public void recordDestruction(Class<?> cl) {
mapDestroyedLevels.put(cl, RunLevelController.getCurrentRunLevel() + 1);
}
public boolean isConstructed(Class<?> cl) {
return mapConstructedLevels.keySet().contains(cl);
}
public boolean isConstructed(Class<?> cl, Integer runLevel) {
Integer recLevel = mapConstructedLevels.get(cl);
return recLevel != null && recLevel.equals(runLevel);
}
public boolean isDestroyed(Class<?> cl) {
return mapDestroyedLevels.keySet().contains(cl);
}
public boolean isDestroyed(Class<?> cl, Integer runLevel) {
Integer recLevel = mapDestroyedLevels.get(cl);
return recLevel != null && recLevel.equals(runLevel);
}
public List<EventTypes<?>> getListEvents() {
return listEvents;
}
@Override
public void event(Event<?> event) {
listEvents.add(event.type());
}
}
// ----- test services inner classes -------------------------------------
/**
* Abstract service that will update the test results from {@link PostConstruct#postConstruct()}.
*/
public static abstract class TestService implements PostConstruct, PreDestroy {
@Override
public void postConstruct() {
AppServerStartupTest.results.recordConstruction(this.getClass());
if (mapPostConstructExceptions != null) {
RuntimeException ex = mapPostConstructExceptions.get(getClass());
if (ex != null) {
throw ex;
}
}
}
@Override
public void preDestroy() {
AppServerStartupTest.results.recordDestruction(this.getClass());
}
}
/**
* Init service annotated with the new style {@link InitRunLevel} annotation.
*/
@Service
@RunLevel(InitRunLevel.VAL)
public static class TestInitRunLevelService extends TestService {
}
/**
* Startup service that implements the old style Startup interface.
*/
@RunLevel(StartupRunLevel.VAL)
@Service
public static class TestStartupService extends TestService implements FutureProvider {
// Make sure the other one starts first
@Inject
private TestStartupRunLevelService dependency;
@Override
public List getFutures() {
return listFutures;
}
}
/**
* Startup service annotated with the new style {@link StartupRunLevel} annotation.
*/
@Service
@RunLevel(StartupRunLevel.VAL)
public static class TestStartupRunLevelService extends TestService {
}
/**
* Post-startup service annotated with the new style {@link PostStartupRunLevel} annotation.
*/
@Service
@RunLevel(PostStartupRunLevel.VAL)
public static class TestPostStartupRunLevelService extends TestService {
}
// ----- TestSystemTasks inner classes -----------------------------------
/**
* Test {@link SystemTasks} implementation.
*/
public static class TestSystemTasks implements SystemTasks {
@Override
public void writePidFile() {
// do nothing.
}
}
// ----- TestModulesRegistry inner classes -------------------------------
/**
* Test {@link ModulesRegistry} implementation.
*/
public static class TestModulesRegistry extends StaticModulesRegistry {
public TestModulesRegistry() {
super(TestModulesRegistry.class.getClassLoader());
}
}
// ----- TestFuture inner classes ----------------------------------------
/**
* Future implementation used for test Startup implementations that also implement {@link FutureProvider}.
*/
public static class TestFuture implements Future<Result<Thread>> {
private boolean canceled;
private boolean done;
private Exception resultException;
public TestFuture() {
}
public TestFuture(Exception resultException) {
this.resultException = resultException;
}
@Override
public boolean cancel(boolean b) {
if (done) {
return false;
}
canceled = done = true;
return true;
}
@Override
public boolean isCancelled() {
return canceled;
}
@Override
public boolean isDone() {
return done;
}
@Override
public Result<Thread> get() throws InterruptedException, ExecutionException {
Result<Thread> result = resultException == null ? new Result<>(Thread.currentThread()) : new Result<>(resultException);
done = true;
return result;
}
@Override
public Result<Thread> get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
return get();
}
}
}