Skip to content

Commit

Permalink
Improve test timing (#923)
Browse files Browse the repository at this point in the history
* test(binding-http): improve test time avoiding sleeps as much as possible

Signed-off-by: reluc <[email protected]>

* test(binding-modbus): reduce timeouts when testing

* test(binding-mqtt): remove intervals

Signed-off-by: reluc <[email protected]>

* style: remove unused code

---------

Signed-off-by: reluc <[email protected]>
  • Loading branch information
relu91 authored Feb 17, 2023
1 parent c59ea4b commit 7678d8a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 35 deletions.
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/binding-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"./dist/http-client.js": "./dist/http-client-browser.js"
},
"devDependencies": {
"@sinonjs/fake-timers": "^10.0.2",
"@testdeck/mocha": "^0.1.2",
"@types/accept-language-parser": "^1.5.2",
"@types/basic-auth": "1.1.3",
Expand Down
6 changes: 0 additions & 6 deletions packages/binding-http/test/http-client-oauth-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import { readFileSync } from "fs";
import OAuthServer from "express-oauth-server";
import bodyParser from "body-parser";

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

@suite("HTTP oauth client implementation")
class HttpClientOAuthTest {
private client: HttpClient;
Expand Down Expand Up @@ -124,7 +120,6 @@ class HttpClientOAuthTest {
const model = HttpClientOAuthTest.model;
await model.expireAllTokens();
this.client.setSecurity([scheme], { clientId: "thom", clientSecret: "nightworld" });
await sleep(1000);
const resource = await this.client.readResource({
href: "https://127.0.0.1:3000/resource",
});
Expand All @@ -148,7 +143,6 @@ class HttpClientOAuthTest {
username: "thomseddon",
password: "nightworld",
});
await sleep(1000);
const resource = await this.client.readResource({
href: "https://127.0.0.1:3000/resource",
});
Expand Down
17 changes: 12 additions & 5 deletions packages/binding-http/test/http-client-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import express from "express";
import serveStatic from "serve-static";
import { DataSchema, DataSchemaValue, Form } from "wot-typescript-definitions";
import SseStream from "ssestream";
import FakeTimers from "@sinonjs/fake-timers";

// Add spies
import spies from "chai-spies";
Expand Down Expand Up @@ -309,7 +310,7 @@ class HttpClientTest1 {
class HttpClientTest2 {
@test "should register to sse server and get server sent event"(done: Mocha.Done) {
// create sse server

const clock = FakeTimers.install();
const app = express();
app.use(serveStatic(__dirname));
app.get("/sse", function (req: express.Request, res: express.Response) {
Expand Down Expand Up @@ -345,10 +346,16 @@ class HttpClientTest2 {
href: `http://localhost:${port1}/sse`,
};

client.subscribeResource(form, (data) => {
client.unlinkResource(form);
server.close();
});
client
.subscribeResource(form, (data) => {
client.unlinkResource(form);
server.close();
clock.uninstall();
})
.then(() => {
// subscription is active we can tick the clock
clock.tick(400);
});
}

@test "should call error() and complete() on subscription with no connection"(done: () => void) {
Expand Down
2 changes: 1 addition & 1 deletion packages/binding-http/test/memory-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default class InMemoryModel implements ClientCredentialsModel, PasswordMo

expireAllTokens(): void {
for (const token of this.tokens) {
token.accessTokenExpiresAt = new Date();
token.accessTokenExpiresAt = new Date(new Date().setHours(-1));
}
}
}
4 changes: 2 additions & 2 deletions packages/binding-modbus/test/modbus-client-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ describe("Modbus client test", () => {
"modbus:address": 4444,
"modbus:quantity": 1,
"modbus:unitID": 1,
"modbus:timeout": 1000,
"modbus:timeout": 100,
};

await client.readResource(form).should.eventually.be.rejected;
await client.readResource(form).should.eventually.be.rejectedWith("Timed out");
}).timeout(5000);
});
describe("read resource", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/binding-modbus/test/modbus-connection-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("Modbus connection", () => {
"modbus:unitID": 1,
};
const connection = new ModbusConnection("127.0.0.1", 8502, {
connectionTimeout: 1000,
connectionTimeout: 100,
connectionRetryTime: 10,
maxRetries: 1,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ describe("MQTT client implementation", () => {

servient.addClientFactory(new MqttClientFactory());

let counter = 0;

servient.start().then((WoT) => {
expect(brokerServer.getPort()).to.equal(brokerPort);
expect(brokerServer.getAddress()).to.equal(brokerAddress);
Expand All @@ -76,7 +74,6 @@ describe("MQTT client implementation", () => {
client
.subscribeEvent(eventName, (x) => {
if (!eventReceived) {
counter = 0;
eventReceived = true;
} else {
ProtocolHelpers.readStreamFully(ProtocolHelpers.toNodeStream(x.data)).then(
Expand All @@ -88,13 +85,9 @@ describe("MQTT client implementation", () => {
}
})
.then(() => {
const job = setInterval(() => {
++counter;
thing.emitEvent(eventName, counter);
if (counter === 3) {
clearInterval(job);
}
}, 1000);
for (let i = 0; i < 4; i++) {
thing.emitEvent(eventName, i);
}
})
.catch((e) => {
expect(true).to.equal(false);
Expand All @@ -116,8 +109,6 @@ describe("MQTT client implementation", () => {
servient.addClientFactory(new MqttClientFactory());
servient.addClientFactory(new MqttsClientFactory({ rejectUnauthorized: false }));

let counter = 0;

servient.start().then((WoT) => {
expect(brokerServer.getPort()).to.equal(brokerPort);
expect(brokerServer.getAddress()).to.equal(brokerAddress);
Expand All @@ -141,7 +132,6 @@ describe("MQTT client implementation", () => {
client
.subscribeEvent(eventName, (x) => {
if (!eventReceived) {
counter = 0;
eventReceived = true;
} else {
ProtocolHelpers.readStreamFully(ProtocolHelpers.toNodeStream(x.data)).then(
Expand All @@ -153,13 +143,9 @@ describe("MQTT client implementation", () => {
}
})
.then(() => {
const job = setInterval(() => {
++counter;
thing.emitEvent(eventName, counter);
if (counter === 3) {
clearInterval(job);
}
}, 1000);
for (let i = 0; i < 4; i++) {
thing.emitEvent(eventName, i);
}
})
.catch((e) => {
expect(true).to.equal(false);
Expand Down

0 comments on commit 7678d8a

Please sign in to comment.