Skip to content

Commit

Permalink
Add reconnection test
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jan 13, 2021
1 parent 29330ba commit 255d9e0
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 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('..');

Expand Down Expand Up @@ -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();
});

0 comments on commit 255d9e0

Please sign in to comment.