summaryrefslogtreecommitdiff
path: root/test/parallel/test-internal-socket-list-send.js
blob: 2e69edcedffd9f4306f847f6c76d4308b2a1269d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Flags: --expose-internals
'use strict';

const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');
const SocketListSend = require('internal/socket_list').SocketListSend;

const key = 'test-key';

// Verify that an error will be received in callback when child is not
// connected.
{
  const child = Object.assign(new EventEmitter(), { connected: false });
  assert.strictEqual(child.listenerCount('internalMessage'), 0);

  const list = new SocketListSend(child, 'test');

  list._request('msg', 'cmd', false, common.mustCall((err) => {
    common.expectsError({
      code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
      type: Error,
      message: 'Child closed before reply received'
    })(err);
    assert.strictEqual(child.listenerCount('internalMessage'), 0);
  }));
}

// Verify that the given message will be received in callback.
{
  const child = Object.assign(new EventEmitter(), {
    connected: true,
    _send: function(msg) {
      process.nextTick(() =>
        this.emit('internalMessage', { key, cmd: 'cmd' })
      );
    }
  });

  const list = new SocketListSend(child, key);

  list._request('msg', 'cmd', false, common.mustCall((err, msg) => {
    assert.strictEqual(err, null);
    assert.strictEqual(msg.cmd, 'cmd');
    assert.strictEqual(msg.key, key);
    assert.strictEqual(child.listenerCount('internalMessage'), 0);
    assert.strictEqual(child.listenerCount('disconnect'), 0);
  }));
}

// Verify that an error will be received in callback when child was
// disconnected.
{
  const child = Object.assign(new EventEmitter(), {
    connected: true,
    _send: function(msg) { process.nextTick(() => this.emit('disconnect')); }
  });

  const list = new SocketListSend(child, key);

  list._request('msg', 'cmd', false, common.mustCall((err) => {
    common.expectsError({
      code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
      type: Error,
      message: 'Child closed before reply received'
    })(err);
    assert.strictEqual(child.listenerCount('internalMessage'), 0);
  }));
}

// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be received
// in callback.
{
  const child = Object.assign(new EventEmitter(), {
    connected: true,
    _send: function(msg) {
      assert.strictEqual(msg.cmd, 'NODE_SOCKET_NOTIFY_CLOSE');
      assert.strictEqual(msg.key, key);
      process.nextTick(() =>
        this.emit('internalMessage', { key, cmd: 'NODE_SOCKET_ALL_CLOSED' })
      );
    }
  });

  const list = new SocketListSend(child, key);

  list.close(common.mustCall((err, msg) => {
    assert.strictEqual(err, null);
    assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED');
    assert.strictEqual(msg.key, key);
    assert.strictEqual(child.listenerCount('internalMessage'), 0);
    assert.strictEqual(child.listenerCount('disconnect'), 0);
  }));
}

// Verify that the count of connections will be received in callback.
{
  const count = 1;
  const child = Object.assign(new EventEmitter(), {
    connected: true,
    _send: function(msg) {
      assert.strictEqual(msg.cmd, 'NODE_SOCKET_GET_COUNT');
      assert.strictEqual(msg.key, key);
      process.nextTick(() =>
        this.emit('internalMessage', {
          key,
          count,
          cmd: 'NODE_SOCKET_COUNT'
        })
      );
    }
  });

  const list = new SocketListSend(child, key);

  list.getConnections(common.mustCall((err, msg) => {
    assert.strictEqual(err, null);
    assert.strictEqual(msg, count);
    assert.strictEqual(child.listenerCount('internalMessage'), 0);
    assert.strictEqual(child.listenerCount('disconnect'), 0);
  }));
}

// Verify that an error will be received in callback when child is
// disconnected after sending a message and before getting the reply.
{
  const count = 1;
  const child = Object.assign(new EventEmitter(), {
    connected: true,
    _send: function() {
      process.nextTick(() => {
        this.emit('disconnect');
        this.emit('internalMessage', { key, count, cmd: 'NODE_SOCKET_COUNT' });
      });
    }
  });

  const list = new SocketListSend(child, key);

  list.getConnections(common.mustCall((err) => {
    common.expectsError({
      code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
      type: Error,
      message: 'Child closed before reply received'
    })(err);
    assert.strictEqual(child.listenerCount('internalMessage'), 0);
  }));
}