From 255d9e0ad2bcea47ea50d8d2b70c1fead1a7a1fe Mon Sep 17 00:00:00 2001 From: Max Isom Date: Tue, 12 Jan 2021 19:57:24 -0500 Subject: [PATCH] Add reconnection test --- test/stub.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/stub.js b/test/stub.js index 4c9ac81..dab6312 100644 --- a/test/stub.js +++ b/test/stub.js @@ -1,6 +1,8 @@ import test from 'ava'; import TuyaStub from '@tuyapi/stub'; import clone from 'clone'; +import pRetry from 'p-retry'; +import delay from 'delay'; const TuyAPI = require('..'); @@ -164,3 +166,29 @@ test.serial('disconnected event is fired when heartbeat times out', async t => { t.pass(); }); + +test('can reconnect if device goes offline', async t => { + const stubDevice = new TuyAPI({id: '22325186db4a2217dc8e', + key: '4226aa407d5c1e2b', + ip: 'localhost'}); + + const thisStub = clone(stub); + thisStub.startServer(); + + stubDevice.on('error', () => {}); + + await stubDevice.connect(); + + thisStub.shutdown(); + + await delay(500); + + thisStub.startServer(); + + // Attempt to reconnect + await pRetry(async () => { + await stubDevice.connect(); + }, {retries: 3}); + + t.pass(); +});