Skip to content

Commit bc6a12e

Browse files
committed
Review comments
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
1 parent f72337b commit bc6a12e

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/hardware/FritzAhaWebInterface.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,17 @@ public void onComplete(@NonNullByDefault({}) Result result) {
210210

211211
private void completeAuthentication(CompletableFuture<Boolean> currentAuthentication, @Nullable String newSid,
212212
ThingStatusDetail statusDetail, @Nullable String description) {
213-
boolean currentAttempt;
214213
synchronized (authenticationLock) {
215-
currentAttempt = currentAuthentication.equals(authentication) && !disposed;
216-
if (currentAttempt) {
217-
sid = newSid;
218-
authentication = null;
214+
if (currentAuthentication != authentication || disposed) {
215+
currentAuthentication.complete(false);
216+
return;
219217
}
218+
sid = newSid;
219+
boolean authenticated = newSid != null;
220+
handler.setStatusInfo(authenticated ? ThingStatus.ONLINE : ThingStatus.OFFLINE, statusDetail, description);
221+
currentAuthentication.complete(authenticated);
222+
authentication = null;
220223
}
221-
if (!currentAttempt) {
222-
currentAuthentication.complete(false);
223-
return;
224-
}
225-
boolean authenticated = newSid != null;
226-
handler.setStatusInfo(authenticated ? ThingStatus.ONLINE : ThingStatus.OFFLINE, statusDetail, description);
227-
currentAuthentication.complete(authenticated);
228224
}
229225

230226
/**

bundles/org.openhab.binding.avmfritz/src/test/java/org/openhab/binding/avmfritz/internal/hardware/FritzAhaWebInterfaceTest.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
package org.openhab.binding.avmfritz.internal.hardware;
1414

1515
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertSame;
1617
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
1718
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.Mockito.doAnswer;
1821

1922
import java.io.BufferedReader;
2023
import java.io.InputStreamReader;
@@ -27,6 +30,8 @@
2730
import java.util.concurrent.ExecutorService;
2831
import java.util.concurrent.Executors;
2932
import java.util.concurrent.TimeUnit;
33+
import java.util.concurrent.atomic.AtomicBoolean;
34+
import java.util.concurrent.atomic.AtomicReference;
3035

3136
import org.eclipse.jdt.annotation.NonNullByDefault;
3237
import org.eclipse.jetty.client.HttpClient;
@@ -113,6 +118,25 @@ protected String createResponse(String challenge) {
113118
}
114119
}
115120

121+
@Test
122+
public void authenticationRemainsCurrentUntilStatusUpdateCompletes() {
123+
AVMFritzBoxConfiguration config = new AVMFritzBoxConfiguration();
124+
FritzAhaWebInterface webInterface = new FritzAhaWebInterface(config, handler, new HttpClient());
125+
AtomicBoolean firstStatusUpdate = new AtomicBoolean(true);
126+
AtomicReference<CompletableFuture<Boolean>> authenticationDuringStatusUpdate = new AtomicReference<>();
127+
doAnswer(invocation -> {
128+
if (firstStatusUpdate.getAndSet(false)) {
129+
authenticationDuringStatusUpdate.set(webInterface.authenticate());
130+
}
131+
return null;
132+
}).when(handler).setStatusInfo(any(), any(), any());
133+
134+
CompletableFuture<Boolean> authentication = webInterface.authenticate();
135+
136+
assertSame(authentication, authenticationDuringStatusUpdate.get());
137+
webInterface.dispose();
138+
}
139+
116140
private void acceptWithoutResponding(ServerSocket serverSocket, CountDownLatch requestAccepted,
117141
CountDownLatch releaseServer) {
118142
try (Socket ignored = serverSocket.accept()) {
@@ -131,9 +155,9 @@ private void respondWithChallenge(ServerSocket serverSocket) {
131155
BufferedReader reader = new BufferedReader(
132156
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))) {
133157
String line;
134-
while ((line = reader.readLine()) != null && !line.isEmpty()) {
135-
// Read the complete request header before responding.
136-
}
158+
do {
159+
line = reader.readLine();
160+
} while (line != null && !line.isEmpty());
137161
String response = "HTTP/1.1 200 OK\r\nContent-Length: " + content.getBytes(StandardCharsets.UTF_8).length
138162
+ "\r\nConnection: close\r\n\r\n" + content;
139163
socket.getOutputStream().write(response.getBytes(StandardCharsets.UTF_8));

0 commit comments

Comments
 (0)