mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
test(iot): retry the Paho WebSocket handshake race in concurrent tests (#5286)
* test(iot): retry the Paho WebSocket handshake race in concurrent tests Paho 1.2.5 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. The concurrent WebSocket tests now retry that failure only. * test(iot): set MQTT 3.1.1 inside the Paho WebSocket connect helper With the default version, Paho answers a failed handshake by reconnecting as MQTT 3.1, which the broker closes, so the race shows up as "Connection lost" and is not retried. That is the EOFException of #3763. The helper now sets 3.1.1 itself, so the race always surfaces as the handshake error it retries and callers cannot forget the pin. * test(iot): drop the unit test of the Paho WebSocket connect helper
This commit is contained in:
1 parent
ecdecf2ae2
commit
362d89f74f
3 files changed
+50
-2
No files matched your search
+1
-1
@@ -442,7 +442,7 @@ public class IotMqttWebSocketIntegrationTest {
|
||||
options.setUserName(username);
|
||||
options.setPassword(password.toCharArray());
|
||||
}
|
||||
client.connect(options);
|
||||
PahoWebSocketConnect.connect(client, options);
|
||||
return wsClient;
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user