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

Abort signal is ignored when iterable doesn't yield anything #96

Open
gnarea opened this issue Oct 3, 2023 · 1 comment
Open

Abort signal is ignored when iterable doesn't yield anything #96

gnarea opened this issue Oct 3, 2023 · 1 comment

Comments

@gnarea
Copy link

gnarea commented Oct 3, 2023

The first test times out and the second passes:

import { PassThrough } from 'stream';

import { source as makeSourceAbortable } from 'abortable-iterator';

test('Empty source', async () => {
  const source = new PassThrough();
  const abortController = new AbortController();
  const abortableSource = makeSourceAbortable(source, abortController.signal, {
    returnOnAbort: true,
  });
  setImmediate(() => abortController.abort());
  for await (const chunk of abortableSource) {
    throw new Error(`Shouldn't have got ${chunk}`);
  }
});

test('Non-empty source', async () => {
  const source = new PassThrough();
  const abortController = new AbortController();
  const abortableSource = makeSourceAbortable(source, abortController.signal, {
    returnOnAbort: true,
  });
  setImmediate(() => {
    source.write('foo');
    abortController.abort();
  });
  for await (const chunk of abortableSource) {
    expect(chunk).toBe('foo');
  }
});
@achingbrain
Copy link
Collaborator

Is this still a problem? I've refactored the first test to not have any node-specific code and it passes:

import all from 'it-all'
import delay from 'delay'
import { abortableSource } from 'abortable-source'
import { expect } from 'aegir/chai'

it('should return with an empty source', async () => {
  const abortController = new AbortController();
  const source = abortableSource(async function * () {
    await delay(1000)
    yield 'foo'
  }(), abortController.signal, {
    returnOnAbort: true,
  })

  setTimeout(() => {
    abortController.abort()
  }, 0)

  // should be empty since the signal was aborted before any value was yielded
  const result = await all(source)
  expect(result).to.have.lengthOf(0)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants