Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

test(iot): retry the Paho WebSocket handshake race in concurrent tests (#5286)

TATSUNO “Taz” Yasuhiro362d89f74f28
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketIntegrationTest.java
index e262f86247..670b904724 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketIntegrationTest.java
@@ -442,7 +442,7 @@ public class IotMqttWebSocketIntegrationTest {
options.setUserName(username);
options.setPassword(password.toCharArray());
}
- client.connect(options);
+ PahoWebSocketConnect.connect(client, options);
return wsClient;
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketLazyStartIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketLazyStartIntegrationTest.java
index 80c25d4ee4..da0f22da26 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketLazyStartIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotMqttWebSocketLazyStartIntegrationTest.java
@@ -8,6 +8,7 @@ import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.junit.jupiter.api.BeforeEach;
@@ -79,7 +80,7 @@ class IotMqttWebSocketLazyStartIntegrationTest {
Thread thread = new Thread(() -> {
try {
MqttClient client = new MqttClient(ws(), clientId, new MemoryPersistence());
- client.connect();
+ PahoWebSocketConnect.connect(client, new MqttConnectOptions());
connected.add(client);
} catch (Exception e) {
failures.add(e);
diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/PahoWebSocketConnect.java b/src/test/java/io/github/hectorvent/floci/services/iot/PahoWebSocketConnect.java
new file mode 100644
index 0000000000..e841f3feb8
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/iot/PahoWebSocketConnect.java
@@ -0,0 +1,47 @@
+package io.github.hectorvent.floci.services.iot;
+
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttException;
+
+import java.io.IOException;
+
+/**
+ * Connects a Paho client over WebSocket, retrying only the handshake failure Paho causes itself.
+ * Paho encodes the Sec-WebSocket-Key, and checks the server's Sec-WebSocket-Accept, with one
+ * Base64 encoder that every client shares unlocked. Clients that connect at the same time can read
+ * each other's value and fail with "Incorrect Sec-WebSocket-Key" before the server is involved.
+ * With the default MQTT version, Paho answers that failure by retrying with an older version,
+ * which the broker closes, so the race would show up as "Connection lost" instead.
+ */
+final class PahoWebSocketConnect {
+
+ private static final String HANDSHAKE_RACE = "WebSocket Response header: Incorrect Sec-WebSocket-Key";
+ private static final int ATTEMPTS = 5;
+
+ private PahoWebSocketConnect() {
+ }
+
+ static void connect(MqttClient client, MqttConnectOptions options) throws MqttException {
+ options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
+ for (int attempt = 1; ; attempt++) {
+ try {
+ client.connect(options);
+ return;
+ } catch (MqttException e) {
+ if (attempt == ATTEMPTS || !isHandshakeRace(e)) {
+ throw e;
+ }
+ }
+ }
+ }
+
+ private static boolean isHandshakeRace(Throwable error) {
+ for (Throwable cause = error; cause != null; cause = cause.getCause()) {
+ if (cause instanceof IOException && HANDSHAKE_RACE.equals(cause.getMessage())) {
+ return true;
+ }
+ }
+ return false;
+ }
+}