index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const { expect } = require('chai');
  2. const Client = require('../../../src/models/client');
  3. const Realm = require('../../../src/models/realm');
  4. const checkBrokenConnectionsBuilder = require('../../../src/services/checkBrokenConnections');
  5. describe('checkBrokenConnections service', () => {
  6. it('should remove client after 2 checks', (done) => {
  7. const realm = new Realm();
  8. const doubleCheckTime = 55;//~ equals to checkBrokenConnections.CHECK_INTERVAL * 2
  9. const checkBrokenConnections = checkBrokenConnectionsBuilder({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
  10. const client = new Client({ id: 'id', token: '' });
  11. realm.setClient(client, 'id');
  12. checkBrokenConnections.start();
  13. setTimeout(() => {
  14. expect(realm.getClientById('id')).to.be.undefined;
  15. checkBrokenConnections.stop();
  16. done();
  17. }, checkBrokenConnections.CHECK_INTERVAL * 2 + 3);
  18. });
  19. it('should remove client after 1 ping', (done) => {
  20. const realm = new Realm();
  21. const doubleCheckTime = 55;//~ equals to checkBrokenConnections.CHECK_INTERVAL * 2
  22. const checkBrokenConnections = checkBrokenConnectionsBuilder({ realm, config: { alive_timeout: doubleCheckTime }, checkInterval: 30 });
  23. const client = new Client({ id: 'id', token: '' });
  24. realm.setClient(client, 'id');
  25. checkBrokenConnections.start();
  26. //set ping after first check
  27. setTimeout(() => {
  28. client.setLastPing(new Date().getTime());
  29. setTimeout(() => {
  30. expect(realm.getClientById('id')).to.be.undefined;
  31. checkBrokenConnections.stop();
  32. done();
  33. }, checkBrokenConnections.CHECK_INTERVAL * 2 + 10);
  34. }, checkBrokenConnections.CHECK_INTERVAL);
  35. });
  36. });