Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit e60d327

Browse files
committed
JERSEY-2291: Add test case to verify Immediate services are started immediately.
1 parent c492d6c commit e60d327

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.glassfish.jersey.internal.inject;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.concurrent.CountDownLatch;
6+
import java.util.concurrent.TimeUnit;
7+
8+
import javax.inject.Inject;
9+
10+
import org.glassfish.hk2.api.Immediate;
11+
import org.glassfish.hk2.api.ServiceLocator;
12+
import org.glassfish.hk2.utilities.binding.AbstractBinder;
13+
import org.junit.Test;
14+
15+
/**
16+
* Test for the {@link Injections} class.
17+
*
18+
* @author Jord Sonneveld ([email protected])
19+
*
20+
*/
21+
public class InjectionsTest {
22+
23+
/**
24+
* Verify that services marked with the HK2 Immediate annotation are indeed
25+
* created "immediately" (or at least "soon").
26+
*
27+
* Because Immediate services are instantiated in a separate thread, we use a
28+
* {@link CountDownLatch} to wait for the service to be created.
29+
*
30+
* After the {@link ServiceLocator} is created, we specifically do not call
31+
* any more methods on it: the locator must instantiate the Immediate service
32+
* without any further prompting to the locator.
33+
*
34+
* @throws InterruptedException
35+
* if awaiting on the latch is interrupted.
36+
*/
37+
@Test
38+
public void testHK2ImmediateAnnotation() throws InterruptedException {
39+
final CountDownLatch latch = new CountDownLatch(1);
40+
41+
@SuppressWarnings("unused") // It is unused by design
42+
ServiceLocator sl = Injections.createLocator(new AbstractBinder() {
43+
@Override
44+
protected void configure() {
45+
bind(latch).to(CountDownLatch.class);
46+
bind(ImmediateMe.class).to(ImmediateMe.class).in(Immediate.class);
47+
}
48+
});
49+
50+
// 10 seconds is a LONG time. It should be faster than that. However, 10
51+
// seconds gives us a reasonable upper limit to wait in case the test fails.
52+
assertTrue("Latch should be unlocked within 10 seconds.",
53+
latch.await(10, TimeUnit.SECONDS));
54+
}
55+
56+
/**
57+
* Helper class for testing Immediate services.
58+
*
59+
*/
60+
public static final class ImmediateMe {
61+
@Inject
62+
public ImmediateMe(CountDownLatch latch) {
63+
latch.countDown();
64+
}
65+
}
66+
67+
}

0 commit comments

Comments
 (0)