diff --git a/content/base/test/test_websocket_basic.html b/content/base/test/test_websocket_basic.html index 5dbd8ed0190..38c13c554d0 100644 --- a/content/base/test/test_websocket_basic.html +++ b/content/base/test/test_websocket_basic.html @@ -18,25 +18,24 @@ const kUrl = "ws://mochi.test:8888/tests/content/base/test/file_websocket_basic" var ws; -var results = ["test", "/tests/content/base/test/file_websocket_basic", "http://mochi.test:8888", "end"]; - function forcegc() { SpecialPowers.forceGC(); SpecialPowers.gc(); } -function finishWSTest() { - SimpleTest.finish(); -} - function testWebSocket() { - testWebSocket1(); + SimpleTest.executeSoon(testWebSocket1); } /** * Sends message keywords, then receives their values. */ function testWebSocket1() { + var results = ["test", + "/tests/content/base/test/file_websocket_basic", + "http://mochi.test:8888", + "end"]; + ws = new WebSocket(kUrl, "test"); is(ws.url, kUrl, "[1] WebSocket.url"); ws.onopen = function(e) { @@ -51,11 +50,11 @@ function testWebSocket1() { is(results.length, 0, "[1] Number of unreceived messages"); ok(e.wasClean, "[1] Connection closed cleanly"); - testWebSocket2(); + SimpleTest.executeSoon(testWebSocket2); }; ws.onerror = function(e) { ok(false, "[1] onerror() should not have been called!"); - finishWSTest(); + SimpleTest.executeSoon(SimpleTest.finish); }; ws.onmessage = function(e) { document.getElementById('log').textContent += "\n" + e.data; @@ -65,13 +64,13 @@ function testWebSocket1() { } /** - * Sends 1000(+1) test messages, then receives them. + * Sends 1000+1 test messages, then receives them. */ function testWebSocket2() { const testCount = 1000; + const testMessage = "test message 2."; var messageCount = 0; - var testMessage = "test message"; ws = new WebSocket(kUrl, "test"); ws.onopen = function(e) { @@ -81,33 +80,33 @@ function testWebSocket2() { ws.send("end"); }; ws.onclose = function(e) { - is(messageCount, testCount, "[2] Number of received messages"); + is(messageCount, testCount + 1, "[2] Number of received messages"); ok(e.wasClean, "[2] Connection closed cleanly"); - testWebSocket3(); + SimpleTest.executeSoon(testWebSocket3); }; ws.onerror = function(e) { ok(false, "[2] onerror() should not have been called!"); - finishWSTest(); + SimpleTest.executeSoon(SimpleTest.finish); }; ws.onmessage = function(e) { ++messageCount; - is(e.data, testMessage + messageCount, "[2] Received message"); + if (messageCount > testCount) + is(e.data, "end", "[2] Received message"); + else + is(e.data, testMessage + messageCount, "[2] Received message"); document.getElementById('log').textContent = messageCount; - if (messageCount == testCount) { - this.onmessage = null; - } }; } /** - * Sends 100(+1) test messages, then receives them, calling forcegc() at each step. + * Sends 100+1 test messages, then receives them, calling forcegc() at each step. */ function testWebSocket3() { const testCount = 100; + const testMessage = "test message 3."; var messageCount = 0; - var testMessage = "test message"; ws = new WebSocket(kUrl, "test"); ws.onopen = function(e) { @@ -115,26 +114,27 @@ function testWebSocket3() { forcegc(); ws.send(testMessage + i); } + forcegc(); ws.send("end"); }; ws.onclose = function(e) { - is(messageCount, testCount, "[3] Number of received messages"); + is(messageCount, testCount + 1, "[3] Number of received messages"); ok(e.wasClean, "[3] Connection closed cleanly"); - testWebSocket4(); + SimpleTest.executeSoon(testWebSocket4); }; ws.onerror = function(e) { ok(false, "[3] onerror() should not have been called!"); - finishWSTest(); + SimpleTest.executeSoon(SimpleTest.finish); }; ws.onmessage = function(e) { forcegc(); ++messageCount; - is(e.data, testMessage + messageCount, "[3] Received message"); + if (messageCount > testCount) + is(e.data, "end", "[3] Received message"); + else + is(e.data, testMessage + messageCount, "[3] Received message"); document.getElementById('log').textContent = messageCount; - if (messageCount == testCount) { - this.onmessage = null; - } }; } @@ -142,9 +142,10 @@ function testWebSocket3() { * Sends a huge test message, then receives it, then closes the WebSocket from client-side. */ function testWebSocket4() { + // String length = 13 + ((10,000 - 1) * 26) + 11 = 259,998 = almost 254 KiB. + const longString = "messageStart " + new Array(10000).join(" -huge WebSocket message- ") + " messageEnd"; + ws = new WebSocket(kUrl, "test"); - // String length = (10,000 - 1) * 23 = 229,977 = almost 225 KiB. - var longString = new Array(10000).join("-huge websocket message"); ws.onopen = function(e) { is(this, ws, "[4, onopen()] 'this' should point to the WebSocket."); ws.send(longString); @@ -153,12 +154,12 @@ function testWebSocket4() { is(this, ws, "[4, onclose()] 'this' should point to the WebSocket."); ok(e.wasClean, "[4] Connection closed cleanly"); - testWebSocket5(); + SimpleTest.executeSoon(testWebSocket5); }; ws.onerror = function(e) { is(this, ws, "[4, onerror()] 'this' should point to the WebSocket."); ok(false, "[4, onerror()] should not have been called!"); - finishWSTest(); + SimpleTest.executeSoon(SimpleTest.finish); }; ws.onmessage = function(e) { is(this, ws, "[4, onmessage()] 'this' should point to the WebSocket."); @@ -187,11 +188,11 @@ function testWebSocket5() { this.send(msg); is(this.bufferedAmount, msg.length, "[5] Length of buffered message sent after closing"); - finishWSTest(); + SimpleTest.executeSoon(SimpleTest.finish); }; ws.onerror = function(e) { ok(false, "[5] onerror() should not have been called!"); - finishWSTest(); + SimpleTest.executeSoon(SimpleTest.finish); }; }