Bug 744663. (Av1) Improve documentation and logs, Nits. r=smaug.

(a=test-only)
This commit is contained in:
Serge Gautherie 2012-04-24 03:00:43 +02:00
Родитель 01a85de846
Коммит 6f47b08461
1 изменённых файлов: 98 добавлений и 72 удалений

Просмотреть файл

@ -1,166 +1,198 @@
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html> <html>
<!--
-->
<head> <head>
<title>Basic websocket test</title> <title>Basic WebSocket test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head> </head>
<body onload="testWebSocket()"> <body onload="testWebSocket()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=472529">Mozilla Bug </a> <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=472529">Mozilla Bug 472529</a>
<p id="display"></p> <p id="display"></p>
<div id="content" style="display: none"> <div id="content" style="display: none">
</div> </div>
<pre id="test"> <pre id="test">
<script class="testbody" type="text/javascript"> <script class="testbody" type="text/javascript">
const kUrl = "ws://mochi.test:8888/tests/content/base/test/file_websocket_basic";
var ws; var ws;
var params = ["protocol", "resource", "origin", "end"];
var results = ["test", "/tests/content/base/test/file_websocket_basic", "http://mochi.test:8888", "end"]; var results = ["test", "/tests/content/base/test/file_websocket_basic", "http://mochi.test:8888", "end"];
function forcegc(){ function forcegc() {
SpecialPowers.forceGC(); SpecialPowers.forceGC();
SpecialPowers.gc(); SpecialPowers.gc();
} }
function finishWSTest() { function finishWSTest() {
SimpleTest.finish(); SimpleTest.finish();
} }
function testWebSocket () { function testWebSocket() {
var url = "ws://mochi.test:8888/tests/content/base/test/file_websocket_basic"; testWebSocket1();
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_websocket_basic", "test"); }
is(ws.url, url, "Wrong Websocket.url!");
/**
* Sends message keywords, then receives their values.
*/
function testWebSocket1() {
ws = new WebSocket(kUrl, "test");
is(ws.url, kUrl, "[1] WebSocket.url");
ws.onopen = function(e) { ws.onopen = function(e) {
const params = ["protocol", "resource", "origin", "end"];
for (var i = 0; i < params.length; ++i) { for (var i = 0; i < params.length; ++i) {
document.getElementById('log').textContent += "sending " + params[i] + "\n"; document.getElementById('log').textContent += "sending " + params[i] + "\n";
ws.send(params[i]); ws.send(params[i]);
} }
} };
ws.onclose = function(e) { ws.onclose = function(e) {
is(results.length, 0, "All the messages should have been processed!"); is(results.length, 0, "[1] Number of unreceived messages");
ok(e.wasClean, "Connection closed cleanly"); ok(e.wasClean, "[1] Connection closed cleanly");
testWebSocket2(); testWebSocket2();
} };
ws.onerror = function(e) { ws.onerror = function(e) {
ok(false, "onerror called!"); ok(false, "[1] onerror() should not have been called!");
finishWSTest(); finishWSTest();
} };
ws.onmessage = function(e) { ws.onmessage = function(e) {
document.getElementById('log').textContent += "\n" + e.data; document.getElementById('log').textContent += "\n" + e.data;
is(e.data, results[0], "Unexpected message"); is(e.data, results[0], "[1] Received message");
results.shift(); results.shift();
} };
} }
/**
* Sends 1000(+1) test messages, then receives them.
*/
function testWebSocket2() { function testWebSocket2() {
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_websocket_basic", "test"); const testCount = 1000;
var testCount = 1000; // Send lots of messages
var messageCount = 0; var messageCount = 0;
var testMessage = "test message"; var testMessage = "test message";
ws = new WebSocket(kUrl, "test");
ws.onopen = function(e) { ws.onopen = function(e) {
for (var i = 0; i < testCount; ++i) { for (var i = 1; i <= testCount; ++i) {
ws.send(testMessage + (i + 1)); ws.send(testMessage + i);
} }
ws.send("end"); ws.send("end");
} };
ws.onclose = function(e) { ws.onclose = function(e) {
is(messageCount, testCount, "Didn't receive all the messages!"); is(messageCount, testCount, "[2] Number of received messages");
ok(e.wasClean, "Connection closed cleanly"); ok(e.wasClean, "[2] Connection closed cleanly");
testWebSocket3(); testWebSocket3();
} };
ws.onerror = function(e) { ws.onerror = function(e) {
ok(false, "onerror called!"); ok(false, "[2] onerror() should not have been called!");
finishWSTest(); finishWSTest();
} };
ws.onmessage = function(e) { ws.onmessage = function(e) {
++messageCount; ++messageCount;
is(e.data, testMessage + messageCount, "Wrong message"); is(e.data, testMessage + messageCount, "[2] Received message");
document.getElementById('log').textContent = messageCount; document.getElementById('log').textContent = messageCount;
if (messageCount == testCount) { if (messageCount == testCount) {
this.onmessage = null; this.onmessage = null;
} }
} };
} }
/**
* Sends 100(+1) test messages, then receives them, calling forcegc() at each step.
*/
function testWebSocket3() { function testWebSocket3() {
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_websocket_basic", "test"); const testCount = 100;
var testCount = 100; // Send lots of messages
var messageCount = 0; var messageCount = 0;
var testMessage = "test message"; var testMessage = "test message";
ws = new WebSocket(kUrl, "test");
ws.onopen = function(e) { ws.onopen = function(e) {
for (var i = 0; i < testCount; ++i) { for (var i = 1; i <= testCount; ++i) {
forcegc(); // Do something evil, call cycle collector a lot. forcegc();
ws.send(testMessage + (i + 1)); ws.send(testMessage + i);
} }
ws.send("end"); ws.send("end");
} };
ws.onclose = function(e) { ws.onclose = function(e) {
is(messageCount, testCount, "Didn't receive all the messages!"); is(messageCount, testCount, "[3] Number of received messages");
ok(e.wasClean, "Connection closed cleanly"); ok(e.wasClean, "[3] Connection closed cleanly");
testWebSocket4(); testWebSocket4();
} };
ws.onerror = function(e) { ws.onerror = function(e) {
ok(false, "onerror called!"); ok(false, "[3] onerror() should not have been called!");
finishWSTest(); finishWSTest();
} };
ws.onmessage = function(e) { ws.onmessage = function(e) {
forcegc(); // Do something evil, call cycle collector a lot. forcegc();
++messageCount; ++messageCount;
is(e.data, testMessage + messageCount, "Wrong message"); is(e.data, testMessage + messageCount, "[3] Received message");
document.getElementById('log').textContent = messageCount; document.getElementById('log').textContent = messageCount;
if (messageCount == testCount) { if (messageCount == testCount) {
this.onmessage = null; this.onmessage = null;
} }
} };
} }
/**
* Sends a huge test message, then receives it, then closes the WebSocket from client-side.
*/
function testWebSocket4() { function testWebSocket4() {
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_websocket_basic", "test"); ws = new WebSocket(kUrl, "test");
// String length = (10,000 - 1) * 23 = 229,977 = almost 225 KiB. // String length = (10,000 - 1) * 23 = 229,977 = almost 225 KiB.
var longString = new Array(10000).join("-huge websocket message"); var longString = new Array(10000).join("-huge websocket message");
ws.onopen = function(e) { ws.onopen = function(e) {
is(this, ws, "'this' should point to the WebSocket. (1)"); is(this, ws, "[4, onopen()] 'this' should point to the WebSocket.");
ws.send(longString); ws.send(longString);
} };
ws.onclose = function(e) { ws.onclose = function(e) {
is(this, ws, "'this' should point to the WebSocket. (2)"); is(this, ws, "[4, onclose()] 'this' should point to the WebSocket.");
ok(e.wasClean, "Connection closed cleanly"); ok(e.wasClean, "[4] Connection closed cleanly");
testWebSocket5(); testWebSocket5();
} };
ws.onerror = function(e) { ws.onerror = function(e) {
ok(false, "onerror called!"); is(this, ws, "[4, onerror()] 'this' should point to the WebSocket.");
ok(false, "[4, onerror()] should not have been called!");
finishWSTest(); finishWSTest();
} };
ws.onmessage = function(e) { ws.onmessage = function(e) {
is(this, ws, "'this' should point to the WebSocket. (3)"); is(this, ws, "[4, onmessage()] 'this' should point to the WebSocket.");
// Do not use |is(e.data, longString, "...");| that results in a _very_ long line. // Do not use |is(e.data, longString, "...");| that results in a _very_ long line.
is(e.data.length, longString.length, "Length of received message"); is(e.data.length, longString.length, "[4] Length of received message");
ok(e.data == longString, "Content of received message"); ok(e.data == longString, "[4] Content of received message");
document.getElementById('log').textContent += "\nReceived the huge message"; document.getElementById('log').textContent += "\nReceived the huge message";
this.close(); this.close();
} };
} }
/**
* Closes the WebSocket from client-side, then sends a test message that should be buffered.
*/
function testWebSocket5() { function testWebSocket5() {
ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_websocket_basic", "test"); ws = new WebSocket(kUrl, "test");
ws.onopen = function(e) { ws.onopen = function(e) {
is(this.bufferedAmount, 0, "[5] Length of empty buffer before closing");
this.close(); this.close();
} };
ws.onclose = function(e) { ws.onclose = function(e) {
ok(e.wasClean, "Connection closed cleanly"); ok(e.wasClean, "[5] Connection closed cleanly");
is(this.bufferedAmount, 0, "Shouldn't have anything buffered"); is(this.bufferedAmount, 0, "[5] Length of empty buffer after closing");
var msg = "some data";
var msg = "test message to be buffered";
this.send(msg); this.send(msg);
is(this.bufferedAmount, msg.length, "Should have some data buffered"); is(this.bufferedAmount, msg.length, "[5] Length of buffered message sent after closing");
finishWSTest(); finishWSTest();
} };
ws.onerror = function(e) { ws.onerror = function(e) {
ok(false, "onerror called!"); ok(false, "[5] onerror() should not have been called!");
finishWSTest(); finishWSTest();
} };
} }
SimpleTest.waitForExplicitFinish(); SimpleTest.waitForExplicitFinish();
@ -169,11 +201,5 @@ SimpleTest.waitForExplicitFinish();
<pre id="log"> <pre id="log">
</pre> </pre>
</pre> </pre>
<div>
</div>
</body> </body>
</html> </html>