Skip to content

Commit 1f237f2

Browse files
authored
[ServiceBus][Test] fix unstable tests that verify message state (Azure#19357)
* [ServiceBus][Test] fix unstable tests that verify message state The test messages we create have `scheduledEnqueueTimeUtc` set. Most of the time messages received from the service have "active" state, but occasionally have "scheduled" state due to the set property, thus failing the assertion. This PR updates the check to check for either "active" or "schedule". In addition, a test is added for "active" state where the `scheduledEnqueueTimeUtc` property is removed before sending the message. Also shown in the failing pipeline that when the test failed, the message wasn't removed because `receiver.completeMessage()` is not called. The lingering message caused the next test to fail. While updating the tests I also moved the cleanup step before assertions so the message should always be removed. * Undo changes to pipeline * Address CR feedback * Disable linting rule * Remove an unstable assertion on message state.
1 parent 90807d1 commit 1f237f2

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

sdk/servicebus/service-bus/test/public/sendAndSchedule.spec.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ describe("Sender Tests", () => {
6161
: TestMessage.getSample();
6262
await sender.sendMessages(testMessage);
6363
const msgs = await receiver.receiveMessages(1);
64-
64+
const receivedMessage = msgs[0];
65+
// remove message first in case any assertion fails to ensure we don't have lingering message
66+
await receiver.completeMessage(receivedMessage);
6567
should.equal(Array.isArray(msgs), true, "`ReceivedMessages` is not an array");
6668
should.equal(msgs.length, 1, "Unexpected number of messages");
67-
should.equal(msgs[0].deliveryCount, 0, "DeliveryCount is different than expected");
68-
should.equal(msgs[0].state, "active");
69+
should.equal(receivedMessage.deliveryCount, 0, "DeliveryCount is different than expected");
70+
should.equal(receivedMessage.messageId, testMessage.messageId);
6971

7072
TestMessage.checkMessageContents(
7173
testMessage,
72-
msgs[0],
74+
receivedMessage,
7375
entityName.usesSessions,
7476
entityName.isPartitioned
7577
);
7678

77-
await receiver.completeMessage(msgs[0]);
78-
7979
await testPeekMsgsLength(receiver, 0);
8080
}
8181

@@ -89,6 +89,22 @@ describe("Sender Tests", () => {
8989
await testSimpleSend();
9090
});
9191

92+
it(withSessionTestClientType + ": Received message has active state", async function(): Promise<
93+
void
94+
> {
95+
await beforeEachTest(withSessionTestClientType);
96+
const testMessage = entityName.usesSessions
97+
? TestMessage.getSessionSample()
98+
: TestMessage.getSample();
99+
// Ensure the message is not scheduled.
100+
delete testMessage.scheduledEnqueueTimeUtc;
101+
await sender.sendMessages(testMessage);
102+
const msgs = await receiver.receiveMessages(1);
103+
// remove message first in case any assertion fails to ensure we don't have lingering message
104+
await receiver.completeMessage(msgs[0]);
105+
msgs[0].state.should.equal("active");
106+
});
107+
92108
async function testSimpleSendArray(): Promise<void> {
93109
const testMessages = [];
94110
testMessages.push(
@@ -101,9 +117,26 @@ describe("Sender Tests", () => {
101117
await sender.sendMessages(testMessages);
102118
const msgs = await receiver.receiveMessages(2);
103119

120+
// remove messages first in case any assertion fails to ensure we don't have lingering messages
121+
await receiver.completeMessage(msgs[0]);
122+
await receiver.completeMessage(msgs[1]);
123+
104124
should.equal(Array.isArray(msgs), true, "`ReceivedMessages` is not an array");
105125
should.equal(msgs.length, 2, "Unexpected number of messages");
106126

127+
should.equal(
128+
msgs[0].messageId === testMessages[0].messageId ||
129+
msgs[0].messageId === testMessages[1].messageId,
130+
true,
131+
`Unexpected message with id ${msgs[0].messageId}`
132+
);
133+
should.equal(
134+
msgs[1].messageId === testMessages[0].messageId ||
135+
msgs[1].messageId === testMessages[1].messageId,
136+
true,
137+
`Unexpected message with id ${msgs[1].messageId}`
138+
);
139+
107140
if (testMessages[0].messageId === msgs[0].messageId) {
108141
TestMessage.checkMessageContents(
109142
testMessages[0],
@@ -132,9 +165,6 @@ describe("Sender Tests", () => {
132165
);
133166
}
134167

135-
await receiver.completeMessage(msgs[0]);
136-
await receiver.completeMessage(msgs[1]);
137-
138168
await testPeekMsgsLength(receiver, 0);
139169
}
140170

0 commit comments

Comments
 (0)