|
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; |
7 | 9 | import static org.mockito.Mockito.verify; |
|
13 | 15 | import io.github.hapjava.server.HomekitWebHandler; |
14 | 16 | import io.github.hapjava.server.impl.http.HomekitClientConnectionFactory; |
15 | 17 | import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser; |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.BindException; |
16 | 20 | import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.CompletionException; |
17 | 22 | import org.junit.Assert; |
18 | 23 | import org.junit.Before; |
19 | 24 | import org.junit.Test; |
@@ -66,6 +71,70 @@ public void testWebHandlerStarts() throws Exception { |
66 | 71 | verify(webHandler).start(any(HomekitClientConnectionFactory.class)); |
67 | 72 | } |
68 | 73 |
|
| 74 | + @Test |
| 75 | + public void testFailedWebHandlerStartIsCleanedUp() throws Exception { |
| 76 | + CompletableFuture<Integer> bind = new CompletableFuture<>(); |
| 77 | + when(webHandler.start(any())).thenReturn(bind); |
| 78 | + |
| 79 | + root.start(); |
| 80 | + bind.completeExceptionally(new BindException("Address not available")); |
| 81 | + |
| 82 | + verify(webHandler).stop(); |
| 83 | + verify(advertiser).stop(); |
| 84 | + root.setConfigurationIndex(2); |
| 85 | + verify(advertiser, never()).setConfigurationIndex(2); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testStartAsyncWaitsForWebHandler() throws Exception { |
| 90 | + CompletableFuture<Integer> bind = new CompletableFuture<>(); |
| 91 | + when(webHandler.start(any())).thenReturn(bind); |
| 92 | + |
| 93 | + CompletableFuture<Void> start = root.startAsync(); |
| 94 | + |
| 95 | + Assert.assertFalse(start.isDone()); |
| 96 | + verify(advertiser, never()).advertise(any(), anyInt(), any(), anyInt(), anyInt(), any()); |
| 97 | + |
| 98 | + bind.complete(PORT); |
| 99 | + |
| 100 | + start.join(); |
| 101 | + verify(advertiser).advertise(eq(LABEL), eq(1), any(), eq(PORT), eq(1), any()); |
| 102 | + verify(webHandler, never()).stop(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testStartAsyncPropagatesBindFailure() throws Exception { |
| 107 | + CompletableFuture<Integer> bind = new CompletableFuture<>(); |
| 108 | + when(webHandler.start(any())).thenReturn(bind); |
| 109 | + BindException failure = new BindException("Address not available"); |
| 110 | + |
| 111 | + CompletableFuture<Void> start = root.startAsync(); |
| 112 | + bind.completeExceptionally(failure); |
| 113 | + |
| 114 | + Assert.assertSame( |
| 115 | + failure, Assert.assertThrows(CompletionException.class, start::join).getCause()); |
| 116 | + verify(webHandler).stop(); |
| 117 | + verify(advertiser).stop(); |
| 118 | + verify(advertiser, never()).advertise(any(), anyInt(), any(), anyInt(), anyInt(), any()); |
| 119 | + root.setConfigurationIndex(2); |
| 120 | + verify(advertiser, never()).setConfigurationIndex(2); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testStartAsyncPropagatesAdvertisingFailure() throws Exception { |
| 125 | + IOException failure = new IOException("mDNS registration failed"); |
| 126 | + doThrow(failure).when(advertiser).advertise(any(), anyInt(), any(), anyInt(), anyInt(), any()); |
| 127 | + |
| 128 | + CompletableFuture<Void> start = root.startAsync(); |
| 129 | + |
| 130 | + Assert.assertSame( |
| 131 | + failure, Assert.assertThrows(CompletionException.class, start::join).getCause()); |
| 132 | + verify(webHandler).stop(); |
| 133 | + verify(advertiser).stop(); |
| 134 | + root.setConfigurationIndex(2); |
| 135 | + verify(advertiser, never()).setConfigurationIndex(2); |
| 136 | + } |
| 137 | + |
69 | 138 | @Test |
70 | 139 | public void testWebHandlerStops() throws Exception { |
71 | 140 | root.start(); |
|
0 commit comments