diff --git a/client.js b/client.js index b4dbfb6..cba85d5 100755 --- a/client.js +++ b/client.js @@ -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'); @@ -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"; diff --git a/test/DeviceTree.test.js b/test/DeviceTree.test.js index 21c55a9..c0a7c73 100644 --- a/test/DeviceTree.test.js +++ b/test/DeviceTree.test.js @@ -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`) + }); }); });