|
1 | 1 | package io.github.hapjava.server.impl; |
2 | 2 |
|
3 | 3 | import static org.mockito.Matchers.any; |
| 4 | +import static org.mockito.Matchers.anyInt; |
4 | 5 | import static org.mockito.Matchers.eq; |
| 6 | +import static org.mockito.Mockito.doThrow; |
5 | 7 | import static org.mockito.Mockito.mock; |
6 | 8 | import static org.mockito.Mockito.never; |
| 9 | +import static org.mockito.Mockito.times; |
7 | 10 | import static org.mockito.Mockito.verify; |
8 | 11 | import static org.mockito.Mockito.when; |
9 | 12 |
|
|
13 | 16 | import io.github.hapjava.server.HomekitWebHandler; |
14 | 17 | import io.github.hapjava.server.impl.http.HomekitClientConnectionFactory; |
15 | 18 | import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser; |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.BindException; |
16 | 21 | import java.util.concurrent.CompletableFuture; |
| 22 | +import java.util.concurrent.CompletionException; |
17 | 23 | import org.junit.Assert; |
18 | 24 | import org.junit.Before; |
19 | 25 | import org.junit.Test; |
@@ -66,6 +72,140 @@ public void testWebHandlerStarts() throws Exception { |
66 | 72 | verify(webHandler).start(any(HomekitClientConnectionFactory.class)); |
67 | 73 | } |
68 | 74 |
|
| 75 | + @Test |
| 76 | + public void testFailedWebHandlerStartIsCleanedUp() throws Exception { |
| 77 | + CompletableFuture<Integer> bind = new CompletableFuture<>(); |
| 78 | + when(webHandler.start(any())).thenReturn(bind); |
| 79 | + |
| 80 | + root.start(); |
| 81 | + bind.completeExceptionally(new BindException("Address not available")); |
| 82 | + |
| 83 | + verify(webHandler).stop(); |
| 84 | + verify(advertiser).stop(); |
| 85 | + root.setConfigurationIndex(2); |
| 86 | + verify(advertiser, never()).setConfigurationIndex(2); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testStartAsyncWaitsForWebHandler() throws Exception { |
| 91 | + CompletableFuture<Integer> bind = new CompletableFuture<>(); |
| 92 | + when(webHandler.start(any())).thenReturn(bind); |
| 93 | + |
| 94 | + CompletableFuture<Void> start = root.startAsync(); |
| 95 | + |
| 96 | + Assert.assertFalse(start.isDone()); |
| 97 | + verify(advertiser, never()).advertise(any(), anyInt(), any(), anyInt(), anyInt(), any()); |
| 98 | + |
| 99 | + bind.complete(PORT); |
| 100 | + |
| 101 | + start.join(); |
| 102 | + verify(advertiser).advertise(eq(LABEL), eq(1), any(), eq(PORT), eq(1), any()); |
| 103 | + verify(webHandler, never()).stop(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testStartAsyncPropagatesBindFailure() throws Exception { |
| 108 | + CompletableFuture<Integer> bind = new CompletableFuture<>(); |
| 109 | + when(webHandler.start(any())).thenReturn(bind); |
| 110 | + BindException failure = new BindException("Address not available"); |
| 111 | + |
| 112 | + CompletableFuture<Void> start = root.startAsync(); |
| 113 | + bind.completeExceptionally(failure); |
| 114 | + |
| 115 | + Assert.assertSame( |
| 116 | + failure, Assert.assertThrows(CompletionException.class, start::join).getCause()); |
| 117 | + verify(webHandler).stop(); |
| 118 | + verify(advertiser).stop(); |
| 119 | + verify(advertiser, never()).advertise(any(), anyInt(), any(), anyInt(), anyInt(), any()); |
| 120 | + root.setConfigurationIndex(2); |
| 121 | + verify(advertiser, never()).setConfigurationIndex(2); |
| 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 | + |
| 194 | + @Test |
| 195 | + public void testStartAsyncPropagatesAdvertisingFailure() throws Exception { |
| 196 | + IOException failure = new IOException("mDNS registration failed"); |
| 197 | + doThrow(failure).when(advertiser).advertise(any(), anyInt(), any(), anyInt(), anyInt(), any()); |
| 198 | + |
| 199 | + CompletableFuture<Void> start = root.startAsync(); |
| 200 | + |
| 201 | + Assert.assertSame( |
| 202 | + failure, Assert.assertThrows(CompletionException.class, start::join).getCause()); |
| 203 | + verify(webHandler).stop(); |
| 204 | + verify(advertiser).stop(); |
| 205 | + root.setConfigurationIndex(2); |
| 206 | + verify(advertiser, never()).setConfigurationIndex(2); |
| 207 | + } |
| 208 | + |
69 | 209 | @Test |
70 | 210 | public void testWebHandlerStops() throws Exception { |
71 | 211 | root.start(); |
|
0 commit comments