diff --git a/test/concurrency.spec.js b/test/concurrency.spec.js index 75f5fd5..cabd37c 100644 --- a/test/concurrency.spec.js +++ b/test/concurrency.spec.js @@ -2,81 +2,81 @@ const branchy = require('../lib/branchy') const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)) -test('handles process numbers below threshold concurrently', () => { +test('handles process numbers below threshold concurrently', async () => { const results = [] const sleeper = branchy(sleep, { concurrent: 2 }) - return Promise.all([ - sleeper(300).then(() => results.push(1)), - sleeper(0).then(() => results.push(2)) - ]).then(() => { - expect(results).toEqual([2, 1]) - }) + const call1 = sleeper(300).then(() => results.push(1)) + const call2 = sleeper(0).then(() => results.push(2)) + + await Promise.all([call1, call2]) + + expect(results).toEqual([2, 1]) }) -test('respects call priority', () => { +test('respects call priority', async () => { const results = [] const sleeper = branchy(sleep, { concurrent: { threads: 1, priority: num => num } }) - return Promise.all([ - sleeper(300).then(() => results.push(1)), - sleeper(0).then(() => results.push(2)) - ]).then(() => { - expect(results).toEqual([1, 2]) - }) + const call1 = sleeper(300).then(() => results.push(1)) + const call2 = sleeper(0).then(() => results.push(2)) + + await Promise.all([call1, call2]) + + expect(results).toEqual([1, 2]) }) -test('handles process numbers above threshold sequentially', () => { +test('handles process numbers above threshold sequentially', async () => { const results = [] const sleeper = branchy(sleep, { concurrent: 1 }) - return Promise.all([ - sleeper(300).then(() => results.push(1)), - sleeper(0).then(() => results.push(2)) - ]).then(() => { - expect(results).toEqual([1, 2]) - }) + const call1 = sleeper(300).then(() => results.push(1)) + const call2 = sleeper(0).then(() => results.push(2)) + + await Promise.all([call1, call2]) + + expect(results).toEqual([1, 2]) }) -test('handles concurrency threshold independently', () => { +test('handles concurrency threshold independently', async () => { const results = [] const sleeper1 = branchy(sleep, { concurrent: 1 }) const sleeper2 = branchy(sleep, { concurrent: 1 }) - return Promise.all([ - sleeper1(300).then(() => results.push(1)), - sleeper2(0).then(() => results.push(2)) - ]).then(() => { - expect(results).toEqual([2, 1]) - }) + const call1 = sleeper1(300).then(() => results.push(1)) + const call2 = sleeper2(0).then(() => results.push(2)) + + await Promise.all([call1, call2]) + + expect(results).toEqual([2, 1]) }) -test('handles stacks correctly', () => { +test('handles stacks correctly', async () => { const results = [] const sleeper = branchy(sleep, { concurrent: { threads: 1, strategy: 'stack' } }) - return Promise.all([ - sleeper(0).then(() => results.push(1)), - sleeper(300).then(() => results.push(2)) - ]).then(() => { - expect(results).toEqual([2, 1]) - }) + const call1 = sleeper(0).then(() => results.push(1)) + const call2 = sleeper(300).then(() => results.push(2)) + + await Promise.all([call1, call2]) + + expect(results).toEqual([2, 1]) }) -test('handles shared context correctly', () => { +test('handles shared context correctly', async () => { const results = [] const context = branchy.createContext({ concurrent: 1 }) const sleeper1 = branchy(sleep, { concurrent: context }) const sleeper2 = branchy(sleep, { concurrent: context }) - return Promise.all([ - sleeper1(300).then(() => results.push(1)), - sleeper2(0).then(() => results.push(2)) - ]).then(() => { - expect(results).toEqual([1, 2]) - }) + const call1 = sleeper1(300).then(() => results.push(1)) + const call2 = sleeper2(0).then(() => results.push(2)) + + await Promise.all([call1, call2]) + + expect(results).toEqual([1, 2]) })