• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const net = require('net');
5
6const tmpdir = require('../common/tmpdir');
7tmpdir.refresh();
8
9function test(clazz, cb) {
10  let have_ping = false;
11  let have_pong = false;
12
13  function check() {
14    assert.ok(have_ping);
15    assert.ok(have_pong);
16  }
17
18  function ping() {
19    const conn = new clazz();
20
21    conn.on('error', function(err) {
22      throw err;
23    });
24
25    conn.connect(common.PIPE, function() {
26      conn.write('PING', 'utf-8');
27    });
28
29    conn.on('data', function(data) {
30      assert.strictEqual(data.toString(), 'PONG');
31      have_pong = true;
32      conn.destroy();
33    });
34  }
35
36  function pong(conn) {
37    conn.on('error', function(err) {
38      throw err;
39    });
40
41    conn.on('data', function(data) {
42      assert.strictEqual(data.toString(), 'PING');
43      have_ping = true;
44      conn.write('PONG', 'utf-8');
45    });
46
47    conn.on('close', function() {
48      server.close();
49    });
50  }
51
52  const server = net.Server();
53  server.listen(common.PIPE, ping);
54  server.on('connection', pong);
55  server.on('close', function() {
56    check();
57    cb && cb();
58  });
59}
60
61test(net.Stream, function() {
62  test(net.Socket);
63});
64