summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_retry_ws.js
blob: 05f26162987066c5d5243ea453c10d2db5d12f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

'use strict';

const {PushDB, PushService, PushServiceWebSocket} = serviceExports;

const userAgentID = '05f7b940-51b6-4b6f-8032-b83ebb577ded';

function run_test() {
  do_get_profile();
  setPrefs({
    userAgentID: userAgentID,
    pingInterval: 2000,
    retryBaseInterval: 25,
  });
  run_next_test();
}

add_task(function* test_ws_retry() {
  let db = PushServiceWebSocket.newPushDB();
  do_register_cleanup(() => {return db.drop().then(_ => db.close());});

  yield db.put({
    channelID: '61770ba9-2d57-4134-b949-d40404630d5b',
    pushEndpoint: 'https://example.org/push/1',
    scope: 'https://example.net/push/1',
    version: 1,
    originAttributes: '',
    quota: Infinity,
  });

  // Use a mock timer to avoid waiting for the backoff interval.
  let reconnects = 0;
  PushServiceWebSocket._backoffTimer = {
    init(observer, delay, type) {
      reconnects++;
      ok(delay >= 5 && delay <= 2000, `Backoff delay ${
        delay} out of range for attempt ${reconnects}`);
      observer.observe(this, "timer-callback", null);
    },

    cancel() {},
  };

  let handshakeDone;
  let handshakePromise = new Promise(resolve => handshakeDone = resolve);
  PushService.init({
    serverURI: "wss://push.example.org/",
    makeWebSocket(uri) {
      return new MockWebSocket(uri, {
        onHello(request) {
          if (reconnects == 10) {
            this.serverSendMsg(JSON.stringify({
              messageType: 'hello',
              status: 200,
              uaid: userAgentID,
            }));
            handshakeDone();
            return;
          }
          this.serverInterrupt();
        },
      });
    },
  });

  yield handshakePromise;
});