Skip to content

Commit aa5caa9

Browse files
bretambroseBret Ambrose
andauthored
Attempt to reduce some test flakiness (#594)
* Update several MQTT will tests to be more forgiving of potential service race conditions * Remove shared subscription distribution assertion because it's not valid Co-authored-by: Bret Ambrose <[email protected]>
1 parent 2eee416 commit aa5caa9

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

lib/common/mqtt.spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,26 @@ test_env.conditional_test(test_env.AWS_IOT_ENV.mqtt311_is_valid_iot_cred())('MQT
129129
const onMessage = once(connectionWaitingForWill, 'message');
130130
await connectionWaitingForWill.subscribe(willTopic, QoS.AtLeastOnce);
131131

132+
// pause for a couple of seconds to try and minimize chance for a service-side race
133+
await new Promise(resolve => setTimeout(resolve, 2000));
134+
132135
// The third connection that will cause the first one to be disconnected because it has the same client ID.
133136
const connectionDuplicate = await makeConnection(undefined, client_id);
134-
const onConnectDuplicate = once(connectionDuplicate, 'connect');
137+
135138
const onDisconnectDuplicate = once(connectionDuplicate, 'disconnect');
136-
await connectionDuplicate.connect()
137-
const connectDuplicateResult = (await onConnectDuplicate)[0];
138-
expect(connectDuplicateResult).toBeFalsy(); /* session present */
139+
140+
// Rarely, IoT Core disconnects the new connection and not the existing one, so retry in that case
141+
let continueConnecting = true;
142+
while (continueConnecting) {
143+
try {
144+
const onConnectDuplicate = once(connectionDuplicate, 'connect');
145+
await connectionDuplicate.connect();
146+
await onConnectDuplicate;
147+
continueConnecting = false;
148+
} catch (err) {
149+
await new Promise(resolve => setTimeout(resolve, 1000));
150+
}
151+
}
139152

140153
// The second connection should receive Will message after the first connection was kicked out.
141154
const messageReceivedArgs = (await onMessage);

lib/native/mqtt5.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ test_utils.conditional_test(test_utils.ClientEnvironmentalConfig.hasIotCoreEnvir
635635
payload: testPayload
636636
});
637637

638-
await setTimeout(()=>{}, 2000);
638+
await new Promise(resolve => setTimeout(resolve, 2000));
639639

640640
statistics = client.getOperationalStatistics();
641641
expect(statistics.incompleteOperationCount).toBeLessThanOrEqual(0);

test/mqtt5.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ export async function subPubUnsubTest(client: mqtt5.Mqtt5Client, qos: mqtt5.QoS,
402402
payload: testPayload
403403
});
404404

405-
await setTimeout(()=>{}, 2000);
405+
await new Promise(resolve => setTimeout(resolve, 2000));
406406

407407
client.stop();
408408
await stopped;
@@ -434,6 +434,9 @@ export async function willTest(publisher: mqtt5.Mqtt5Client, subscriber: mqtt5.M
434434
throw new CrtError("doh");
435435
}
436436

437+
// pause to minimize eventual consistency race condition possibility
438+
await new Promise(resolve => setTimeout(resolve, 1000));
439+
437440
publisher.stop({
438441
reasonCode: mqtt5.DisconnectReasonCode.DisconnectWithWillMessage
439442
});
@@ -659,8 +662,6 @@ export async function doSharedSubscriptionsTest(publisher: mqtt5.Mqtt5Client, su
659662
let messagesReceived : number = 0;
660663
subscriberMessages.forEach(v => {
661664
messagesReceived += v;
662-
// Each subscriber should receive a portion of messages.
663-
expect(v).toBeGreaterThan(0);
664665
});
665666
expect(messagesReceived).toEqual(messagesNumber);
666667

0 commit comments

Comments
 (0)