@@ -2,81 +2,81 @@ const branchy = require('../lib/branchy')
2
2
3
3
const sleep = ms => new Promise ( resolve => setTimeout ( resolve , ms ) )
4
4
5
- test ( 'handles process numbers below threshold concurrently' , ( ) => {
5
+ test ( 'handles process numbers below threshold concurrently' , async ( ) => {
6
6
const results = [ ]
7
7
const sleeper = branchy ( sleep , { concurrent : 2 } )
8
8
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 ] )
15
15
} )
16
16
17
- test ( 'respects call priority' , ( ) => {
17
+ test ( 'respects call priority' , async ( ) => {
18
18
const results = [ ]
19
19
const sleeper = branchy ( sleep , {
20
20
concurrent : { threads : 1 , priority : num => num }
21
21
} )
22
22
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 ] )
29
29
} )
30
30
31
- test ( 'handles process numbers above threshold sequentially' , ( ) => {
31
+ test ( 'handles process numbers above threshold sequentially' , async ( ) => {
32
32
const results = [ ]
33
33
const sleeper = branchy ( sleep , { concurrent : 1 } )
34
34
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 ] )
41
41
} )
42
42
43
- test ( 'handles concurrency threshold independently' , ( ) => {
43
+ test ( 'handles concurrency threshold independently' , async ( ) => {
44
44
const results = [ ]
45
45
const sleeper1 = branchy ( sleep , { concurrent : 1 } )
46
46
const sleeper2 = branchy ( sleep , { concurrent : 1 } )
47
47
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 ] )
54
54
} )
55
55
56
- test ( 'handles stacks correctly' , ( ) => {
56
+ test ( 'handles stacks correctly' , async ( ) => {
57
57
const results = [ ]
58
58
const sleeper = branchy ( sleep , {
59
59
concurrent : { threads : 1 , strategy : 'stack' }
60
60
} )
61
61
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 ] )
68
68
} )
69
69
70
- test ( 'handles shared context correctly' , ( ) => {
70
+ test ( 'handles shared context correctly' , async ( ) => {
71
71
const results = [ ]
72
72
const context = branchy . createContext ( { concurrent : 1 } )
73
73
const sleeper1 = branchy ( sleep , { concurrent : context } )
74
74
const sleeper2 = branchy ( sleep , { concurrent : context } )
75
75
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 ] )
82
82
} )
0 commit comments