Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4bb34f

Browse files
[event-hubs] Fixing flaky tests. (Azure#8684)
Fixing two flaky tests: - createBatch() was dumping out a Diagnostic-Id property into its messages. The issue was random tests overwriting the global tracer object with their test tracer. Now all those tests use the same utility function and cleanup after they're done. - There were a few tests that were using the date to choose their start position which ends up being unreliable in the face of clock skew. Changed over to use the same sequenceNumber positioning trick as other tests with a new testutils method.
1 parent acec1aa commit e4bb34f

File tree

4 files changed

+98
-41
lines changed

4 files changed

+98
-41
lines changed

sdk/eventhub/event-hubs/test/hubruntime.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import chaiAsPromised from "chai-as-promised";
77
chai.use(chaiAsPromised);
88
import debugModule from "debug";
99
const debug = debugModule("azure:event-hubs:hubruntime-spec");
10-
import { EnvVarKeys, getEnvVars } from "./utils/testUtils";
10+
import { EnvVarKeys, getEnvVars, setTracerForTest } from "./utils/testUtils";
1111
const env = getEnvVars();
1212

1313
import { EventHubClient } from "../src/impl/eventHubClient";
1414
import { AbortController } from "@azure/abort-controller";
15-
import { TestTracer, setTracer, SpanGraph } from "@azure/core-tracing";
15+
import { SpanGraph } from "@azure/core-tracing";
1616
describe("RuntimeInformation", function(): void {
1717
let client: EventHubClient;
1818
const service = {
@@ -76,8 +76,7 @@ describe("RuntimeInformation", function(): void {
7676
});
7777

7878
it("can be manually traced", async function(): Promise<void> {
79-
const tracer = new TestTracer();
80-
setTracer(tracer);
79+
const { tracer, resetTracer } = setTracerForTest();
8180

8281
const rootSpan = tracer.startSpan("root");
8382
client = new EventHubClient(service.connectionString, service.path);
@@ -116,6 +115,7 @@ describe("RuntimeInformation", function(): void {
116115

117116
tracer.getSpanGraph(rootSpan.context().traceId).should.eql(expectedGraph);
118117
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
118+
resetTracer();
119119
});
120120

121121
describe("hub runtime information", function(): void {
@@ -150,8 +150,7 @@ describe("RuntimeInformation", function(): void {
150150
});
151151

152152
it("can be manually traced", async function(): Promise<void> {
153-
const tracer = new TestTracer();
154-
setTracer(tracer);
153+
const { tracer, resetTracer } = setTracerForTest();
155154

156155
const rootSpan = tracer.startSpan("root");
157156
client = new EventHubClient(service.connectionString, service.path);
@@ -187,6 +186,7 @@ describe("RuntimeInformation", function(): void {
187186

188187
tracer.getSpanGraph(rootSpan.context().traceId).should.eql(expectedGraph);
189188
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
189+
resetTracer();
190190
});
191191
});
192192

@@ -262,8 +262,7 @@ describe("RuntimeInformation", function(): void {
262262
});
263263

264264
it("can be manually traced", async function(): Promise<void> {
265-
const tracer = new TestTracer();
266-
setTracer(tracer);
265+
const { tracer, resetTracer } = setTracerForTest();
267266

268267
const rootSpan = tracer.startSpan("root");
269268
client = new EventHubClient(service.connectionString, service.path);
@@ -301,6 +300,7 @@ describe("RuntimeInformation", function(): void {
301300

302301
tracer.getSpanGraph(rootSpan.context().traceId).should.eql(expectedGraph);
303302
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
303+
resetTracer();
304304
});
305305
});
306306
}).timeout(60000);

sdk/eventhub/event-hubs/test/partitionPump.spec.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// Licensed under the MIT license.
33

44
import { createProcessingSpan, trace } from "../src/partitionPump";
5-
import { TestTracer, setTracer, TestSpan, NoOpSpan } from "@azure/core-tracing";
5+
import { TestTracer, TestSpan, NoOpSpan } from "@azure/core-tracing";
66
import { CanonicalCode, SpanOptions, SpanKind } from "@opentelemetry/api";
77
import chai from "chai";
88
import { ReceivedEventData } from "../src/eventData";
99
import { instrumentEventData } from "../src/diagnostics/instrumentEventData";
10+
import { setTracerForTest } from "./utils/testUtils";
1011

1112
const should = chai.should();
1213

@@ -34,8 +35,7 @@ describe("PartitionPump", () => {
3435

3536
it("basic span properties are set", async () => {
3637
const fakeParentSpanContext = new NoOpSpan().context();
37-
const tracer = new TestTracer2();
38-
setTracer(tracer);
38+
const { tracer, resetTracer } = setTracerForTest(new TestTracer2());
3939

4040
await createProcessingSpan([], eventHubProperties, {
4141
tracingOptions: {
@@ -58,6 +58,8 @@ describe("PartitionPump", () => {
5858
"message_bus.destination": "theeventhubname",
5959
"peer.address": "theendpoint"
6060
});
61+
62+
resetTracer();
6163
});
6264

6365
it("received events are linked to this span using Diagnostic-Id", async () => {
@@ -69,8 +71,7 @@ describe("PartitionPump", () => {
6971
sequenceNumber: 0
7072
};
7173

72-
const tracer = new TestTracer2();
73-
setTracer(tracer);
74+
const { tracer, resetTracer } = setTracerForTest(new TestTracer2());
7475

7576
const firstEvent = tracer.startSpan("a");
7677
const thirdEvent = tracer.startSpan("c");
@@ -90,6 +91,8 @@ describe("PartitionPump", () => {
9091
// incremented
9192
tracer.spanOptions!.links![0]!.context.traceId.should.equal(firstEvent.context().traceId);
9293
tracer.spanOptions!.links![1]!.context.traceId.should.equal(thirdEvent.context().traceId);
94+
95+
resetTracer();
9396
});
9497

9598
it("trace - normal", async () => {

sdk/eventhub/event-hubs/test/sender.spec.ts

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ import {
1111
EventData,
1212
EventHubProducerClient,
1313
EventHubConsumerClient,
14-
ReceivedEventData
14+
ReceivedEventData,
15+
EventPosition
1516
} from "../src";
1617
import { EventHubClient } from "../src/impl/eventHubClient";
1718
import { SendOptions, SendBatchOptions } from "../src/models/public";
18-
import { EnvVarKeys, getEnvVars } from "./utils/testUtils";
19+
import {
20+
EnvVarKeys,
21+
getEnvVars,
22+
getStartingPositionsForTests,
23+
setTracerForTest
24+
} from "./utils/testUtils";
1925
import { AbortController } from "@azure/abort-controller";
20-
import { TestTracer, setTracer, SpanGraph } from "@azure/core-tracing";
26+
import { SpanGraph } from "@azure/core-tracing";
2127
import { TRACEPARENT_PROPERTY } from "../src/diagnostics/instrumentEventData";
2228
import { EventHubProducer } from "../src/sender";
2329
import { SubscriptionHandlerForTests } from "./utils/subscriptionHandlerForTests";
@@ -118,8 +124,7 @@ describe("EventHub Sender", function(): void {
118124
});
119125

120126
it("can be manually traced", async function(): Promise<void> {
121-
const tracer = new TestTracer();
122-
setTracer(tracer);
127+
const { tracer, resetTracer } = setTracerForTest();
123128

124129
const rootSpan = tracer.startSpan("root");
125130

@@ -164,6 +169,7 @@ describe("EventHub Sender", function(): void {
164169
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
165170

166171
await producer.close();
172+
resetTracer();
167173
});
168174
});
169175

@@ -338,9 +344,9 @@ describe("EventHub Sender", function(): void {
338344
it("should be sent successfully with properties", async function(): Promise<void> {
339345
const properties = { test: "super" };
340346
const list = [
341-
{ body: "Albert", properties },
342-
{ body: "Mike", properties },
343-
{ body: "Marie", properties }
347+
{ body: "Albert-With-Properties", properties },
348+
{ body: "Mike-With-Properties", properties },
349+
{ body: "Marie-With-Properties", properties }
344350
];
345351

346352
const batch = await producerClient.createBatch({
@@ -356,6 +362,10 @@ describe("EventHub Sender", function(): void {
356362
const receivedEvents: ReceivedEventData[] = [];
357363
let waitUntilEventsReceivedResolver: Function;
358364
const waitUntilEventsReceived = new Promise((r) => (waitUntilEventsReceivedResolver = r));
365+
366+
const sequenceNumber = (await consumerClient.getPartitionProperties("0"))
367+
.lastEnqueuedSequenceNumber;
368+
359369
const subscriber = consumerClient.subscribe(
360370
"0",
361371
{
@@ -369,8 +379,7 @@ describe("EventHub Sender", function(): void {
369379
},
370380
{
371381
startPosition: {
372-
sequenceNumber: (await consumerClient.getPartitionProperties("0"))
373-
.lastEnqueuedSequenceNumber
382+
sequenceNumber
374383
},
375384
maxBatchSize: 3
376385
}
@@ -380,6 +389,10 @@ describe("EventHub Sender", function(): void {
380389
await waitUntilEventsReceived;
381390
await subscriber.close();
382391

392+
sequenceNumber.should.be.lessThan(receivedEvents[0].sequenceNumber);
393+
sequenceNumber.should.be.lessThan(receivedEvents[1].sequenceNumber);
394+
sequenceNumber.should.be.lessThan(receivedEvents[2].sequenceNumber);
395+
383396
[list[0], list[1], list[2]].should.be.deep.eq(
384397
receivedEvents.map((event) => {
385398
return {
@@ -392,8 +405,7 @@ describe("EventHub Sender", function(): void {
392405
});
393406

394407
it("can be manually traced", async function(): Promise<void> {
395-
const tracer = new TestTracer();
396-
setTracer(tracer);
408+
const { tracer, resetTracer } = setTracerForTest();
397409

398410
const rootSpan = tracer.startSpan("root");
399411

@@ -433,11 +445,11 @@ describe("EventHub Sender", function(): void {
433445

434446
tracer.getSpanGraph(rootSpan.context().traceId).should.eql(expectedGraph);
435447
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
448+
resetTracer();
436449
});
437450

438451
it("will not instrument already instrumented events", async function(): Promise<void> {
439-
const tracer = new TestTracer();
440-
setTracer(tracer);
452+
const { tracer, resetTracer } = setTracerForTest();
441453

442454
const rootSpan = tracer.startSpan("test");
443455

@@ -484,11 +496,11 @@ describe("EventHub Sender", function(): void {
484496

485497
tracer.getSpanGraph(rootSpan.context().traceId).should.eql(expectedGraph);
486498
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
499+
resetTracer();
487500
});
488501

489502
it("will support tracing batch and send", async function(): Promise<void> {
490-
const tracer = new TestTracer();
491-
setTracer(tracer);
503+
const { tracer, resetTracer } = setTracerForTest();
492504

493505
const rootSpan = tracer.startSpan("root");
494506

@@ -537,6 +549,7 @@ describe("EventHub Sender", function(): void {
537549

538550
tracer.getSpanGraph(rootSpan.context().traceId).should.eql(expectedGraph);
539551
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
552+
resetTracer();
540553
});
541554

542555
it("with partition key should be sent successfully.", async function(): Promise<void> {
@@ -777,8 +790,7 @@ describe("EventHub Sender", function(): void {
777790
});
778791

779792
it("can be manually traced", async function(): Promise<void> {
780-
const tracer = new TestTracer();
781-
setTracer(tracer);
793+
const { tracer, resetTracer } = setTracerForTest();
782794

783795
const rootSpan = tracer.startSpan("root");
784796

@@ -839,11 +851,11 @@ describe("EventHub Sender", function(): void {
839851
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
840852

841853
await producer.close();
854+
resetTracer();
842855
});
843856

844857
it("skips already instrumented events when manually traced", async function(): Promise<void> {
845-
const tracer = new TestTracer();
846-
setTracer(tracer);
858+
const { tracer, resetTracer } = setTracerForTest();
847859

848860
const rootSpan = tracer.startSpan("root");
849861

@@ -901,17 +913,22 @@ describe("EventHub Sender", function(): void {
901913
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
902914

903915
await producer.close();
916+
resetTracer();
904917
});
905918
});
906919

907920
describe("Array of events", function() {
908921
let consumerClient: EventHubConsumerClient;
909-
beforeEach(() => {
922+
let startPosition: { [partitionId: string]: EventPosition };
923+
924+
beforeEach(async () => {
910925
consumerClient = new EventHubConsumerClient(
911926
EventHubConsumerClient.defaultConsumerGroupName,
912927
service.connectionString,
913928
service.path
914929
);
930+
931+
startPosition = await getStartingPositionsForTests(consumerClient);
915932
});
916933

917934
afterEach(() => {
@@ -922,6 +939,7 @@ describe("EventHub Sender", function(): void {
922939
const data: EventData[] = [{ body: "Hello World 1" }, { body: "Hello World 2" }];
923940
const receivedEvents: ReceivedEventData[] = [];
924941
let receivingResolver: Function;
942+
925943
const receivingPromise = new Promise((r) => (receivingResolver = r));
926944
const subscription = consumerClient.subscribe(
927945
{
@@ -932,7 +950,7 @@ describe("EventHub Sender", function(): void {
932950
}
933951
},
934952
{
935-
startPosition: { enqueuedOn: new Date(), isInclusive: true },
953+
startPosition,
936954
maxBatchSize: data.length
937955
}
938956
);
@@ -960,7 +978,7 @@ describe("EventHub Sender", function(): void {
960978
}
961979
},
962980
{
963-
startPosition: { enqueuedOn: new Date(), isInclusive: true },
981+
startPosition,
964982
maxBatchSize: data.length
965983
}
966984
);
@@ -993,7 +1011,7 @@ describe("EventHub Sender", function(): void {
9931011
}
9941012
},
9951013
{
996-
startPosition: { enqueuedOn: new Date(), isInclusive: true },
1014+
startPosition,
9971015
maxBatchSize: data.length
9981016
}
9991017
);
@@ -1011,8 +1029,7 @@ describe("EventHub Sender", function(): void {
10111029
});
10121030

10131031
it("can be manually traced", async function(): Promise<void> {
1014-
const tracer = new TestTracer();
1015-
setTracer(tracer);
1032+
const { tracer, resetTracer } = setTracerForTest();
10161033

10171034
const rootSpan = tracer.startSpan("root");
10181035

@@ -1069,11 +1086,11 @@ describe("EventHub Sender", function(): void {
10691086

10701087
tracer.getSpanGraph(rootSpan.context().traceId).should.eql(expectedGraph);
10711088
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
1089+
resetTracer();
10721090
});
10731091

10741092
it("skips already instrumented events when manually traced", async function(): Promise<void> {
1075-
const tracer = new TestTracer();
1076-
setTracer(tracer);
1093+
const { tracer, resetTracer } = setTracerForTest();
10771094

10781095
const rootSpan = tracer.startSpan("root");
10791096

@@ -1127,6 +1144,7 @@ describe("EventHub Sender", function(): void {
11271144

11281145
tracer.getSpanGraph(rootSpan.context().traceId).should.eql(expectedGraph);
11291146
tracer.getActiveSpans().length.should.equal(0, "All spans should have had end called.");
1147+
resetTracer();
11301148
});
11311149
});
11321150

0 commit comments

Comments
 (0)