Skip to content

Commit

Permalink
refactor: callback for nextTick
Browse files Browse the repository at this point in the history
test: wait duration ms to check end

chore: eslint

test: end before timeout
  • Loading branch information
nyapat committed Aug 25, 2024
1 parent 674d4a3 commit d6a380e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
50 changes: 33 additions & 17 deletions packages/voice/__tests__/AudioReceiveStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,27 @@ describe('AudioReceiveStream', () => {
await wait(200);
stream.push(DUMMY_BUFFER);
expect(stream.readable).toEqual(true);
stream.push(null);
await wait(200);
expect(stream.readable).toEqual(false);
});

// TODO: Fix this test
// test('AfterSilence end behavior', async () => {
// const duration = 100;
// const increment = 20;

// const stream = new AudioReceiveStream({ end: { behavior: EndBehaviorType.AfterSilence, duration: 100 } });
// stream.resume();
test('AfterSilence end behavior', async () => {
const duration = 100;
const increment = 20;

// for (let i = increment; i < duration / 2; i += increment) {
// await stepSilence(stream, increment);
// }
const stream = new AudioReceiveStream({ end: { behavior: EndBehaviorType.AfterSilence, duration } });
stream.resume();

// stream.push(DUMMY_BUFFER);
for (let step = increment; step < duration / 2; step += increment) {
await stepSilence(stream, increment);
}

// for (let i = increment; i < duration; i += increment) {
// await stepSilence(stream, increment);
// }
stream.push(DUMMY_BUFFER);

// await wait(increment);
// expect(stream.readableEnded).toEqual(true);
// });
await wait(duration);
expect(stream.readableEnded).toEqual(true);
});

test('AfterInactivity end behavior', async () => {
const duration = 100;
Expand All @@ -71,4 +69,22 @@ describe('AudioReceiveStream', () => {

expect(stream.readableEnded).toEqual(true);
});

test('Stream ends after pushing null', async () => {
const stream = new AudioReceiveStream({ end: { behavior: EndBehaviorType.AfterInactivity, duration: 100 } });
stream.resume();

stream.push(DUMMY_BUFFER);

expect(stream.readable).toEqual(true);
expect(stream.readableEnded).toEqual(false);
expect(stream.destroyed).toEqual(false);

stream.push(null);
await wait(50);

expect(stream.readable).toEqual(false);
expect(stream.readableEnded).toEqual(true);
expect(stream.destroyed).toEqual(true);
});
});
7 changes: 3 additions & 4 deletions packages/voice/src/receive/AudioReceiveStream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Buffer } from 'node:buffer';
import { nextTick } from 'node:process';
import { Readable, type ReadableOptions } from 'node:stream';
import { SILENCE_FRAME } from '../audio/AudioPlayer';

Expand Down Expand Up @@ -74,14 +75,12 @@ export class AudioReceiveStream extends Readable {
this.renewEndTimeout(this.end);
}

const result = super.push(buffer);

if (buffer === null) {
// null marks EOF for stream
this.destroy();
nextTick(() => this.destroy());
}

return result;
return super.push(buffer);
}

private renewEndTimeout(end: EndBehavior & { duration: number }) {
Expand Down

0 comments on commit d6a380e

Please sign in to comment.