amqplib 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +54 -0
- package/callback_api.d.ts +19 -3
- package/index.d.ts +18 -2
- package/lib/callback_model.js +3 -22
- package/lib/channel.js +27 -1
- package/lib/channel_model.js +4 -29
- package/lib/properties.d.ts +3 -0
- package/lib/recovery.js +71 -10
- package/package.json +12 -2
- package/.claude/commands/lint-disable.md +0 -9
- package/.claude/commands/lint-enable.md +0 -12
- package/.claude/settings.local.json +0 -7
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -41
- package/.github/workflows/publish.yml +0 -59
- package/.github/workflows/test.yml +0 -54
- package/CLAUDE.md +0 -12
- package/Makefile +0 -42
- package/bin/amqp-rabbitmq-0.9.1.json +0 -483
- package/bin/generate-defs.js +0 -734
- package/biome.json +0 -51
- package/lefthook.yml +0 -17
- package/test/bitset.test.js +0 -65
- package/test/callback_api.test.js +0 -439
- package/test/channel.test.js +0 -918
- package/test/channel_api.test.js +0 -599
- package/test/codec.test.js +0 -171
- package/test/connect.test.js +0 -155
- package/test/connection.test.js +0 -772
- package/test/data.test.js +0 -33
- package/test/frame.test.js +0 -180
- package/test/lib/data.js +0 -249
- package/test/lib/util.js +0 -223
- package/test/mux.test.js +0 -258
- package/test/recovery.test.js +0 -402
- package/test/uri_spec.test.js +0 -210
package/test/mux.test.js
DELETED
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
const { describe, it } = require('node:test');
|
|
2
|
-
const assert = require('node:assert');
|
|
3
|
-
const Mux = require('../lib/mux').Mux;
|
|
4
|
-
const PassThrough = require('node:stream').PassThrough;
|
|
5
|
-
|
|
6
|
-
const latch = require('./lib/util').latch;
|
|
7
|
-
const schedule = require('./lib/util').schedule;
|
|
8
|
-
|
|
9
|
-
function stream() {
|
|
10
|
-
return new PassThrough({ objectMode: true });
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function readAllObjects(s, cb) {
|
|
14
|
-
const objs = [];
|
|
15
|
-
|
|
16
|
-
function read() {
|
|
17
|
-
let v = s.read();
|
|
18
|
-
while (v !== null) {
|
|
19
|
-
objs.push(v);
|
|
20
|
-
v = s.read();
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
s.on('end', () => {
|
|
25
|
-
cb(objs);
|
|
26
|
-
});
|
|
27
|
-
s.on('readable', read);
|
|
28
|
-
|
|
29
|
-
read();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
describe('mux', () => {
|
|
33
|
-
|
|
34
|
-
it('single input', (_t, done) => {
|
|
35
|
-
const input = stream();
|
|
36
|
-
const output = stream();
|
|
37
|
-
input.on('end', () => output.end());
|
|
38
|
-
|
|
39
|
-
const mux = new Mux(output);
|
|
40
|
-
mux.pipeFrom(input);
|
|
41
|
-
|
|
42
|
-
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
43
|
-
// not 0, it's treated specially by PassThrough for some reason. By
|
|
44
|
-
// 'specially' I mean it breaks the stream. See e.g.,
|
|
45
|
-
// https://github.com/isaacs/readable-stream/pull/55
|
|
46
|
-
data.forEach((chunk) => input.write(chunk));
|
|
47
|
-
|
|
48
|
-
readAllObjects(output, (vals) => {
|
|
49
|
-
assert.deepEqual(data, vals);
|
|
50
|
-
done();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
input.end();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('single input, resuming stream', (_t, done) => {
|
|
57
|
-
const input = stream();
|
|
58
|
-
const output = stream();
|
|
59
|
-
input.on('end', () => output.end());
|
|
60
|
-
|
|
61
|
-
const mux = new Mux(output);
|
|
62
|
-
mux.pipeFrom(input);
|
|
63
|
-
|
|
64
|
-
// Streams might be blocked and become readable again, simulate this
|
|
65
|
-
// using a special read function and a marker
|
|
66
|
-
const data = [1, 2, 3, 4, 'skip', 6, 7, 8, 9];
|
|
67
|
-
|
|
68
|
-
const oldRead = input.read;
|
|
69
|
-
input.read = (size) => {
|
|
70
|
-
const val = oldRead.call(input, size);
|
|
71
|
-
|
|
72
|
-
if (val === 'skip') {
|
|
73
|
-
input.emit('readable');
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return val;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
data.forEach((chunk) => input.write(chunk));
|
|
81
|
-
|
|
82
|
-
readAllObjects(output, (vals) => {
|
|
83
|
-
assert.deepEqual([1, 2, 3, 4, 6, 7, 8, 9], vals);
|
|
84
|
-
done();
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
input.end();
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('two sequential inputs', (_t, done) => {
|
|
91
|
-
const input1 = stream();
|
|
92
|
-
const input2 = stream();
|
|
93
|
-
const output = stream();
|
|
94
|
-
const mux = new Mux(output);
|
|
95
|
-
mux.pipeFrom(input1);
|
|
96
|
-
mux.pipeFrom(input2);
|
|
97
|
-
|
|
98
|
-
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
99
|
-
data.forEach((v) => {
|
|
100
|
-
input1.write(v);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
input1.on('end', () => {
|
|
104
|
-
data.forEach((v) => {
|
|
105
|
-
input2.write(v);
|
|
106
|
-
});
|
|
107
|
-
input2.end();
|
|
108
|
-
});
|
|
109
|
-
input2.on('end', () => output.end());
|
|
110
|
-
|
|
111
|
-
input1.end();
|
|
112
|
-
readAllObjects(output, (vs) => {
|
|
113
|
-
assert.equal(2 * data.length, vs.length);
|
|
114
|
-
done();
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('two interleaved inputs', (_t, done) => {
|
|
119
|
-
const input1 = stream();
|
|
120
|
-
const input2 = stream();
|
|
121
|
-
const output = stream();
|
|
122
|
-
const mux = new Mux(output);
|
|
123
|
-
mux.pipeFrom(input1);
|
|
124
|
-
mux.pipeFrom(input2);
|
|
125
|
-
|
|
126
|
-
const decrementLatch = latch(2, () => output.end());
|
|
127
|
-
input1.on('end', decrementLatch);
|
|
128
|
-
input2.on('end', decrementLatch);
|
|
129
|
-
|
|
130
|
-
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
131
|
-
data.forEach((v) => {
|
|
132
|
-
input1.write(v);
|
|
133
|
-
});
|
|
134
|
-
input1.end();
|
|
135
|
-
|
|
136
|
-
data.forEach((v) => input2.write(v));
|
|
137
|
-
input2.end();
|
|
138
|
-
|
|
139
|
-
readAllObjects(output, (vs) => {
|
|
140
|
-
assert.equal(2 * data.length, vs.length);
|
|
141
|
-
done();
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it('unpipe', (_t, done) => {
|
|
146
|
-
const input = stream();
|
|
147
|
-
const output = stream();
|
|
148
|
-
const mux = new Mux(output);
|
|
149
|
-
|
|
150
|
-
const pipedData = [1, 2, 3, 4, 5];
|
|
151
|
-
const unpipedData = [6, 7, 8, 9];
|
|
152
|
-
|
|
153
|
-
mux.pipeFrom(input);
|
|
154
|
-
|
|
155
|
-
schedule(() => {
|
|
156
|
-
pipedData.forEach((chunk) => input.write(chunk));
|
|
157
|
-
|
|
158
|
-
schedule(() => {
|
|
159
|
-
mux.unpipeFrom(input);
|
|
160
|
-
|
|
161
|
-
schedule(() => {
|
|
162
|
-
unpipedData.forEach((chunk) => input.write(chunk));
|
|
163
|
-
input.end();
|
|
164
|
-
schedule(() => {
|
|
165
|
-
// exhaust so that 'end' fires
|
|
166
|
-
let v;
|
|
167
|
-
do {
|
|
168
|
-
v = input.read();
|
|
169
|
-
} while (v);
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
input.on('end', () => output.end());
|
|
176
|
-
|
|
177
|
-
readAllObjects(output, (vals) => {
|
|
178
|
-
try {
|
|
179
|
-
assert.deepEqual(pipedData, vals);
|
|
180
|
-
done();
|
|
181
|
-
} catch (e) {
|
|
182
|
-
done(e);
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('backpressure does not crash when newStreams is drained', () => {
|
|
188
|
-
// Regression test: out.write() returns false (backpressure) while
|
|
189
|
-
// draining newStreams. The buggy code does an unconditional shift at the
|
|
190
|
-
// bottom of the loop, so if the next stream reads null (empty) it gets
|
|
191
|
-
// discarded without being pushed back, leaving newStreams empty and
|
|
192
|
-
// triggering assert(this.newStreams.length > 0) in the else-branch.
|
|
193
|
-
//
|
|
194
|
-
// Scenario: newStreams = [input1, input2], input1 has data, input2 empty.
|
|
195
|
-
// - shift input1, read 1, write → backpressure (false), push input1 back
|
|
196
|
-
// - (buggy) unconditional shift takes input2, loop condition false → exit
|
|
197
|
-
// - newStreams = [input1], length > 0, assert passes ← not the bug path
|
|
198
|
-
//
|
|
199
|
-
// The crash path requires backpressure AND the shifted stream reading null
|
|
200
|
-
// with no other streams remaining. Use a single stream that has data, then
|
|
201
|
-
// engineer a second read() on the same stream returning null after the
|
|
202
|
-
// write so the stream is not pushed back, emptying newStreams entirely.
|
|
203
|
-
const input1 = stream();
|
|
204
|
-
const output = stream();
|
|
205
|
-
const mux = new Mux(output);
|
|
206
|
-
|
|
207
|
-
mux.newStreams.push(input1);
|
|
208
|
-
input1.write(1);
|
|
209
|
-
|
|
210
|
-
// After the first write (which triggers backpressure), wrap read() so
|
|
211
|
-
// the stream appears empty on the next call — simulating a stream that
|
|
212
|
-
// had one item buffered and is now drained within the same roundrobin pass.
|
|
213
|
-
const originalRead = input1.read.bind(input1);
|
|
214
|
-
let readCount = 0;
|
|
215
|
-
input1.read = (size) => {
|
|
216
|
-
readCount++;
|
|
217
|
-
if (readCount > 1) return null; // appear empty after first read
|
|
218
|
-
return originalRead(size);
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
const originalWrite = output.write.bind(output);
|
|
222
|
-
output.write = (...args) => {
|
|
223
|
-
originalWrite(...args);
|
|
224
|
-
return false; // signal backpressure immediately
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
assert.doesNotThrow(() => mux._readIncoming());
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
it('roundrobin', (_t, done) => {
|
|
231
|
-
const input1 = stream();
|
|
232
|
-
const input2 = stream();
|
|
233
|
-
const output = stream();
|
|
234
|
-
const mux = new Mux(output);
|
|
235
|
-
|
|
236
|
-
mux.pipeFrom(input1);
|
|
237
|
-
mux.pipeFrom(input2);
|
|
238
|
-
|
|
239
|
-
const decrementLatch = latch(2, () => output.end());
|
|
240
|
-
input1.on('end', decrementLatch);
|
|
241
|
-
input2.on('end', decrementLatch);
|
|
242
|
-
|
|
243
|
-
const ones = [1, 1, 1, 1, 1];
|
|
244
|
-
ones.forEach((v) => {
|
|
245
|
-
input1.write(v);
|
|
246
|
-
});
|
|
247
|
-
input1.end();
|
|
248
|
-
|
|
249
|
-
const twos = [2, 2, 2, 2, 2];
|
|
250
|
-
twos.forEach((v) => input2.write(v));
|
|
251
|
-
input2.end();
|
|
252
|
-
|
|
253
|
-
readAllObjects(output, (vs) => {
|
|
254
|
-
assert.deepEqual([1, 2, 1, 2, 1, 2, 1, 2, 1, 2], vs);
|
|
255
|
-
done();
|
|
256
|
-
});
|
|
257
|
-
});
|
|
258
|
-
});
|
package/test/recovery.test.js
DELETED
|
@@ -1,402 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const { describe, it } = require('node:test');
|
|
4
|
-
const assert = require('node:assert');
|
|
5
|
-
const EventEmitter = require('node:events');
|
|
6
|
-
const recovery = require('../lib/recovery');
|
|
7
|
-
|
|
8
|
-
class FakePromiseModel extends EventEmitter {
|
|
9
|
-
constructor(id) {
|
|
10
|
-
super();
|
|
11
|
-
this.id = id;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
close() {
|
|
15
|
-
return Promise.resolve();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
createChannel(options) {
|
|
19
|
-
return Promise.resolve({kind: 'channel', id: this.id, options});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
createConfirmChannel(options) {
|
|
23
|
-
return Promise.resolve({kind: 'confirm', id: this.id, options});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
updateSecret(_newSecret, _reason) {
|
|
27
|
-
return Promise.resolve();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
class FakeCallbackModel extends EventEmitter {
|
|
32
|
-
constructor(id) {
|
|
33
|
-
super();
|
|
34
|
-
this.id = id;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
close(cb) {
|
|
38
|
-
cb && cb(null);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
createChannel(options, cb) {
|
|
42
|
-
if (typeof options === 'function') {
|
|
43
|
-
cb = options;
|
|
44
|
-
options = undefined;
|
|
45
|
-
}
|
|
46
|
-
cb && cb(null, {kind: 'channel', id: this.id, options});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
createConfirmChannel(options, cb) {
|
|
50
|
-
if (typeof options === 'function') {
|
|
51
|
-
cb = options;
|
|
52
|
-
options = undefined;
|
|
53
|
-
}
|
|
54
|
-
cb && cb(null, {kind: 'confirm', id: this.id, options});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
updateSecret(_newSecret, _reason, cb) {
|
|
58
|
-
cb && cb(null);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
describe('recovery', () => {
|
|
63
|
-
it('promise recovery reconnects after close', async () => {
|
|
64
|
-
const models = [];
|
|
65
|
-
let opened = 0;
|
|
66
|
-
|
|
67
|
-
function openModel() {
|
|
68
|
-
const model = new FakePromiseModel(++opened);
|
|
69
|
-
models.push(model);
|
|
70
|
-
return Promise.resolve(model);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const client = await recovery.connectWithRecoveryPromise(openModel, {
|
|
74
|
-
initialDelay: 1,
|
|
75
|
-
maxDelay: 1,
|
|
76
|
-
jitter: 0,
|
|
77
|
-
maxRetries: 3,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
assert.equal(1, opened);
|
|
81
|
-
|
|
82
|
-
await new Promise((resolve) => {
|
|
83
|
-
client.once('connect', () => {
|
|
84
|
-
assert.equal(2, opened);
|
|
85
|
-
resolve();
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
models[0].emit('close', new Error('socket closed'));
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
await client.close();
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('promise recovery runs setup on every connect', async () => {
|
|
95
|
-
const models = [];
|
|
96
|
-
let setupCalls = 0;
|
|
97
|
-
let opened = 0;
|
|
98
|
-
|
|
99
|
-
function openModel() {
|
|
100
|
-
const model = new FakePromiseModel(++opened);
|
|
101
|
-
models.push(model);
|
|
102
|
-
return Promise.resolve(model);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const client = await recovery.connectWithRecoveryPromise(openModel, {
|
|
106
|
-
initialDelay: 1,
|
|
107
|
-
maxDelay: 1,
|
|
108
|
-
jitter: 0,
|
|
109
|
-
maxRetries: 3,
|
|
110
|
-
setup() {
|
|
111
|
-
setupCalls++;
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
assert.equal(1, setupCalls);
|
|
116
|
-
|
|
117
|
-
await new Promise((resolve) => {
|
|
118
|
-
client.once('connect', () => {
|
|
119
|
-
assert.equal(2, setupCalls);
|
|
120
|
-
resolve();
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
models[0].emit('close', new Error('socket closed'));
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
await client.close();
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('callback recovery reconnects and creates channels after reconnect', (done) => {
|
|
130
|
-
const models = [];
|
|
131
|
-
let opened = 0;
|
|
132
|
-
|
|
133
|
-
function openModel() {
|
|
134
|
-
const model = new FakeCallbackModel(++opened);
|
|
135
|
-
models.push(model);
|
|
136
|
-
return Promise.resolve(model);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const client = recovery.connectWithRecoveryCallback(
|
|
140
|
-
openModel,
|
|
141
|
-
{
|
|
142
|
-
initialDelay: 1,
|
|
143
|
-
maxDelay: 1,
|
|
144
|
-
jitter: 0,
|
|
145
|
-
maxRetries: 3,
|
|
146
|
-
},
|
|
147
|
-
(err, c) => {
|
|
148
|
-
if (err) return done(err);
|
|
149
|
-
|
|
150
|
-
c.createChannel((createErr, ch) => {
|
|
151
|
-
if (createErr) return done(createErr);
|
|
152
|
-
assert.equal(1, ch.id);
|
|
153
|
-
|
|
154
|
-
c.once('connect', () => {
|
|
155
|
-
c.createChannel((reconnectErr, reconnectedChannel) => {
|
|
156
|
-
if (reconnectErr) return done(reconnectErr);
|
|
157
|
-
assert.equal(2, reconnectedChannel.id);
|
|
158
|
-
c.close(done);
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
models[0].emit('close', new Error('socket closed'));
|
|
163
|
-
});
|
|
164
|
-
},
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
assert(client);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('handler-error from underlying model is forwarded to recovery wrapper', async () => {
|
|
171
|
-
const models = [];
|
|
172
|
-
let opened = 0;
|
|
173
|
-
|
|
174
|
-
function openModel() {
|
|
175
|
-
const model = new FakePromiseModel(++opened);
|
|
176
|
-
models.push(model);
|
|
177
|
-
return Promise.resolve(model);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const client = await recovery.connectWithRecoveryPromise(openModel, {
|
|
181
|
-
initialDelay: 1,
|
|
182
|
-
maxDelay: 1,
|
|
183
|
-
jitter: 0,
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
const expectedErr = new Error('user close handler explodes');
|
|
187
|
-
|
|
188
|
-
await new Promise((resolve) => {
|
|
189
|
-
client.on('handler-error', (err, event) => {
|
|
190
|
-
assert.strictEqual(err, expectedErr);
|
|
191
|
-
assert.strictEqual(event, 'close');
|
|
192
|
-
resolve();
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
// Simulate a handler throwing in a 'close' listener on the underlying model
|
|
196
|
-
models[0].emit('handler-error', expectedErr, 'close');
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
await client.close();
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it('promise recovery fails after max retries', async () => {
|
|
203
|
-
let attempts = 0;
|
|
204
|
-
|
|
205
|
-
function openModel() {
|
|
206
|
-
attempts++;
|
|
207
|
-
return Promise.reject(new Error('connect failed'));
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
let failed = false;
|
|
211
|
-
try {
|
|
212
|
-
await recovery.connectWithRecoveryPromise(openModel, {
|
|
213
|
-
initialDelay: 1,
|
|
214
|
-
maxDelay: 1,
|
|
215
|
-
jitter: 0,
|
|
216
|
-
maxRetries: 1,
|
|
217
|
-
});
|
|
218
|
-
} catch (err) {
|
|
219
|
-
failed = true;
|
|
220
|
-
assert.equal('connect failed', err.message);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
assert(failed);
|
|
224
|
-
assert.equal(2, attempts);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
describe('waitForConnect: false', () => {
|
|
228
|
-
function watchUnhandledRejections(t) {
|
|
229
|
-
const rejections = [];
|
|
230
|
-
const onRejection = (err) => rejections.push(err);
|
|
231
|
-
process.on('unhandledRejection', onRejection);
|
|
232
|
-
t.after(() => process.removeListener('unhandledRejection', onRejection));
|
|
233
|
-
return rejections;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
it('promise connect resolves before the first connection and later emits connect', async () => {
|
|
237
|
-
let opened = 0;
|
|
238
|
-
|
|
239
|
-
function openModel() {
|
|
240
|
-
return Promise.resolve(new FakePromiseModel(++opened));
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const client = await recovery.connectWithRecoveryPromise(openModel, {waitForConnect: false});
|
|
244
|
-
|
|
245
|
-
assert.equal(0, opened);
|
|
246
|
-
|
|
247
|
-
const model = await new Promise((resolve) => client.once('connect', resolve));
|
|
248
|
-
assert.equal(1, model.id);
|
|
249
|
-
assert.equal(1, opened);
|
|
250
|
-
|
|
251
|
-
await client.close();
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
it('listeners attached after connect observe a failed first attempt', async () => {
|
|
255
|
-
let attempts = 0;
|
|
256
|
-
|
|
257
|
-
function openModel() {
|
|
258
|
-
attempts++;
|
|
259
|
-
return attempts === 1 ? Promise.reject(new Error('broker unavailable')) : Promise.resolve(new FakePromiseModel(attempts));
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const client = await recovery.connectWithRecoveryPromise(openModel, {
|
|
263
|
-
initialDelay: 1,
|
|
264
|
-
maxDelay: 1,
|
|
265
|
-
jitter: 0,
|
|
266
|
-
waitForConnect: false,
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
const failures = [];
|
|
270
|
-
const scheduled = [];
|
|
271
|
-
client.on('connect-failed', (err) => failures.push(err));
|
|
272
|
-
client.on('reconnect-scheduled', (info) => scheduled.push(info));
|
|
273
|
-
|
|
274
|
-
await client.waitForConnect();
|
|
275
|
-
|
|
276
|
-
assert.equal(1, failures.length);
|
|
277
|
-
assert.equal('broker unavailable', failures[0].message);
|
|
278
|
-
assert.equal(1, scheduled.length);
|
|
279
|
-
assert.equal(1, scheduled[0].attempt);
|
|
280
|
-
assert.strictEqual(failures[0], scheduled[0].error);
|
|
281
|
-
assert.equal(2, attempts);
|
|
282
|
-
|
|
283
|
-
await client.close();
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
it('waitForConnect resolves with the recovering model once connected', async () => {
|
|
287
|
-
function openModel() {
|
|
288
|
-
return Promise.resolve(new FakePromiseModel(1));
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const client = await recovery.connectWithRecoveryPromise(openModel, {waitForConnect: false});
|
|
292
|
-
const connected = await client.waitForConnect();
|
|
293
|
-
|
|
294
|
-
assert.strictEqual(client, connected);
|
|
295
|
-
const ch = await client.createChannel();
|
|
296
|
-
assert.equal(1, ch.id);
|
|
297
|
-
|
|
298
|
-
await client.close();
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
it('close before the first connection cancels it without an unhandled rejection', async (t) => {
|
|
302
|
-
const rejections = watchUnhandledRejections(t);
|
|
303
|
-
let setupCalls = 0;
|
|
304
|
-
let closeCalls = 0;
|
|
305
|
-
let releaseOpen;
|
|
306
|
-
|
|
307
|
-
function openModel() {
|
|
308
|
-
return new Promise((resolve) => {
|
|
309
|
-
const model = new FakePromiseModel(1);
|
|
310
|
-
model.close = () => {
|
|
311
|
-
closeCalls++;
|
|
312
|
-
return Promise.resolve();
|
|
313
|
-
};
|
|
314
|
-
releaseOpen = () => resolve(model);
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
const client = await recovery.connectWithRecoveryPromise(openModel, {
|
|
319
|
-
waitForConnect: false,
|
|
320
|
-
setup() {
|
|
321
|
-
setupCalls++;
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
let connected = false;
|
|
326
|
-
client.on('connect', () => {
|
|
327
|
-
connected = true;
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
const pendingChannel = client.createChannel();
|
|
331
|
-
await new Promise((resolve) => setImmediate(resolve));
|
|
332
|
-
assert(releaseOpen, 'open should be in flight');
|
|
333
|
-
|
|
334
|
-
await client.close();
|
|
335
|
-
|
|
336
|
-
await assert.rejects(pendingChannel, {message: 'Connection closed'});
|
|
337
|
-
await assert.rejects(client.waitForConnect(), {message: 'Connection closed'});
|
|
338
|
-
|
|
339
|
-
releaseOpen();
|
|
340
|
-
await new Promise((resolve) => setImmediate(resolve));
|
|
341
|
-
|
|
342
|
-
assert.equal(0, setupCalls);
|
|
343
|
-
assert.equal(1, closeCalls);
|
|
344
|
-
assert.equal(false, connected);
|
|
345
|
-
assert.deepEqual([], rejections);
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
it('exhausting retries emits reconnect-failed without an unhandled rejection', async (t) => {
|
|
349
|
-
const rejections = watchUnhandledRejections(t);
|
|
350
|
-
let attempts = 0;
|
|
351
|
-
|
|
352
|
-
function openModel() {
|
|
353
|
-
attempts++;
|
|
354
|
-
return Promise.reject(new Error('connect failed'));
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const client = await recovery.connectWithRecoveryPromise(openModel, {
|
|
358
|
-
initialDelay: 1,
|
|
359
|
-
maxDelay: 1,
|
|
360
|
-
jitter: 0,
|
|
361
|
-
maxRetries: 1,
|
|
362
|
-
waitForConnect: false,
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
const err = await new Promise((resolve) => client.once('reconnect-failed', resolve));
|
|
366
|
-
assert.equal('connect failed', err.message);
|
|
367
|
-
assert.equal(2, attempts);
|
|
368
|
-
|
|
369
|
-
await assert.rejects(client.waitForConnect(), {message: 'connect failed'});
|
|
370
|
-
await new Promise((resolve) => setImmediate(resolve));
|
|
371
|
-
assert.deepEqual([], rejections);
|
|
372
|
-
|
|
373
|
-
await client.close();
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
it('callback connect invokes the callback before the first connection', (_t, done) => {
|
|
377
|
-
let opened = 0;
|
|
378
|
-
|
|
379
|
-
function openModel() {
|
|
380
|
-
return Promise.resolve(new FakeCallbackModel(++opened));
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
const failures = [];
|
|
384
|
-
const client = recovery.connectWithRecoveryCallback(openModel, {waitForConnect: false}, (err, c) => {
|
|
385
|
-
if (err) return done(err);
|
|
386
|
-
assert.strictEqual(client, c);
|
|
387
|
-
assert.equal(0, opened);
|
|
388
|
-
|
|
389
|
-
c.on('connect-failed', (failure) => failures.push(failure));
|
|
390
|
-
c.waitForConnect((waitErr, connected) => {
|
|
391
|
-
if (waitErr) return done(waitErr);
|
|
392
|
-
assert.strictEqual(client, connected);
|
|
393
|
-
assert.equal(1, opened);
|
|
394
|
-
assert.deepEqual([], failures);
|
|
395
|
-
c.close(done);
|
|
396
|
-
});
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
assert(client);
|
|
400
|
-
});
|
|
401
|
-
});
|
|
402
|
-
});
|