test_reconnect_retry.js (2265B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 function run_test() { 7 do_get_profile(); 8 setPrefs({ 9 requestTimeout: 10000, 10 retryBaseInterval: 150, 11 }); 12 run_next_test(); 13 } 14 15 add_task(async function test_reconnect_retry() { 16 let db = PushServiceWebSocket.newPushDB(); 17 registerCleanupFunction(() => { 18 return db.drop().then(_ => db.close()); 19 }); 20 21 let registers = 0; 22 let channelID; 23 PushService.init({ 24 serverURI: "wss://push.example.org/", 25 db, 26 makeWebSocket(uri) { 27 return new MockWebSocket(uri, { 28 onHello() { 29 this.serverSendMsg( 30 JSON.stringify({ 31 messageType: "hello", 32 status: 200, 33 uaid: "083e6c17-1063-4677-8638-ab705aebebc2", 34 }) 35 ); 36 }, 37 onRegister(request) { 38 registers++; 39 if (registers == 1) { 40 channelID = request.channelID; 41 this.serverClose(); 42 return; 43 } 44 if (registers == 2) { 45 equal( 46 request.channelID, 47 channelID, 48 "Should retry registers after reconnect" 49 ); 50 } 51 this.serverSendMsg( 52 JSON.stringify({ 53 messageType: "register", 54 channelID: request.channelID, 55 pushEndpoint: "https://example.org/push/" + request.channelID, 56 status: 200, 57 }) 58 ); 59 }, 60 }); 61 }, 62 }); 63 64 let registration = await PushService.register({ 65 scope: "https://example.com/page/1", 66 originAttributes: ChromeUtils.originAttributesToSuffix({ 67 inIsolatedMozBrowser: false, 68 }), 69 }); 70 let retryEndpoint = "https://example.org/push/" + channelID; 71 equal( 72 registration.endpoint, 73 retryEndpoint, 74 "Wrong endpoint for retried request" 75 ); 76 77 registration = await PushService.register({ 78 scope: "https://example.com/page/2", 79 originAttributes: ChromeUtils.originAttributesToSuffix({ 80 inIsolatedMozBrowser: false, 81 }), 82 }); 83 notEqual( 84 registration.endpoint, 85 retryEndpoint, 86 "Wrong endpoint for new request" 87 ); 88 89 equal(registers, 3, "Wrong registration count"); 90 });