Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reintroduce incremental delivery support for subscriptions #4314

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
219 changes: 219 additions & 0 deletions src/execution/__tests__/flattenAsyncIterable-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { expectPromise } from '../../__testUtils__/expectPromise.js';

import { flattenAsyncIterable } from '../flattenAsyncIterable.js';

describe('flattenAsyncIterable', () => {
it('flatten nested async generators', async () => {
async function* source() {
yield await Promise.resolve({
value: 1,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(1.1);
yield await Promise.resolve(1.2);
})(),
});
yield await Promise.resolve({
value: 2,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(2.1);
yield await Promise.resolve(2.2);
})(),
});
}

const doubles = flattenAsyncIterable(source(), (item) => item);

const result = [];
for await (const x of doubles) {
result.push(x);
}
expect(result).to.deep.equal([1, 1.1, 1.2, 2, 2.1, 2.2]);
});

it('passes through errors from a nested async generator', async () => {
async function* source() {
yield await Promise.resolve({
value: 1,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(1.1);
yield await Promise.resolve(1.2);
})(),
});
throw new Error('ouch');
}
/* c8 ignore stop */

const doubles = flattenAsyncIterable(source(), (item) => item);

expect(await doubles.next()).to.deep.equal({ value: 1, done: false });
expect(await doubles.next()).to.deep.equal({ value: 1.1, done: false });
expect(await doubles.next()).to.deep.equal({ value: 1.2, done: false });

await expectPromise(doubles.next()).toRejectWith('ouch');
});

it('allows returning early from a nested async generator', async () => {
async function* source() {
yield await Promise.resolve({
value: 1,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(1.1);
yield await Promise.resolve(1.2);
})(),
});
yield await Promise.resolve({
value: 2,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(2.1); /* c8 ignore start */
// Not reachable, early return
yield await Promise.resolve(2.2);
})(),
});
// Not reachable, early return
yield await Promise.resolve({
value: 3,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(3.1);
yield await Promise.resolve(3.2);
})(),
});
}
/* c8 ignore stop */

const doubles = flattenAsyncIterable(source(), (item) => item);

expect(await doubles.next()).to.deep.equal({ value: 1, done: false });
expect(await doubles.next()).to.deep.equal({ value: 1.1, done: false });
expect(await doubles.next()).to.deep.equal({ value: 1.2, done: false });
expect(await doubles.next()).to.deep.equal({ value: 2, done: false });
expect(await doubles.next()).to.deep.equal({ value: 2.1, done: false });

// Early return
expect(await doubles.return()).to.deep.equal({
value: undefined,
done: true,
});

// Subsequent next calls
expect(await doubles.next()).to.deep.equal({
value: undefined,
done: true,
});
expect(await doubles.next()).to.deep.equal({
value: undefined,
done: true,
});
});

it('allows throwing errors into a nested async generator', async () => {
async function* source() {
yield await Promise.resolve({
value: 1,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(1.1);
yield await Promise.resolve(1.2);
})(),
});
yield await Promise.resolve({
value: 2,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(2.1); /* c8 ignore start */
// Not reachable, early return
yield await Promise.resolve(2.2);
})(),
});
// Not reachable, early return
yield await Promise.resolve({ value: 3 });
}
/* c8 ignore stop */

const doubles = flattenAsyncIterable(source(), (item) => item);

expect(await doubles.next()).to.deep.equal({ value: 1, done: false });
expect(await doubles.next()).to.deep.equal({ value: 1.1, done: false });
expect(await doubles.next()).to.deep.equal({ value: 1.2, done: false });
expect(await doubles.next()).to.deep.equal({ value: 2, done: false });
expect(await doubles.next()).to.deep.equal({ value: 2.1, done: false });

// Throw error
await expectPromise(doubles.throw(new Error('ouch'))).toRejectWith('ouch');
});

it('completely yields sub-iterables even when next() called in parallel', async () => {
async function* source() {
yield await Promise.resolve({
value: 1,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(1.1);
yield await Promise.resolve(1.2);
})(),
});
yield await Promise.resolve({
value: 2,
nestedIterable: (async function* nested(): AsyncGenerator<
number,
void,
void
> {
yield await Promise.resolve(2.1);
yield await Promise.resolve(2.2);
})(),
});
}

const result = flattenAsyncIterable(source(), (item) => item);

const promise1 = result.next();
const promise2 = result.next();
const promise3 = result.next();
expect(await promise1).to.deep.equal({ value: 1, done: false });
expect(await promise2).to.deep.equal({ value: 1.1, done: false });
expect(await promise3).to.deep.equal({ value: 1.2, done: false });
expect(await result.next()).to.deep.equal({ value: 2, done: false });
expect(await result.next()).to.deep.equal({ value: 2.1, done: false });
expect(await result.next()).to.deep.equal({ value: 2.2, done: false });
expect(await result.next()).to.deep.equal({
value: undefined,
done: true,
});
});
});
Loading
Loading