Skip to content

Commit

Permalink
Merge pull request #28 from evs-broadcast/bugfix/handle_timeout_on_co…
Browse files Browse the repository at this point in the history
…nnect

Timeout is not taken into account when a endpoint does not respond.
  • Loading branch information
dufourgilles authored Jan 16, 2019
2 parents 74b15ed + fecc148 commit 67592ad
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
13 changes: 6 additions & 7 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ S101Socket.prototype.connect = function (timeout = 2) {
self.socket = net.createConnection({
port: self.port,
host: self.address,
timeout: timeout
timeout: 1000 * timeout
},
() => {
winston.debug('socket connected');
Expand Down Expand Up @@ -204,15 +204,14 @@ S101Socket.prototype.connect = function (timeout = 2) {
}
).on('error', (e) => {
self.emit("error", e);
});

self.socket.on('data', (data) => {
}).on("timeout", () => {
self.socket.destroy();
self.emit("error", new Error(`Could not connect to ${self.address}:${self.port} after a timeout of ${timeout} seconds`));
}).on('data', (data) => {
if (self.isConnected()) {
self.codec.dataIn(data);
}
});

self.socket.on('close', () => {
}).on('close', () => {
clearInterval(self.keepaliveIntervalTimer);
self.emit('disconnected');
self.status = "disconnected";
Expand Down
76 changes: 48 additions & 28 deletions test/DeviceTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,60 @@ const DeviceTree = require("../").DeviceTree;
const TreeServer = require("../").TreeServer;

const LOCALHOST = "127.0.0.1";
const UNKNOWN_HOST = "192.168.99.99";
const PORT = 9008;

describe("DeviceTree", () => {
let server;

beforeAll(() => {
return Promise.resolve()
.then(() => new Promise((resolve, reject) => {
fs.readFile("./embrionix.ember", (e, data) => {
if (e) {
reject(e);
}
resolve(Decoder(data));
describe("With server", () => {
let server;
beforeAll(() => {
return Promise.resolve()
.then(() => new Promise((resolve, reject) => {
fs.readFile("./embrionix.ember", (e, data) => {
if (e) {
reject(e);
}
resolve(Decoder(data));
});
}))
.then(root => {
server = new TreeServer(LOCALHOST, PORT, root);
return server.listen();
});
}))
.then(root => {
server = new TreeServer(LOCALHOST, PORT, root);
return server.listen();
});
});
});

afterAll(() => server.close());
afterAll(() => server.close());

it("should gracefully connect and disconnect", () => {
return Promise.resolve()
it("should gracefully connect and disconnect", () => {
return Promise.resolve()
.then(() => {
let tree = new DeviceTree(LOCALHOST, PORT);
return Promise.resolve()
.then(() => tree.connect())
.then(() => tree.getDirectory())
.then(() => tree.disconnect())
.then(() => tree.connect())
.then(() => tree.getDirectory())
.then(() => tree.disconnect())
})
});
});

it("timeout should be taken into account when connecting to unknown host", () => {
let tree = new DeviceTree(UNKNOWN_HOST, PORT);
tree.on("error", () => {
});
const expectedTimeoutInSec = 2;
const initialTime = performance.now();
return tree.connect(expectedTimeoutInSec)
.then(() => {
let tree = new DeviceTree(LOCALHOST, PORT);
return Promise.resolve()
.then(() => tree.connect())
.then(() => tree.getDirectory())
.then(() => tree.disconnect())
.then(() => tree.connect())
.then(() => tree.getDirectory())
.then(() => tree.disconnect())
})
throw new Error("Should have thrown");
},
error => {
const durationMs = performance.now() - initialTime;
const deltaMs = Math.abs(expectedTimeoutInSec * 1000 - durationMs);
expect(deltaMs).toBeLessThan(10);
expect(error.message).toBe(`Could not connect to ${UNKNOWN_HOST}:${PORT} after a timeout of ${expectedTimeoutInSec} seconds`)
});
});
});

0 comments on commit 67592ad

Please sign in to comment.