Skip to content

Commit

Permalink
test(action): added custom concurrency test case
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Dec 12, 2022
1 parent cf73ef0 commit e1fe050
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions extensions/action/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ describe('Actions Extension', () => {

const abortFn = vi.fn();

const concurrentAction = action('concurrent-action', async () => {});
const concurrentAction = action('concurrent-action', async () => sleep(100));
const nonConcurrentAction = action('non-concurrent-action', async (_, __, ___, onAbort) => {
onAbort(abortFn);
return sleep(100);
}, {
concurrent: false,
});
Expand Down Expand Up @@ -243,6 +244,42 @@ describe('Actions Extension', () => {
expect(abortFn).toHaveBeenCalled();
});

test('Handles custom concurrency methods', async () => {
expect.assertions(2);

const {
action,
} = instance.store;

const customConcurrentAction = action('concurrent-action', async (payload: number) => sleep(100), {
concurrent: (payload, runningPayloads) => !runningPayloads.includes(payload),
});

let hasCustomConcurrentFailed = false;

try {
await Promise.all([
customConcurrentAction(1),
customConcurrentAction(2),
]);
} catch {
hasCustomConcurrentFailed = true;
}

expect(hasCustomConcurrentFailed).toBe(false);

try {
await Promise.all([
customConcurrentAction(2),
customConcurrentAction(2),
]);
} catch {
hasCustomConcurrentFailed = true;
}

expect(hasCustomConcurrentFailed).toBe(true);
});

test('Handles nested cancellation', async () => {
expect.assertions(7);

Expand All @@ -254,8 +291,8 @@ describe('Actions Extension', () => {

const catchAssertion = vi.fn();

const childAction1 = action('child1', () => sleep(1000));
const childAction2 = action('child2', () => sleep(1000));
const childAction1 = action('child1', () => sleep(100));
const childAction2 = action('child2', () => sleep(100));
const parentAction = action('parent', (_, __, controller) => Promise.all([
childAction1(_, controller),
childAction2(_, controller),
Expand Down

0 comments on commit e1fe050

Please sign in to comment.