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

Fix setTimeout error #32

Merged
merged 2 commits into from
Jan 12, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,27 @@ it('test finalizer deletes queries with no subscribers', () => {
});

it('producers can run a finalizer when the last subscriber unsubscribes', (done) => {
const workerGroup: RequestProcessorWorkerGroup<string, number> = new RequestProcessorWorkerGroup<string, number>(
const recorder: SubscriberRecorder<number> = new SubscriberRecorder<number>();
let workerGroup: RequestProcessorWorkerGroup<string, number>;

function unsubscribe() {
expect(recorder.peek()).toEqual(5);
recorder.unsubscribe();
// expect no workers to remain because finalizer ran to delete the worker
expect(workerGroup.size()).toEqual(0);
}

workerGroup = new RequestProcessorWorkerGroup<string, number>(
(query) => {
let timeoutID: any;
let counter = 0;
return new Observable<number>((subscriber) => {
timeoutID = setInterval(function incrementer() {
counter++;
subscriber.next(counter);
if (counter === 5) {
unsubscribe();
}
}, 5);
}).pipe(
finalize(() => {
Expand All @@ -230,14 +243,6 @@ it('producers can run a finalizer when the last subscriber unsubscribes', (done)
identity
);

const recorder: SubscriberRecorder<number> = new SubscriberRecorder<number>();
workerGroup.subscribe('test', recorder);
expect(workerGroup.size()).toEqual(1);

setTimeout(() => {
expect(recorder.peek()).toBeGreaterThan(1);
recorder.unsubscribe();
// expect no workers to remain because finalizer ran to delete the worker
expect(workerGroup.size()).toEqual(0);
}, 25);
});