Skip to content

Commit 0f5c305

Browse files
authored
chore: fix mplex tests (#2234)
The timing has changed on these tests has changed to do a release of `it-foreach` which now does not introduce artificial async when calling the each method.
1 parent 17d980c commit 0f5c305

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

packages/stream-multiplexer-mplex/test/fixtures/utils.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,55 @@ export function messageWithBytes (msg: Message): Message | MessageWithBytes {
1616

1717
return msg
1818
}
19+
20+
export function arrayToGenerator <T> (data: T[]): AsyncGenerator<T, void, unknown> {
21+
let done: Error | boolean = false
22+
let index = -1
23+
24+
const generator: AsyncGenerator<T, void, unknown> = {
25+
[Symbol.asyncIterator]: () => {
26+
return generator
27+
},
28+
async next () {
29+
if (done instanceof Error) {
30+
throw done
31+
}
32+
33+
index++
34+
35+
if (index === data.length) {
36+
done = true
37+
}
38+
39+
if (done) {
40+
return {
41+
done: true,
42+
value: undefined
43+
}
44+
}
45+
46+
return {
47+
done: false,
48+
value: data[index]
49+
}
50+
},
51+
async return (): Promise<IteratorReturnResult<void>> {
52+
done = true
53+
54+
return {
55+
done: true,
56+
value: undefined
57+
}
58+
},
59+
async throw (err: Error): Promise<IteratorReturnResult<void>> {
60+
done = err
61+
62+
return {
63+
done: true,
64+
value: undefined
65+
}
66+
}
67+
}
68+
69+
return generator
70+
}

packages/stream-multiplexer-mplex/test/stream.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Uint8ArrayList } from 'uint8arraylist'
1313
import { fromString as uint8ArrayFromString } from 'uint8arrays'
1414
import { MessageTypes, MessageTypeNames } from '../src/message-types.js'
1515
import { createStream } from '../src/stream.js'
16-
import { messageWithBytes } from './fixtures/utils.js'
16+
import { arrayToGenerator, messageWithBytes } from './fixtures/utils.js'
1717
import type { Message } from '../src/message-types.js'
1818
import type { MplexStream } from '../src/stream.js'
1919

@@ -95,7 +95,7 @@ async function streamPair (n: number, onInitiatorMessage?: onMessage, onReceiver
9595

9696
try {
9797
await pipe(
98-
input,
98+
arrayToGenerator(input),
9999
initiator,
100100
(source) => map(source, buf => {
101101
const msg: Message = bufferToMessage(buf)
@@ -392,7 +392,6 @@ describe('stream', () => {
392392
MessageTypes.MESSAGE_INITIATOR,
393393
MessageTypes.MESSAGE_INITIATOR,
394394
MessageTypes.MESSAGE_INITIATOR,
395-
MessageTypes.MESSAGE_INITIATOR,
396395
MessageTypes.MESSAGE_INITIATOR
397396
])
398397

@@ -456,7 +455,6 @@ describe('stream', () => {
456455
MessageTypes.MESSAGE_INITIATOR,
457456
MessageTypes.MESSAGE_INITIATOR,
458457
MessageTypes.MESSAGE_INITIATOR,
459-
MessageTypes.MESSAGE_INITIATOR,
460458
MessageTypes.MESSAGE_INITIATOR
461459
])
462460

0 commit comments

Comments
 (0)