blob: 3a6aa03ad45c6bdb2289d529b258424b138026aa (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;
import com.google.inject.Inject;
import com.yahoo.jdisc.service.CurrentContainer;
import com.yahoo.jdisc.test.TestDriver;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Simon Thoresen Hult
*/
public class AbstractApplicationTestCase {
@Test
public void requireThatContainerApiIsAvailable() {
TestDriver driver = TestDriver.newInjectedApplicationInstance(MyApplication.class);
MyApplication app = (MyApplication)driver.application();
app.activateContainer(app.newContainerBuilder());
assertNotNull(app.container());
assertTrue(driver.close());
}
@Test
public void requireThatDestroySignalsTermination() {
TestDriver driver = TestDriver.newInjectedApplicationInstance(MyApplication.class);
MyApplication app = (MyApplication)driver.application();
assertFalse(app.isTerminated());
assertTrue(driver.close());
assertTrue(app.isTerminated());
}
@Test
public void requireThatTerminationCanBeWaitedForWithTimeout() throws InterruptedException {
TestDriver driver = TestDriver.newInjectedApplicationInstance(MyApplication.class);
final MyApplication app = (MyApplication)driver.application();
final CountDownLatch latch = new CountDownLatch(1);
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
app.awaitTermination(600, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
});
assertFalse(latch.await(100, TimeUnit.MILLISECONDS));
assertTrue(driver.close());
assertTrue(latch.await(600, TimeUnit.SECONDS));
}
@Test
public void requireThatTerminationCanBeWaitedForWithoutTimeout() throws InterruptedException {
TestDriver driver = TestDriver.newInjectedApplicationInstance(MyApplication.class);
final MyApplication app = (MyApplication)driver.application();
final CountDownLatch latch = new CountDownLatch(1);
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
app.awaitTermination();
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}
});
assertFalse(latch.await(100, TimeUnit.MILLISECONDS));
assertTrue(driver.close());
assertTrue(latch.await(600, TimeUnit.SECONDS));
}
private static class MyApplication extends AbstractApplication {
@Inject
public MyApplication(BundleInstaller bundleInstaller, ContainerActivator activator,
CurrentContainer container) {
super(bundleInstaller, activator, container);
}
@Override
public void start() {
}
}
}
|