amqplib 2.2.0 → 2.3.0

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/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
- });