|
24 | 24 | @Test(groups = "integration") |
25 | 25 | public class EventsCmdImplTest extends AbstractDockerClientTest { |
26 | 26 |
|
27 | | - private static int KNOWN_NUM_EVENTS = 4; |
28 | | - |
29 | | - private static String getEpochTime() { |
30 | | - return String.valueOf(System.currentTimeMillis() / 1000); |
31 | | - } |
32 | | - |
33 | | - @BeforeTest |
34 | | - public void beforeTest() throws DockerException { |
35 | | - super.beforeTest(); |
36 | | - } |
37 | | - |
38 | | - @AfterTest |
39 | | - public void afterTest() { |
40 | | - super.afterTest(); |
41 | | - } |
42 | | - |
43 | | - @BeforeMethod |
44 | | - public void beforeMethod(Method method) { |
45 | | - super.beforeMethod(method); |
46 | | - } |
47 | | - |
48 | | - @AfterMethod |
49 | | - public void afterMethod(ITestResult result) { |
50 | | - super.afterMethod(result); |
51 | | - } |
52 | | - |
53 | | - @Test |
54 | | - public void testEventStreamTimeBound() throws InterruptedException, IOException { |
55 | | - // Don't include other tests events |
56 | | - TimeUnit.SECONDS.sleep(1); |
57 | | - |
58 | | - String startTime = getEpochTime(); |
59 | | - int expectedEvents = generateEvents(); |
60 | | - String endTime = getEpochTime(); |
61 | | - |
62 | | - CountDownLatch countDownLatch = new CountDownLatch(expectedEvents); |
63 | | - EventCallbackTest eventCallback = new EventCallbackTest(countDownLatch); |
64 | | - |
65 | | - EventsCmd eventsCmd = dockerClient.eventsCmd(eventCallback).withSince(startTime).withUntil(endTime); |
66 | | - ExecutorService executorService = eventsCmd.exec(); |
67 | | - |
68 | | - boolean zeroCount = countDownLatch.await(5, TimeUnit.SECONDS); |
69 | | - |
70 | | - executorService.shutdown(); |
71 | | - eventCallback.close(); |
72 | | - |
73 | | - assertTrue(zeroCount, "Expected 4 events, [create, start, die, stop]"); |
74 | | - } |
75 | | - |
76 | | - @Test |
77 | | - public void testEventStreaming() throws InterruptedException, IOException { |
78 | | - // Don't include other tests events |
79 | | - TimeUnit.SECONDS.sleep(1); |
80 | | - |
81 | | - CountDownLatch countDownLatch = new CountDownLatch(KNOWN_NUM_EVENTS); |
82 | | - EventCallbackTest eventCallback = new EventCallbackTest(countDownLatch); |
83 | | - |
84 | | - EventsCmd eventsCmd = dockerClient.eventsCmd(eventCallback).withSince(getEpochTime()); |
85 | | - ExecutorService executorService = eventsCmd.exec(); |
86 | | - |
87 | | - generateEvents(); |
88 | | - |
89 | | - boolean zeroCount = countDownLatch.await(5, TimeUnit.SECONDS); |
90 | | - executorService.shutdown(); |
91 | | - eventCallback.close(); |
92 | | - assertTrue(zeroCount, "Expected 4 events, [create, start, die, stop]"); |
93 | | - } |
94 | | - |
95 | | - /** |
96 | | - * This method generates {#link KNOWN_NUM_EVENTS} events |
97 | | - */ |
98 | | - private int generateEvents() { |
99 | | - String testImage = "busybox"; |
100 | | - asString(dockerClient.pullImageCmd(testImage).exec()); |
101 | | - CreateContainerResponse container = dockerClient |
102 | | - .createContainerCmd(testImage).withCmd("sleep", "9999").exec(); |
103 | | - dockerClient.startContainerCmd(container.getId()).exec(); |
104 | | - dockerClient.stopContainerCmd(container.getId()).exec(); |
105 | | - return KNOWN_NUM_EVENTS; |
106 | | - } |
107 | | - |
108 | | - private class EventCallbackTest implements EventCallback { |
109 | | - private final CountDownLatch countDownLatch; |
110 | | - private final AtomicBoolean isReceiving = new AtomicBoolean(true); |
111 | | - |
112 | | - public EventCallbackTest(CountDownLatch countDownLatch) { |
113 | | - this.countDownLatch = countDownLatch; |
114 | | - } |
115 | | - |
116 | | - public void close() { |
117 | | - isReceiving.set(false); |
118 | | - } |
119 | | - |
120 | | - @Override |
121 | | - public void onEvent(Event event) { |
122 | | - LOG.info("Received event #{}: {}", countDownLatch.getCount(), event); |
123 | | - countDownLatch.countDown(); |
124 | | - } |
125 | | - |
126 | | - @Override |
127 | | - public void onException(Throwable throwable) { |
128 | | - LOG.error("Error occurred: {}", throwable.getMessage()); |
129 | | - } |
130 | | - |
131 | | - @Override |
132 | | - public void onCompletion(int numEvents) { |
133 | | - LOG.info("Number of events received: {}", numEvents); |
134 | | - } |
135 | | - |
136 | | - @Override |
137 | | - public boolean isReceiving() { |
138 | | - return isReceiving.get(); |
139 | | - } |
140 | | - } |
| 27 | + private static int KNOWN_NUM_EVENTS = 4; |
| 28 | + |
| 29 | + private static String getEpochTime() { |
| 30 | + return String.valueOf(System.currentTimeMillis() / 1000); |
| 31 | + } |
| 32 | + |
| 33 | + @BeforeTest |
| 34 | + public void beforeTest() throws DockerException { |
| 35 | + super.beforeTest(); |
| 36 | + } |
| 37 | + |
| 38 | + @AfterTest |
| 39 | + public void afterTest() { |
| 40 | + super.afterTest(); |
| 41 | + } |
| 42 | + |
| 43 | + @BeforeMethod |
| 44 | + public void beforeMethod(Method method) { |
| 45 | + super.beforeMethod(method); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterMethod |
| 49 | + public void afterMethod(ITestResult result) { |
| 50 | + super.afterMethod(result); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testEventStreamTimeBound() throws InterruptedException, |
| 55 | + IOException { |
| 56 | + // Don't include other tests events |
| 57 | + TimeUnit.SECONDS.sleep(1); |
| 58 | + |
| 59 | + String startTime = getEpochTime(); |
| 60 | + int expectedEvents = generateEvents(); |
| 61 | + String endTime = getEpochTime(); |
| 62 | + |
| 63 | + CountDownLatch countDownLatch = new CountDownLatch(expectedEvents); |
| 64 | + EventCallbackTest eventCallback = new EventCallbackTest(countDownLatch); |
| 65 | + |
| 66 | + EventsCmd eventsCmd = dockerClient.eventsCmd(eventCallback) |
| 67 | + .withSince(startTime).withUntil(endTime); |
| 68 | + ExecutorService executorService = eventsCmd.exec(); |
| 69 | + |
| 70 | + boolean zeroCount = countDownLatch.await(5, TimeUnit.SECONDS); |
| 71 | + |
| 72 | + executorService.shutdown(); |
| 73 | + eventCallback.close(); |
| 74 | + |
| 75 | + assertTrue(zeroCount, "Expected 4 events, [create, start, die, stop]"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testEventStreaming() throws InterruptedException, IOException { |
| 80 | + // Don't include other tests events |
| 81 | + TimeUnit.SECONDS.sleep(1); |
| 82 | + |
| 83 | + CountDownLatch countDownLatch = new CountDownLatch(KNOWN_NUM_EVENTS); |
| 84 | + EventCallbackTest eventCallback = new EventCallbackTest(countDownLatch); |
| 85 | + |
| 86 | + EventsCmd eventsCmd = dockerClient.eventsCmd(eventCallback).withSince( |
| 87 | + getEpochTime()); |
| 88 | + ExecutorService executorService = eventsCmd.exec(); |
| 89 | + |
| 90 | + generateEvents(); |
| 91 | + |
| 92 | + boolean zeroCount = countDownLatch.await(5, TimeUnit.SECONDS); |
| 93 | + executorService.shutdown(); |
| 94 | + eventCallback.close(); |
| 95 | + assertTrue(zeroCount, "Expected 4 events, [create, start, die, stop]"); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * This method generates {#link KNOWN_NUM_EVENTS} events |
| 100 | + */ |
| 101 | + private int generateEvents() { |
| 102 | + String testImage = "busybox"; |
| 103 | + asString(dockerClient.pullImageCmd(testImage).exec()); |
| 104 | + CreateContainerResponse container = dockerClient |
| 105 | + .createContainerCmd(testImage).withCmd("sleep", "9999").exec(); |
| 106 | + dockerClient.startContainerCmd(container.getId()).exec(); |
| 107 | + dockerClient.stopContainerCmd(container.getId()).exec(); |
| 108 | + return KNOWN_NUM_EVENTS; |
| 109 | + } |
| 110 | + |
| 111 | + private class EventCallbackTest implements EventCallback { |
| 112 | + private final CountDownLatch countDownLatch; |
| 113 | + private final AtomicBoolean isReceiving = new AtomicBoolean(true); |
| 114 | + |
| 115 | + public EventCallbackTest(CountDownLatch countDownLatch) { |
| 116 | + this.countDownLatch = countDownLatch; |
| 117 | + } |
| 118 | + |
| 119 | + public void close() { |
| 120 | + isReceiving.set(false); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onEvent(Event event) { |
| 125 | + LOG.info("Received event #{}: {}", countDownLatch.getCount(), event); |
| 126 | + countDownLatch.countDown(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void onException(Throwable throwable) { |
| 131 | + LOG.error("Error occurred: {}", throwable.getMessage()); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void onCompletion(int numEvents) { |
| 136 | + LOG.info("Number of events received: {}", numEvents); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean isReceiving() { |
| 141 | + return isReceiving.get(); |
| 142 | + } |
| 143 | + } |
141 | 144 | } |
0 commit comments