Update Iot.js
[platform/upstream/iotjs.git] / test / run_pass / test_net6.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
21 var server = net.createServer();
22 var port = 22706;
23
24 server.listen(port, 5);
25
26 server.on('connection', function(socket) {
27   socket.on('data', function(data) {
28     msg += data;
29     socket.end();
30   });
31   socket.on('close', function() {
32     server.close();
33   });
34
35   // as soon as connection established, pause the socket
36   socket.pause();
37
38   // resume after 2 secs
39   timers.setTimeout(function() {
40     socket.resume();
41   }, 2000);
42 });
43
44
45 var socket = new net.Socket();
46 var msg = "";
47
48 socket.connect(port, "127.0.0.1");
49 socket.on('connect', function() {
50   // client writes "1" first, but server is paused for 2 secs
51   // server gets "1" after 2 secs
52   socket.write("1");
53
54   // "2" is appended to msg before "1"
55   timers.setTimeout(function() {
56     msg += "2";
57   }, 1000);
58 });
59
60 socket.on('end', function() {
61   socket.end();
62 });
63
64 process.on('exit', function(code) {
65   assert.equal(code, 0);
66   assert.equal(msg, "21");
67 });