From e3c7fd0bd4d7f333b1a68c5571db8b08fd7da202 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Thu, 21 Sep 2023 13:15:28 +0200 Subject: [PATCH 1/4] test(binding-coap): `await` all calls of `coapServer.stop()` --- packages/binding-coap/test/coap-server-test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/binding-coap/test/coap-server-test.ts b/packages/binding-coap/test/coap-server-test.ts index 294815152..5dcb8a308 100644 --- a/packages/binding-coap/test/coap-server-test.ts +++ b/packages/binding-coap/test/coap-server-test.ts @@ -280,7 +280,7 @@ class CoapServerTest { const resp = await coapClient.readResource(new TD.Form(uri + "properties/test?id=testId&globalVarTest=test1")); expect((await resp.toBuffer()).toString()).to.equal('"testValue"'); - return coapServer.stop(); + await coapServer.stop(); } @test async "should support /.well-known/core"() { @@ -307,7 +307,7 @@ class CoapServerTest { ';rt="wot.thing";ct="50 432",;rt="wot.thing";ct="50 432"' ); - return coapServer.stop(); + await coapServer.stop(); } @test async "should support TD Content-Format negotiation"() { @@ -344,7 +344,7 @@ class CoapServerTest { req.setHeader("Accept", contentFormat); } - req.on("response", (res: IncomingMessage) => { + req.on("response", async (res: IncomingMessage) => { const requestContentFormat = res.headers["Content-Format"]; if (contentFormat === unsupportedContentFormat) { @@ -357,7 +357,7 @@ class CoapServerTest { } if (++responseCounter >= contentFormats.length) { - coapServer.stop(); + await coapServer.stop(); } }); req.end(); @@ -383,9 +383,9 @@ class CoapServerTest { port: coapServer.getPort(), }); req.setOption("Size2", 0); - req.on("response", (res) => { + req.on("response", async (res) => { expect(res.headers.Size2).to.equal(JSON.stringify(testThing.getThingDescription()).length); - coapServer.stop(); + await coapServer.stop(); }); req.end(); } From 3d5fbad85c0e25e137cafee0ee91c38b8edd6eed Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Thu, 21 Sep 2023 14:18:52 +0200 Subject: [PATCH 2/4] fixup! test(binding-coap): `await` all calls of `coapServer.stop()` --- .../binding-coap/test/coap-server-test.ts | 72 ++++++++++--------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/packages/binding-coap/test/coap-server-test.ts b/packages/binding-coap/test/coap-server-test.ts index 5dcb8a308..135f9702f 100644 --- a/packages/binding-coap/test/coap-server-test.ts +++ b/packages/binding-coap/test/coap-server-test.ts @@ -337,31 +337,35 @@ class CoapServerTest { null, ]; - for (const contentFormat of contentFormats) { - const req = request(uri); + await new Promise((resolve) => { + for (const contentFormat of contentFormats) { + const req = request(uri); - if (contentFormat != null) { - req.setHeader("Accept", contentFormat); - } - - req.on("response", async (res: IncomingMessage) => { - const requestContentFormat = res.headers["Content-Format"]; - - if (contentFormat === unsupportedContentFormat) { - expect(res.code).to.equal("4.06"); - expect(res.payload.toString()).to.equal( - `Content-Format ${unsupportedContentFormat} is not supported by this resource.` - ); - } else { - expect(requestContentFormat).to.equal(contentFormat ?? defaultContentFormat); + if (contentFormat != null) { + req.setHeader("Accept", contentFormat); } - if (++responseCounter >= contentFormats.length) { - await coapServer.stop(); - } - }); - req.end(); - } + req.on("response", async (res: IncomingMessage) => { + const requestContentFormat = res.headers["Content-Format"]; + + if (contentFormat === unsupportedContentFormat) { + expect(res.code).to.equal("4.06"); + expect(res.payload.toString()).to.equal( + `Content-Format ${unsupportedContentFormat} is not supported by this resource.` + ); + } else { + expect(requestContentFormat).to.equal(contentFormat ?? defaultContentFormat); + } + + if (++responseCounter >= contentFormats.length) { + resolve(); + } + }); + req.end(); + } + }); + + await coapServer.stop(); } @test async "should supply Size2 option when fetching a TD"() { @@ -377,17 +381,21 @@ class CoapServerTest { await coapServer.expose(testThing); - const req = request({ - host: "localhost", - pathname: "test", - port: coapServer.getPort(), - }); - req.setOption("Size2", 0); - req.on("response", async (res) => { - expect(res.headers.Size2).to.equal(JSON.stringify(testThing.getThingDescription()).length); - await coapServer.stop(); + await new Promise((resolve) => { + const req = request({ + host: "localhost", + pathname: "test", + port: coapServer.getPort(), + }); + req.setOption("Size2", 0); + req.on("response", (res) => { + expect(res.headers.Size2).to.equal(JSON.stringify(testThing.getThingDescription()).length); + resolve(); + }); + req.end(); }); - req.end(); + + await coapServer.stop(); } @test async "should check uriVariables consistency"() { From 7b464404a2dfcd73d6e06c1c193939731da62bd2 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Thu, 21 Sep 2023 14:27:22 +0200 Subject: [PATCH 3/4] fixup! test(binding-coap): `await` all calls of `coapServer.stop()` --- packages/binding-coap/test/coap-server-test.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/binding-coap/test/coap-server-test.ts b/packages/binding-coap/test/coap-server-test.ts index 135f9702f..fa5b625ec 100644 --- a/packages/binding-coap/test/coap-server-test.ts +++ b/packages/binding-coap/test/coap-server-test.ts @@ -323,7 +323,6 @@ class CoapServerTest { await coapServer.expose(testThing); const uri = `coap://localhost:${coapServer.getPort()}/test`; - let responseCounter = 0; registerFormat("application/foobar", 65000); @@ -337,9 +336,8 @@ class CoapServerTest { null, ]; - await new Promise((resolve) => { - for (const contentFormat of contentFormats) { - const req = request(uri); + const promises = contentFormats.map((contentFormat) => new Promise((resolve) => { + const req = request(uri); if (contentFormat != null) { req.setHeader("Accept", contentFormat); @@ -357,13 +355,12 @@ class CoapServerTest { expect(requestContentFormat).to.equal(contentFormat ?? defaultContentFormat); } - if (++responseCounter >= contentFormats.length) { - resolve(); - } + resolve(); }); req.end(); - } - }); + })); + + await Promise.all(promises); await coapServer.stop(); } From 7a3538a5cde5f44940f186c0c1f548d409edceb7 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Thu, 21 Sep 2023 14:28:15 +0200 Subject: [PATCH 4/4] fixup! test(binding-coap): `await` all calls of `coapServer.stop()` --- .../binding-coap/test/coap-server-test.ts | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/packages/binding-coap/test/coap-server-test.ts b/packages/binding-coap/test/coap-server-test.ts index fa5b625ec..aee6a287f 100644 --- a/packages/binding-coap/test/coap-server-test.ts +++ b/packages/binding-coap/test/coap-server-test.ts @@ -336,29 +336,32 @@ class CoapServerTest { null, ]; - const promises = contentFormats.map((contentFormat) => new Promise((resolve) => { - const req = request(uri); - - if (contentFormat != null) { - req.setHeader("Accept", contentFormat); - } - - req.on("response", async (res: IncomingMessage) => { - const requestContentFormat = res.headers["Content-Format"]; - - if (contentFormat === unsupportedContentFormat) { - expect(res.code).to.equal("4.06"); - expect(res.payload.toString()).to.equal( - `Content-Format ${unsupportedContentFormat} is not supported by this resource.` - ); - } else { - expect(requestContentFormat).to.equal(contentFormat ?? defaultContentFormat); + const promises = contentFormats.map( + (contentFormat) => + new Promise((resolve) => { + const req = request(uri); + + if (contentFormat != null) { + req.setHeader("Accept", contentFormat); } - resolve(); - }); - req.end(); - })); + req.on("response", async (res: IncomingMessage) => { + const requestContentFormat = res.headers["Content-Format"]; + + if (contentFormat === unsupportedContentFormat) { + expect(res.code).to.equal("4.06"); + expect(res.payload.toString()).to.equal( + `Content-Format ${unsupportedContentFormat} is not supported by this resource.` + ); + } else { + expect(requestContentFormat).to.equal(contentFormat ?? defaultContentFormat); + } + + resolve(); + }); + req.end(); + }) + ); await Promise.all(promises);