f83db7213495512778bf0a7246dd1a43d7edd85c
[platform/upstream/iotjs.git] / tools / test / run_pass / test_net8.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 var net = require('net');
17 var assert = require('assert');
18 var timers = require('timers');
19
20 var port = 22708;
21
22 var server = net.createServer();
23
24 server.listen(port, 5);
25
26 server.on('close', function() {
27   assert.equal(msg, '12');
28 });
29
30 server.on('connection', function(socket) {
31   socket.on('data', function(data) {
32     socket.end(data);
33   });
34   socket.on('close', function() {
35     server.close();
36   });
37 });
38
39 var sock1 = new net.Socket();
40 var sock2 = new net.Socket();
41
42 var msg = '';
43
44 sock1.connect(port, 'localhost');
45 sock2.connect(port, 'localhost');
46
47 sock1.on('data', function(data) {
48   msg += data;
49 });
50
51 sock1.on('end', function() {
52   sock1.end();
53 });
54
55 sock2.on('data', function(data) {
56   msg += data;
57 });
58
59 sock2.on('end', function() {
60   sock2.end();
61 });
62
63 timers.setTimeout(function() {
64   sock1.write('1');
65 }, 1000);
66
67 timers.setTimeout(function() {
68   sock2.write('2');
69 }, 2000);
70
71 process.on('exit', function(code) {
72   assert.equal(code, 0);
73 });