|
6 | 6 | import static org.mockito.Mockito.doThrow; |
7 | 7 | import static org.mockito.Mockito.mock; |
8 | 8 | import static org.mockito.Mockito.never; |
| 9 | +import static org.mockito.Mockito.times; |
9 | 10 | import static org.mockito.Mockito.verify; |
10 | 11 | import static org.mockito.Mockito.when; |
11 | 12 |
|
@@ -120,6 +121,76 @@ public void testStartAsyncPropagatesBindFailure() throws Exception { |
120 | 121 | verify(advertiser, never()).setConfigurationIndex(2); |
121 | 122 | } |
122 | 123 |
|
| 124 | + @Test |
| 125 | + public void testStartAsyncPropagatesSynchronousFailure() throws Exception { |
| 126 | + RuntimeException failure = new IllegalStateException("HTTP startup failed"); |
| 127 | + when(webHandler.start(any())).thenThrow(failure); |
| 128 | + |
| 129 | + CompletableFuture<Void> start = root.startAsync(); |
| 130 | + |
| 131 | + Assert.assertTrue(start.isCompletedExceptionally()); |
| 132 | + Assert.assertSame( |
| 133 | + failure, Assert.assertThrows(CompletionException.class, start::join).getCause()); |
| 134 | + verify(webHandler).stop(); |
| 135 | + verify(advertiser).stop(); |
| 136 | + verify(advertiser, never()).advertise(any(), anyInt(), any(), anyInt(), anyInt(), any()); |
| 137 | + root.setConfigurationIndex(2); |
| 138 | + verify(advertiser, never()).setConfigurationIndex(2); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testStartCleansUpSynchronousFailure() throws Exception { |
| 143 | + when(webHandler.start(any())).thenThrow(new IllegalStateException("HTTP startup failed")); |
| 144 | + |
| 145 | + root.start(); |
| 146 | + |
| 147 | + verify(webHandler).stop(); |
| 148 | + verify(advertiser).stop(); |
| 149 | + root.setConfigurationIndex(2); |
| 150 | + verify(advertiser, never()).setConfigurationIndex(2); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testDuplicateStartDoesNotStopPendingStartup() throws Exception { |
| 155 | + CompletableFuture<Integer> bind = new CompletableFuture<>(); |
| 156 | + when(webHandler.start(any())).thenReturn(bind); |
| 157 | + |
| 158 | + CompletableFuture<Void> first = root.startAsync(); |
| 159 | + CompletableFuture<Void> duplicate = root.startAsync(); |
| 160 | + |
| 161 | + Assert.assertTrue(duplicate.isCompletedExceptionally()); |
| 162 | + Assert.assertTrue( |
| 163 | + Assert.assertThrows(CompletionException.class, duplicate::join).getCause() |
| 164 | + instanceof IllegalStateException); |
| 165 | + Assert.assertFalse(first.isDone()); |
| 166 | + verify(webHandler, times(1)).start(any()); |
| 167 | + verify(webHandler, never()).stop(); |
| 168 | + verify(advertiser, never()).stop(); |
| 169 | + |
| 170 | + bind.complete(PORT); |
| 171 | + |
| 172 | + Assert.assertTrue(first.isDone()); |
| 173 | + first.join(); |
| 174 | + verify(advertiser).advertise(eq(LABEL), eq(1), any(), eq(PORT), eq(1), any()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testDuplicateStartDoesNotStopRunningRoot() throws Exception { |
| 179 | + root.startAsync().join(); |
| 180 | + |
| 181 | + CompletableFuture<Void> duplicate = root.startAsync(); |
| 182 | + |
| 183 | + Assert.assertTrue(duplicate.isCompletedExceptionally()); |
| 184 | + Assert.assertTrue( |
| 185 | + Assert.assertThrows(CompletionException.class, duplicate::join).getCause() |
| 186 | + instanceof IllegalStateException); |
| 187 | + verify(webHandler, times(1)).start(any()); |
| 188 | + verify(webHandler, never()).stop(); |
| 189 | + verify(advertiser, never()).stop(); |
| 190 | + root.setConfigurationIndex(2); |
| 191 | + verify(advertiser).setConfigurationIndex(2); |
| 192 | + } |
| 193 | + |
123 | 194 | @Test |
124 | 195 | public void testStartAsyncPropagatesAdvertisingFailure() throws Exception { |
125 | 196 | IOException failure = new IOException("mDNS registration failed"); |
|
0 commit comments