Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions xdk-gen/src/typescript/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ language! {
render "test_pagination" => "tests/{}/test_pagination.test.ts"
},
render "test_client_errors" => "tests/client_errors.test.ts",
render "test_event_driven_stream" => "tests/event_driven_stream.test.ts",
render "jest.config" => "jest.config.cjs"
]
}
Expand Down
4 changes: 3 additions & 1 deletion xdk-gen/templates/typescript/event_driven_stream.j2
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ export class EventDrivenStream {
this.on(StreamEvent.Close, closeListener);

try {
while (!isComplete && !hasError) {
// Keep draining until the stream has ended AND the queue is empty, so
// events that arrived just before a close/error are not dropped.
while (dataQueue.length > 0 || (!isComplete && !hasError)) {
if (dataQueue.length > 0) {
yield dataQueue.shift()!;
} else {
Expand Down
89 changes: 89 additions & 0 deletions xdk-gen/templates/typescript/test_event_driven_stream.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* AUTO-GENERATED FILE - DO NOT EDIT
* This file was automatically generated by the XDK build tool.
* Any manual changes will be overwritten on the next generation.
*
* Auto-generated tests for the EventDrivenStream async iterator. They check
* that events which were already queued when the connection closed or failed
* are still delivered to the consumer before the iterator finishes.
*
* Generated automatically - do not edit manually.
*/

import { describe, it, expect, afterEach, jest } from '@jest/globals';
import { EventDrivenStream } from '../src/stream/event_driven_stream.js';

const encoder = new TextEncoder();

const NDJSON_3_POSTS = [1, 2, 3]
.map(id => JSON.stringify({ data: { id: String(id), text: `post ${id}` } }))
.join('\n') + '\n';

/**
* Connects a stream to a controllable ReadableStream and starts the async
* iterator (so its listeners are attached) before any events are produced.
*/
const connect = async () => {
let controller!: ReadableStreamDefaultController<Uint8Array>;
const source = new ReadableStream<Uint8Array>({
start(c) {
controller = c;
},
});
const stream = new EventDrivenStream();
await stream.connect(source);
const iterator = stream[Symbol.asyncIterator]();
const first = iterator.next();
return { controller, stream, iterator, first };
};

const nextTick = () => new Promise(resolve => setTimeout(resolve, 0));

describe('EventDrivenStream async iterator', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('yields every queued event when the stream closes right after the data', async () => {
const { controller, iterator, first } = await connect();

// Three posts arrive in one chunk and the server ends the response.
controller.enqueue(encoder.encode(NDJSON_3_POSTS));
controller.close();

const ids: string[] = [];
let result = await first;
while (!result.done) {
ids.push(result.value.data.id);
result = await iterator.next();
}

expect(ids).toEqual(['1', '2', '3']);
});

it('yields every queued event before surfacing a stream error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
const { controller, iterator, first } = await connect();

controller.enqueue(encoder.encode(NDJSON_3_POSTS));
// Let the reader consume the chunk before the stream fails.
await nextTick();
controller.error(new Error('connection reset'));

const ids: string[] = [];
let thrown: unknown = null;
try {
let result = await first;
while (!result.done) {
ids.push(result.value.data.id);
result = await iterator.next();
}
} catch (error) {
thrown = error;
}

expect(ids).toEqual(['1', '2', '3']);
expect(thrown).toBeInstanceOf(Error);
expect((thrown as Error).message).toBe('connection reset');
});
});