forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[event-hubs] fixes sendBatch race condition causing TypeError to be t…
…hrown (Azure#15021) * [event-hubs] fixes sendBatch race condition causing TypeError to be thrown * [event-hubs] add changelog entry for 14606 bug fix * [event-hubs] rename EventHubSender _createLinkIfNotOpen -> _getLink * [event-hubs] cleanup EventHubSender _init if/else statements * [event-hubs] centralize setting of this._sender link
- Loading branch information
Showing
5 changed files
with
102 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
sdk/eventhub/event-hubs/test/internal/node/disconnect.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import chai from "chai"; | ||
const should = chai.should(); | ||
import chaiAsPromised from "chai-as-promised"; | ||
chai.use(chaiAsPromised); | ||
import { EnvVarKeys, getEnvVars } from "../../public/utils/testUtils"; | ||
import { EventHubSender } from "../../../src/eventHubSender"; | ||
import { createConnectionContext } from "../../../src/connectionContext"; | ||
import { stub } from "sinon"; | ||
import { MessagingError } from "@azure/core-amqp"; | ||
const env = getEnvVars(); | ||
|
||
describe("disconnected", function() { | ||
const service = { | ||
connectionString: env[EnvVarKeys.EVENTHUB_CONNECTION_STRING], | ||
path: env[EnvVarKeys.EVENTHUB_NAME] | ||
}; | ||
before("validate environment", function(): 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." | ||
); | ||
}); | ||
|
||
describe("EventHubSender", function() { | ||
/** | ||
* Test added for issue https://github.com/Azure/azure-sdk-for-js/issues/15002 | ||
* Prior to fixing this issue, a TypeError would be thrown when this test was ran. | ||
*/ | ||
it("send works after disconnect", async () => { | ||
const context = createConnectionContext(service.connectionString, service.path); | ||
const sender = EventHubSender.create(context); | ||
|
||
// Create the sender link via getMaxMessageSize() so we can check when 'send' is about to be called on it. | ||
await sender.getMaxMessageSize(); | ||
should.equal(sender.isOpen(), true, "Expected sender to be open."); | ||
|
||
// Here we stub out the 'send' call on the AwaitableSender. | ||
// We do 2 things: | ||
// 1. Call `idle()` on the underlying rhea connection so that a disconnect is triggered. | ||
// 2. Reject with a MessagingError. | ||
// The MessagingError is thrown so that the send operation will be retried. | ||
// The disconnect that's triggered will cause the existing AwaitableSender to be closed. | ||
|
||
// If everything works as expected, then a new AwaitableSender should be created on the next | ||
// retry attempt and the event should be successfully sent. | ||
const senderLink = sender["_sender"]!; | ||
const sendStub = stub(senderLink, "send"); | ||
sendStub.callsFake(async () => { | ||
context.connection["_connection"].idle(); | ||
throw new MessagingError("Fake rejection!"); | ||
}); | ||
|
||
await sender.send([{ body: "foo" }]); | ||
|
||
await context.close(); | ||
}); | ||
}); | ||
}); |