From bc5076a772027e0eef5f30f285b514da235b71ee Mon Sep 17 00:00:00 2001 From: Manasa Tully Date: Fri, 16 Jun 2023 13:52:12 -0400 Subject: [PATCH 1/3] full ci - broker connection test --- packages/mqtt/package.json | 4 ++-- packages/mqtt/src/MqttSink.ts | 7 +++++++ packages/mqtt/src/__test__/MqttSink.test.ts | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/mqtt/package.json b/packages/mqtt/package.json index 2a01898b..ed35d354 100644 --- a/packages/mqtt/package.json +++ b/packages/mqtt/package.json @@ -1,6 +1,6 @@ { "name": "@walmartlabs/cookie-cutter-mqtt", - "version": "1.6.0-beta.0", + "version": "1.6.0-beta.1", "license": "Apache-2.0", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -27,4 +27,4 @@ "lint": "tslint --project tsconfig.json", "lint:fix": "yarn run lint --fix" } -} +} \ No newline at end of file diff --git a/packages/mqtt/src/MqttSink.ts b/packages/mqtt/src/MqttSink.ts index d9ea2a38..3bccec23 100644 --- a/packages/mqtt/src/MqttSink.ts +++ b/packages/mqtt/src/MqttSink.ts @@ -79,6 +79,13 @@ export class MqttPublisherSink ); } + while (!this.client.connected) { + this.logger.error("Not connected to client", { + hostName: this.config.hostName, + hostPort: this.config.hostPort, + }); + } + this.client.publish( topic, Buffer.from(JSON.stringify(formattedMsg)), diff --git a/packages/mqtt/src/__test__/MqttSink.test.ts b/packages/mqtt/src/__test__/MqttSink.test.ts index cd1937d3..f7a80d66 100644 --- a/packages/mqtt/src/__test__/MqttSink.test.ts +++ b/packages/mqtt/src/__test__/MqttSink.test.ts @@ -35,6 +35,7 @@ jest.mock("mqtt", () => { publish: mockMqttPublisher, removeAllListeners: jest.fn(), end: jest.fn(), + connected: true, }; }), }; From 8ebe38fcb0e04f78f4df7fe061e8a3e212fabf87 Mon Sep 17 00:00:00 2001 From: Manasa Tully Date: Mon, 19 Jun 2023 17:24:20 -0400 Subject: [PATCH 2/3] throw error instaed of looping --- packages/mqtt/src/MqttSink.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/mqtt/src/MqttSink.ts b/packages/mqtt/src/MqttSink.ts index 3bccec23..c82c3400 100644 --- a/packages/mqtt/src/MqttSink.ts +++ b/packages/mqtt/src/MqttSink.ts @@ -79,11 +79,13 @@ export class MqttPublisherSink ); } - while (!this.client.connected) { + if (!this.client.connected) { this.logger.error("Not connected to client", { hostName: this.config.hostName, hostPort: this.config.hostPort, }); + + throw new Error(`Connection could not be established with broker`); } this.client.publish( From 70f2066d8eb187126782b943759350ce35e94edd Mon Sep 17 00:00:00 2001 From: Manasa Tully Date: Tue, 27 Jun 2023 16:20:17 -0400 Subject: [PATCH 3/3] throwing error --- packages/mqtt/src/MqttSink.ts | 11 ++--------- packages/mqtt/src/__test__/MqttSink.test.ts | 6 ++---- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/packages/mqtt/src/MqttSink.ts b/packages/mqtt/src/MqttSink.ts index c82c3400..eacd2185 100644 --- a/packages/mqtt/src/MqttSink.ts +++ b/packages/mqtt/src/MqttSink.ts @@ -79,15 +79,6 @@ export class MqttPublisherSink ); } - if (!this.client.connected) { - this.logger.error("Not connected to client", { - hostName: this.config.hostName, - hostPort: this.config.hostPort, - }); - - throw new Error(`Connection could not be established with broker`); - } - this.client.publish( topic, Buffer.from(JSON.stringify(formattedMsg)), @@ -114,6 +105,8 @@ export class MqttPublisherSink MqttMetricResults.error ); failSpan(span, error); + + throw error; } else { this.emitMetrics( topic, diff --git a/packages/mqtt/src/__test__/MqttSink.test.ts b/packages/mqtt/src/__test__/MqttSink.test.ts index f7a80d66..e06063df 100644 --- a/packages/mqtt/src/__test__/MqttSink.test.ts +++ b/packages/mqtt/src/__test__/MqttSink.test.ts @@ -151,8 +151,7 @@ describe.each([ mockMessageHandlerWithoutTopicMetadata ); - await testApp; - expect(mockMqttPublisher).toBeCalledTimes(testMessages.length); + await expect(testApp).rejects.toThrow(); }); it("Verifies if CONNECT handler WITHOUT ERROR generated works as expected", async () => { @@ -291,8 +290,7 @@ describe.each([ mockMessageHandlerWithTopicMetadata ); - await testApp; - expect(mockMqttPublisher).toBeCalledTimes(testMessages.length); + await expect(testApp).rejects.toThrow(); }); it("Verifies if CONNECT handler WITHOUT ERROR generated works as expected", async () => {