Update Iot.js
[platform/upstream/iotjs.git] / test / run_pass / test_net3.js
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 var net = require('net');
18 var assert = require('assert');
19
20
21 var port = 22703;
22 var limit = 200;
23 var server = net.createServer();
24
25 server.listen({ port: port });
26
27 server.on('connection', function(socket) {
28   var i = 0;
29   var writing = function() {
30     var ok;
31     do {
32       ok = socket.write("" + (i % 10));
33       if (++i == limit) {
34         socket.end();
35         ok = false;
36       }
37     } while (ok);
38   };
39   socket.on('drain', writing);
40   writing();
41 });
42
43
44 var msg1 = '';
45 var socket1 = net.createConnection(port);
46
47 socket1.on('data', function(data) {
48   msg1 += data;
49 });
50
51
52 var msg2 = '';
53 var socket2 = net.createConnection({port: port});
54
55 socket2.on('data', function(data) {
56   msg2 += data;
57 });
58
59
60 var msg3 = '';
61 var socket3 = net.createConnection({port: port, host: '127.0.0.1'});
62
63 socket3.on('data', function(data) {
64   msg3 += data;
65 });
66
67
68 var msg4 = '';
69 var connectListenerCheck = false;
70 var socket4 = net.createConnection({port: port}, function() {
71   connectListenerCheck = true;
72 });
73
74 socket4.on('data', function(data) {
75   msg4 += data;
76 });
77
78 socket4.on('end', function() {
79   server.close();
80 });
81
82
83 process.on('exit', function(code) {
84   assert.equal(msg1.length, limit);
85   assert.equal(msg2.length, limit);
86   assert.equal(msg3.length, limit);
87   assert.equal(msg4.length, limit);
88   assert(connectListenerCheck);
89 });