index.ts 1.7 KB

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