Skip to content

Commit 1afabd3

Browse files
[Service Bus] Fix flaky test - Peek until we get the message (Azure#13346)
Investigated at Azure#13336 to figure out and fix the flakyness. Sometimes, the service returns zero messages for the peek call and is forcing us to retry the peek operation until we get the message so the test is green. (We may have other tests that may fail with the same, we can extend this fix to those tests as needed.)
1 parent c431d53 commit 1afabd3

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

sdk/servicebus/service-bus/test/renewLock.spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = chai.assert;
77
import chaiAsPromised from "chai-as-promised";
88
chai.use(chaiAsPromised);
99
import { delay } from "rhea-promise";
10-
import { TestMessage } from "./utils/testUtils";
10+
import { checkWithTimeout, TestMessage } from "./utils/testUtils";
1111
import {
1212
ServiceBusClientForTests,
1313
createServiceBusClientForTests,
@@ -74,9 +74,21 @@ describe("Message Lock Renewal", () => {
7474
const testMessage = TestMessage.getSample();
7575
await sender.sendMessages(testMessage);
7676

77-
const [peekedMsg] = await receiver.peekMessages(1);
77+
let peekedMsg: ServiceBusReceivedMessage | undefined;
78+
should.equal(
79+
await checkWithTimeout(
80+
async () => {
81+
[peekedMsg] = await receiver.peekMessages(1);
82+
return peekedMsg != undefined;
83+
},
84+
100,
85+
5000
86+
),
87+
true,
88+
"Unable to peek messages in 5000 milliseconds"
89+
);
7890
try {
79-
await receiver.renewMessageLock(peekedMsg);
91+
await receiver.renewMessageLock(peekedMsg!);
8092
assert.fail("renewMessageLock should have failed");
8193
} catch (error) {
8294
should.equal(error.message, InvalidOperationForPeekedMessage);

sdk/servicebus/service-bus/test/utils/testUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ export enum TestClientType {
151151
* Keep checking whether the predicate is true after every `1000 ms`(default value is 1 second) (= delayBetweenRetriesInMilliseconds)
152152
*/
153153
export async function checkWithTimeout(
154-
predicate: () => boolean,
154+
predicate: () => boolean | Promise<boolean>,
155155
delayBetweenRetriesInMilliseconds: number = 1000,
156156
maxWaitTimeInMilliseconds: number = 10000
157157
): Promise<boolean> {
158158
const maxTime = Date.now() + maxWaitTimeInMilliseconds;
159159
while (Date.now() < maxTime) {
160-
if (predicate()) return true;
160+
if (await predicate()) return true;
161161
await delay(delayBetweenRetriesInMilliseconds);
162162
}
163163
return false;

0 commit comments

Comments
 (0)