Skip to content

Commit afd4e43

Browse files
committed
Use async/await in tests
1 parent ff53e16 commit afd4e43

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

test/concurrency.spec.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,81 @@ const branchy = require('../lib/branchy')
22

33
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
44

5-
test('handles process numbers below threshold concurrently', () => {
5+
test('handles process numbers below threshold concurrently', async () => {
66
const results = []
77
const sleeper = branchy(sleep, { concurrent: 2 })
88

9-
return Promise.all([
10-
sleeper(300).then(() => results.push(1)),
11-
sleeper(0).then(() => results.push(2))
12-
]).then(() => {
13-
expect(results).toEqual([2, 1])
14-
})
9+
const call1 = sleeper(300).then(() => results.push(1))
10+
const call2 = sleeper(0).then(() => results.push(2))
11+
12+
await Promise.all([call1, call2])
13+
14+
expect(results).toEqual([2, 1])
1515
})
1616

17-
test('respects call priority', () => {
17+
test('respects call priority', async () => {
1818
const results = []
1919
const sleeper = branchy(sleep, {
2020
concurrent: { threads: 1, priority: num => num }
2121
})
2222

23-
return Promise.all([
24-
sleeper(300).then(() => results.push(1)),
25-
sleeper(0).then(() => results.push(2))
26-
]).then(() => {
27-
expect(results).toEqual([1, 2])
28-
})
23+
const call1 = sleeper(300).then(() => results.push(1))
24+
const call2 = sleeper(0).then(() => results.push(2))
25+
26+
await Promise.all([call1, call2])
27+
28+
expect(results).toEqual([1, 2])
2929
})
3030

31-
test('handles process numbers above threshold sequentially', () => {
31+
test('handles process numbers above threshold sequentially', async () => {
3232
const results = []
3333
const sleeper = branchy(sleep, { concurrent: 1 })
3434

35-
return Promise.all([
36-
sleeper(300).then(() => results.push(1)),
37-
sleeper(0).then(() => results.push(2))
38-
]).then(() => {
39-
expect(results).toEqual([1, 2])
40-
})
35+
const call1 = sleeper(300).then(() => results.push(1))
36+
const call2 = sleeper(0).then(() => results.push(2))
37+
38+
await Promise.all([call1, call2])
39+
40+
expect(results).toEqual([1, 2])
4141
})
4242

43-
test('handles concurrency threshold independently', () => {
43+
test('handles concurrency threshold independently', async () => {
4444
const results = []
4545
const sleeper1 = branchy(sleep, { concurrent: 1 })
4646
const sleeper2 = branchy(sleep, { concurrent: 1 })
4747

48-
return Promise.all([
49-
sleeper1(300).then(() => results.push(1)),
50-
sleeper2(0).then(() => results.push(2))
51-
]).then(() => {
52-
expect(results).toEqual([2, 1])
53-
})
48+
const call1 = sleeper1(300).then(() => results.push(1))
49+
const call2 = sleeper2(0).then(() => results.push(2))
50+
51+
await Promise.all([call1, call2])
52+
53+
expect(results).toEqual([2, 1])
5454
})
5555

56-
test('handles stacks correctly', () => {
56+
test('handles stacks correctly', async () => {
5757
const results = []
5858
const sleeper = branchy(sleep, {
5959
concurrent: { threads: 1, strategy: 'stack' }
6060
})
6161

62-
return Promise.all([
63-
sleeper(0).then(() => results.push(1)),
64-
sleeper(300).then(() => results.push(2))
65-
]).then(() => {
66-
expect(results).toEqual([2, 1])
67-
})
62+
const call1 = sleeper(0).then(() => results.push(1))
63+
const call2 = sleeper(300).then(() => results.push(2))
64+
65+
await Promise.all([call1, call2])
66+
67+
expect(results).toEqual([2, 1])
6868
})
6969

70-
test('handles shared context correctly', () => {
70+
test('handles shared context correctly', async () => {
7171
const results = []
7272
const context = branchy.createContext({ concurrent: 1 })
7373
const sleeper1 = branchy(sleep, { concurrent: context })
7474
const sleeper2 = branchy(sleep, { concurrent: context })
7575

76-
return Promise.all([
77-
sleeper1(300).then(() => results.push(1)),
78-
sleeper2(0).then(() => results.push(2))
79-
]).then(() => {
80-
expect(results).toEqual([1, 2])
81-
})
76+
const call1 = sleeper1(300).then(() => results.push(1))
77+
const call2 = sleeper2(0).then(() => results.push(2))
78+
79+
await Promise.all([call1, call2])
80+
81+
expect(results).toEqual([1, 2])
8282
})

0 commit comments

Comments
 (0)