Skip to content

Commit 1d2c90f

Browse files
committed
Expose asynchronous HomeKit startup failures
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
1 parent a162ea5 commit 1d2c90f

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎CHANGES.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# HAP-Java 2.0.10
2+
* Expose asynchronous HomeKit startup failures through `HomekitRoot.startAsync()` and clean up failed starts
23
* Add constructors with custom min/max to the window covering tilt angle characteristics [#162](https://github.com/hap-java/HAP-Java/issues/162)
34
* Add optional Mute characteristic to Doorbell service
45
* Fix leak of mDNS instances and shutdown hooks when stopping `HomekitRoot`

‎src/main/java/io/github/hapjava/server/impl/HomekitRoot.java‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser;
1111
import java.io.IOException;
1212
import java.net.InetAddress;
13+
import java.util.concurrent.CompletableFuture;
14+
import java.util.concurrent.CompletionException;
1315
import javax.jmdns.JmDNS;
1416
import org.slf4j.Logger;
1517
import org.slf4j.LoggerFactory;
@@ -158,12 +160,30 @@ public void removeAccessory(HomekitAccessory accessory) {
158160
* call this after you have used the {@link #addAccessory(HomekitAccessory)} method to add all the
159161
* initial accessories you plan on advertising, as any later additions will cause the HomeKit
160162
* clients to reconnect.
163+
*
164+
* <p>Startup failures are logged. Use {@link #startAsync()} to handle them in the caller.
161165
*/
162166
public void start() {
167+
startAsync()
168+
.whenComplete(
169+
(ignored, error) -> {
170+
if (error != null) {
171+
logger.error("Could not start HomeKit root {}", label, error);
172+
}
173+
});
174+
}
175+
176+
/**
177+
* Starts handling and advertising the previously added HomeKit accessories.
178+
*
179+
* @return a future that completes after the HTTP listener has started and the advertiser has
180+
* registered the service, or exceptionally after a failed start has been cleaned up
181+
*/
182+
public CompletableFuture<Void> startAsync() {
163183
started = true;
164184
madeChanges = false;
165185
registry.reset();
166-
webHandler
186+
return webHandler
167187
.start(
168188
new HomekitClientConnectionFactoryImpl(authInfo, registry, subscriptions, advertiser))
169189
.thenAccept(
@@ -178,7 +198,14 @@ public void start() {
178198
configurationIndex,
179199
authInfo.getSetupId());
180200
} catch (Exception e) {
181-
throw new RuntimeException(e);
201+
throw new CompletionException(e);
202+
}
203+
})
204+
.whenComplete(
205+
(ignored, error) -> {
206+
if (error != null) {
207+
started = false;
208+
stop();
182209
}
183210
});
184211
}

‎src/test/java/io/github/hapjava/server/impl/HomekitRootTest.java‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.github.hapjava.server.impl;
22

33
import static org.mockito.Matchers.any;
4+
import static org.mockito.Matchers.anyInt;
45
import static org.mockito.Matchers.eq;
6+
import static org.mockito.Mockito.doThrow;
57
import static org.mockito.Mockito.mock;
68
import static org.mockito.Mockito.never;
79
import static org.mockito.Mockito.verify;
@@ -13,7 +15,10 @@
1315
import io.github.hapjava.server.HomekitWebHandler;
1416
import io.github.hapjava.server.impl.http.HomekitClientConnectionFactory;
1517
import io.github.hapjava.server.impl.jmdns.JmdnsHomekitAdvertiser;
18+
import java.io.IOException;
19+
import java.net.BindException;
1620
import java.util.concurrent.CompletableFuture;
21+
import java.util.concurrent.CompletionException;
1722
import org.junit.Assert;
1823
import org.junit.Before;
1924
import org.junit.Test;
@@ -66,6 +71,70 @@ public void testWebHandlerStarts() throws Exception {
6671
verify(webHandler).start(any(HomekitClientConnectionFactory.class));
6772
}
6873

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+
69138
@Test
70139
public void testWebHandlerStops() throws Exception {
71140
root.start();

0 commit comments

Comments
 (0)