-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathreceiveBatch.spec.ts
173 lines (152 loc) · 5.92 KB
/
receiveBatch.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { EnvVarKeys, getEnvVars } from "../public/utils/testUtils";
import {
EventData,
EventHubConsumerClient,
EventHubProducerClient,
EventPosition,
MessagingError,
} from "../../src";
import { EventHubReceiver } from "../../src/eventHubReceiver";
import chai from "chai";
import chaiAsPromised from "chai-as-promised";
import { createMockServer } from "../public/utils/mockService";
import { testWithServiceTypes } from "../public/utils/testWithServiceTypes";
import { translate } from "@azure/core-amqp";
const should = chai.should();
chai.use(chaiAsPromised);
testWithServiceTypes((serviceVersion) => {
const env = getEnvVars();
if (serviceVersion === "mock") {
let service: ReturnType<typeof createMockServer>;
before("Starting mock service", () => {
service = createMockServer();
return service.start();
});
after("Stopping mock service", () => {
return service?.stop();
});
}
describe("EventHubConsumerClient", function (): void {
const service = {
connectionString: env[EnvVarKeys.EVENTHUB_CONNECTION_STRING],
path: env[EnvVarKeys.EVENTHUB_NAME],
};
let producerClient: EventHubProducerClient;
let consumerClient: EventHubConsumerClient;
let partitionIds: string[];
before("validate environment", async function (): Promise<void> {
should.exist(
env[EnvVarKeys.EVENTHUB_CONNECTION_STRING],
"define EVENTHUB_CONNECTION_STRING in your environment before running integration tests."
);
should.exist(
env[EnvVarKeys.EVENTHUB_NAME],
"define EVENTHUB_NAME in your environment before running integration tests."
);
});
beforeEach("Creating the clients", async () => {
producerClient = new EventHubProducerClient(service.connectionString, service.path);
consumerClient = new EventHubConsumerClient(
EventHubConsumerClient.defaultConsumerGroupName,
service.connectionString,
service.path
);
partitionIds = await producerClient.getPartitionIds({});
});
afterEach("Closing the clients", async () => {
await producerClient.close();
await consumerClient.close();
});
describe("EventHubConsumer receiveBatch", function (): void {
it("should not lose messages on error", async () => {
const partitionId = partitionIds[0];
const { lastEnqueuedSequenceNumber } = await producerClient.getPartitionProperties(
partitionId
);
// Ensure the receiver only looks at new messages.
const startPosition: EventPosition = {
sequenceNumber: lastEnqueuedSequenceNumber,
isInclusive: false,
};
// Send a message we expect to receive.
const message: EventData = { body: "remember me!" };
await producerClient.sendBatch([message], { partitionId });
// Disable retries to make it easier to test scenario.
const receiver = new EventHubReceiver(
consumerClient["_context"],
EventHubConsumerClient.defaultConsumerGroupName,
partitionId,
startPosition,
{
retryOptions: {
maxRetries: 0,
},
}
);
// Periodically check that the receiver's checkpoint has been updated.
const checkpointInterval = setInterval(() => {
if (receiver.checkpoint > -1) {
clearInterval(checkpointInterval);
const error = translate(new Error("I break receivers for fun."));
receiver["_onError"]!(error);
}
}, 10);
try {
// There is only 1 message.
// We expect to see an error.
await receiver.receiveBatch(2, 60);
throw new Error(`Test failure`);
} catch (err: any) {
err.message.should.not.equal("Test failure");
receiver.checkpoint.should.be.greaterThan(-1, "Did not see a message come through.");
} finally {
clearInterval(checkpointInterval);
}
const events = await receiver.receiveBatch(1);
events.length.should.equal(1, "Unexpected number of events received.");
events[0].body.should.equal(message.body, "Unexpected message received.");
});
it("should not lose messages between retries", async () => {
const partitionId = partitionIds[0];
const { lastEnqueuedSequenceNumber } = await producerClient.getPartitionProperties(
partitionId
);
// Ensure the receiver only looks at new messages.
const startPosition: EventPosition = {
sequenceNumber: lastEnqueuedSequenceNumber,
isInclusive: false,
};
// Send a message we expect to receive.
const message: EventData = { body: "remember me!" };
await producerClient.sendBatch([message], { partitionId });
// Disable retries to make it easier to test scenario.
const receiver = new EventHubReceiver(
consumerClient["_context"],
EventHubConsumerClient.defaultConsumerGroupName,
partitionId,
startPosition,
{
retryOptions: {
maxRetries: 1,
},
}
);
// Periodically check that the receiver's checkpoint has been updated.
const checkpointInterval = setInterval(() => {
if (receiver.checkpoint > -1) {
clearInterval(checkpointInterval);
const error = translate(new Error("I break receivers for fun.")) as MessagingError;
error.retryable = true;
receiver["_onError"]!(error);
}
}, 10);
// There is only 1 message.
const events = await receiver.receiveBatch(2, 20);
events.length.should.equal(1, "Unexpected number of events received.");
events[0].body.should.equal(message.body, "Unexpected message received.");
});
});
}).timeout(90000);
});