11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst net = require('net');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
71cb0ef41Sopenharmony_citmpdir.refresh();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cifunction test(clazz, cb) {
101cb0ef41Sopenharmony_ci  let have_ping = false;
111cb0ef41Sopenharmony_ci  let have_pong = false;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  function check() {
141cb0ef41Sopenharmony_ci    assert.ok(have_ping);
151cb0ef41Sopenharmony_ci    assert.ok(have_pong);
161cb0ef41Sopenharmony_ci  }
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  function ping() {
191cb0ef41Sopenharmony_ci    const conn = new clazz();
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    conn.on('error', function(err) {
221cb0ef41Sopenharmony_ci      throw err;
231cb0ef41Sopenharmony_ci    });
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    conn.connect(common.PIPE, function() {
261cb0ef41Sopenharmony_ci      conn.write('PING', 'utf-8');
271cb0ef41Sopenharmony_ci    });
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    conn.on('data', function(data) {
301cb0ef41Sopenharmony_ci      assert.strictEqual(data.toString(), 'PONG');
311cb0ef41Sopenharmony_ci      have_pong = true;
321cb0ef41Sopenharmony_ci      conn.destroy();
331cb0ef41Sopenharmony_ci    });
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  function pong(conn) {
371cb0ef41Sopenharmony_ci    conn.on('error', function(err) {
381cb0ef41Sopenharmony_ci      throw err;
391cb0ef41Sopenharmony_ci    });
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    conn.on('data', function(data) {
421cb0ef41Sopenharmony_ci      assert.strictEqual(data.toString(), 'PING');
431cb0ef41Sopenharmony_ci      have_ping = true;
441cb0ef41Sopenharmony_ci      conn.write('PONG', 'utf-8');
451cb0ef41Sopenharmony_ci    });
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    conn.on('close', function() {
481cb0ef41Sopenharmony_ci      server.close();
491cb0ef41Sopenharmony_ci    });
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  const server = net.Server();
531cb0ef41Sopenharmony_ci  server.listen(common.PIPE, ping);
541cb0ef41Sopenharmony_ci  server.on('connection', pong);
551cb0ef41Sopenharmony_ci  server.on('close', function() {
561cb0ef41Sopenharmony_ci    check();
571cb0ef41Sopenharmony_ci    cb && cb();
581cb0ef41Sopenharmony_ci  });
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_citest(net.Stream, function() {
621cb0ef41Sopenharmony_ci  test(net.Socket);
631cb0ef41Sopenharmony_ci});
64