7 var v8tools = sysapps_raw_socket_test.v8tools;
8 var api = xwalk.experimental.raw_socket;
18 function runNextTest() {
19 test_list[current_test++]();
22 function reportFail(message) {
24 document.title = "Fail";
28 document.title = "Pass";
31 function memoryManagement() {
33 var garbageCollectionCount = 0;
35 // We cannot run the GC inside a callback, because the callee of the
36 // callback holds a reference (that is why runGCandCheck is called from
37 // a setTimeout. Another important thing to notice is nullifying the
38 // server or client object right away won't work because they use
39 // delayed initialization, which is fine, because as soon as the
40 // initialization is completed, the object will be collected if no
41 // variable is keeping a reference to it.
42 function runGCandCheck() {
44 if (garbageCollectionCount != 2)
45 reportFail("TCPSocket or TCPServerSocket is leaking.");
50 var server = new api.TCPServerSocket(
51 {"localAddress": "127.0.0.1", "localPort": 54321});
52 server.onerror = server.onopen = nullifyServer;
53 server.tracker = v8tools.lifecycleTracker();
54 server.tracker.destructor = function() {
55 garbageCollectionCount++;
58 function nullifyServer() {
59 server.onerror = null;
63 if (++eventCount == 2)
64 setTimeout(runGCandCheck, 0);
67 var client = new api.TCPSocket("127.0.0.1", 54321);
68 client.onerror = client.onopen = nullifyClient;
69 client.tracker = v8tools.lifecycleTracker();
70 client.tracker.destructor = function() {
71 garbageCollectionCount++;
74 function nullifyClient() {
75 client.onerror = null;
79 if (++eventCount == 2)
80 setTimeout(runGCandCheck, 0);
84 // This test is designed like a data ping pong. First we open a server
85 // listening for incoming connections. Secondly we connect a client socket
86 // into this server, that immediately sends a greetings message that is
87 // readily verified. The client then sends back some data to the server,
88 // which also gets checked.
90 // This is not an API conformance test, it just verifies if the basic
91 // functionality works. W3C should provide the former.
92 function pingPong(serverPort) {
93 serverPort = serverPort || 5000;
94 var serverPortMax = 5020;
95 var testData = "Hello World!";
97 var server = new api.TCPServerSocket(
98 {"localAddress": "127.0.0.1", "localPort": serverPort});
100 server.onerror = function() {
101 // The default port might be busy, so we try different ports
102 // before reporting failure.
103 if (serverPort < serverPortMax)
104 pingPong(++serverPort);
106 reportFail("Not able to listen at port " + serverPort + ".");
109 server.onopen = function() {
110 var client = new api.TCPSocket("127.0.0.1", serverPort);
112 client.onerror = function() {
113 reportFail("Not able to connect to port " + serverPort + ".");
116 client.ondata = function(event) {
117 var view = new Uint8Array(event.data);
118 var data = String.fromCharCode.apply(null, view);
120 if (data != testData)
121 reportFail("Invalid data received by the client socket.");
123 client.send(testData);
127 server.onconnect = function(event) {
128 event.connectedSocket.send(testData);
129 event.connectedSocket.ondata = function (event) {
130 var view = new Uint8Array(event.data);
131 var data = String.fromCharCode.apply(null, view);
133 if (data != testData)
134 reportFail("Invalid data received by server socket.");
141 function serverPortBusy(serverPort) {
142 serverPort = serverPort || 7000;
143 var serverPortMax = 7020;
145 var server = new api.TCPServerSocket(
146 {"localAddress": "127.0.0.1", "localPort": serverPort});
148 server.onerror = function() {
149 if (serverPort < serverPortMax)
150 serverPortBusy(++serverPort);
152 reportFail("Not able to listen at port " + serverPort + ".");
155 server.onopen = function() {
156 var serverShouldFail = new api.TCPServerSocket(
157 {"localAddress": "127.0.0.1", "localPort": serverPort});
159 // Should fail, port already in use by another server.
160 serverShouldFail.onerror = runNextTest();
161 serverShouldFail.onopen = function() {
162 reportFail("Port " + serverPort + " should be busy.");