• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6const EventEmitter = require('events');
7const SocketListSend = require('internal/socket_list').SocketListSend;
8
9const key = 'test-key';
10
11// Verify that an error will be received in callback when child is not
12// connected.
13{
14  const child = Object.assign(new EventEmitter(), { connected: false });
15  assert.strictEqual(child.listenerCount('internalMessage'), 0);
16
17  const list = new SocketListSend(child, 'test');
18
19  list._request('msg', 'cmd', false, common.mustCall((err) => {
20    common.expectsError({
21      code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
22      name: 'Error',
23      message: 'Child closed before reply received'
24    })(err);
25    assert.strictEqual(child.listenerCount('internalMessage'), 0);
26  }));
27}
28
29// Verify that the given message will be received in callback.
30{
31  const child = Object.assign(new EventEmitter(), {
32    connected: true,
33    _send: function(msg) {
34      process.nextTick(() =>
35        this.emit('internalMessage', { key, cmd: 'cmd' })
36      );
37    }
38  });
39
40  const list = new SocketListSend(child, key);
41
42  list._request('msg', 'cmd', false, common.mustCall((err, msg) => {
43    assert.strictEqual(err, null);
44    assert.strictEqual(msg.cmd, 'cmd');
45    assert.strictEqual(msg.key, key);
46    assert.strictEqual(child.listenerCount('internalMessage'), 0);
47    assert.strictEqual(child.listenerCount('disconnect'), 0);
48  }));
49}
50
51// Verify that an error will be received in callback when child was
52// disconnected.
53{
54  const child = Object.assign(new EventEmitter(), {
55    connected: true,
56    _send: function(msg) { process.nextTick(() => this.emit('disconnect')); }
57  });
58
59  const list = new SocketListSend(child, key);
60
61  list._request('msg', 'cmd', false, common.mustCall((err) => {
62    common.expectsError({
63      code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
64      name: 'Error',
65      message: 'Child closed before reply received'
66    })(err);
67    assert.strictEqual(child.listenerCount('internalMessage'), 0);
68  }));
69}
70
71// Verify that a "NODE_SOCKET_ALL_CLOSED" message will be received
72// in callback.
73{
74  const child = Object.assign(new EventEmitter(), {
75    connected: true,
76    _send: function(msg) {
77      assert.strictEqual(msg.cmd, 'NODE_SOCKET_NOTIFY_CLOSE');
78      assert.strictEqual(msg.key, key);
79      process.nextTick(() =>
80        this.emit('internalMessage', { key, cmd: 'NODE_SOCKET_ALL_CLOSED' })
81      );
82    }
83  });
84
85  const list = new SocketListSend(child, key);
86
87  list.close(common.mustCall((err, msg) => {
88    assert.strictEqual(err, null);
89    assert.strictEqual(msg.cmd, 'NODE_SOCKET_ALL_CLOSED');
90    assert.strictEqual(msg.key, key);
91    assert.strictEqual(child.listenerCount('internalMessage'), 0);
92    assert.strictEqual(child.listenerCount('disconnect'), 0);
93  }));
94}
95
96// Verify that the count of connections will be received in callback.
97{
98  const count = 1;
99  const child = Object.assign(new EventEmitter(), {
100    connected: true,
101    _send: function(msg) {
102      assert.strictEqual(msg.cmd, 'NODE_SOCKET_GET_COUNT');
103      assert.strictEqual(msg.key, key);
104      process.nextTick(() =>
105        this.emit('internalMessage', {
106          key,
107          count,
108          cmd: 'NODE_SOCKET_COUNT'
109        })
110      );
111    }
112  });
113
114  const list = new SocketListSend(child, key);
115
116  list.getConnections(common.mustCall((err, msg) => {
117    assert.strictEqual(err, null);
118    assert.strictEqual(msg, count);
119    assert.strictEqual(child.listenerCount('internalMessage'), 0);
120    assert.strictEqual(child.listenerCount('disconnect'), 0);
121  }));
122}
123
124// Verify that an error will be received in callback when child is
125// disconnected after sending a message and before getting the reply.
126{
127  const count = 1;
128  const child = Object.assign(new EventEmitter(), {
129    connected: true,
130    _send: function() {
131      process.nextTick(() => {
132        this.emit('disconnect');
133        this.emit('internalMessage', { key, count, cmd: 'NODE_SOCKET_COUNT' });
134      });
135    }
136  });
137
138  const list = new SocketListSend(child, key);
139
140  list.getConnections(common.mustCall((err) => {
141    common.expectsError({
142      code: 'ERR_CHILD_CLOSED_BEFORE_REPLY',
143      name: 'Error',
144      message: 'Child closed before reply received'
145    })(err);
146    assert.strictEqual(child.listenerCount('internalMessage'), 0);
147  }));
148}
149