1
1
2
2
import { expect , test } from '@jest/globals'
3
3
import some from './some.mjs'
4
- import Deferred from './Deferred.mjs'
5
4
import { range } from 'itertools'
5
+ import Deferred from './Deferred.mjs'
6
+
7
+ test ( 'some compatibility' , async ( ) => {
8
+ const p = Promise . resolve ( )
9
+ let res = await some ( [ ...range ( 3 ) ] , async ( v ) => {
10
+ await p
11
+ return true
12
+ } , 1 )
13
+ expect ( res ) . toBe ( [ ...range ( 3 ) ] . some ( ( v ) => true ) )
6
14
7
- test ( 'some all no pass' , async ( ) => {
15
+ res = await some ( [ ...range ( 3 ) ] , async ( v ) => {
16
+ await p
17
+ return v !== 2
18
+ } , 1 )
19
+ expect ( res ) . toBe ( [ ...range ( 3 ) ] . some ( ( v ) => v !== 2 ) )
20
+
21
+ res = await some ( [ ...range ( 3 ) ] , async ( v ) => {
22
+ await p
23
+ return false
24
+ } , 1 )
25
+ expect ( res ) . toBe ( [ ...range ( 3 ) ] . some ( ( v ) => false ) )
26
+
27
+ res = await some ( [ ] , async ( v ) => {
28
+ await p
29
+ return false
30
+ } , 1 )
31
+ expect ( res ) . toBe ( [ ] . some ( ( v ) => false ) )
32
+
33
+ res = await some ( [ ] , async ( v ) => {
34
+ await p
35
+ return true
36
+ } , 1 )
37
+ expect ( res ) . toBe ( [ ] . some ( ( v ) => true ) )
38
+ } )
39
+
40
+ test ( 'some parallel' , async ( ) => {
41
+ const p = Promise . resolve ( )
42
+ let res = await some ( [ ...range ( 3 ) ] , async ( v ) => {
43
+ await p
44
+ return true
45
+ } , 10 )
46
+ expect ( res ) . toBe ( [ ...range ( 3 ) ] . some ( ( v ) => true ) )
47
+
48
+ res = await some ( [ ...range ( 3 ) ] , async ( v ) => {
49
+ await p
50
+ return v !== 2
51
+ } , 10 )
52
+ expect ( res ) . toBe ( [ ...range ( 3 ) ] . some ( ( v ) => v !== 2 ) )
53
+
54
+ res = await some ( [ ...range ( 3 ) ] , async ( v ) => {
55
+ await p
56
+ return false
57
+ } , 10 )
58
+ expect ( res ) . toBe ( [ ...range ( 3 ) ] . some ( ( v ) => false ) )
59
+
60
+ res = await some ( [ ] , async ( v ) => {
61
+ await p
62
+ return false
63
+ } , 10 )
64
+ expect ( res ) . toBe ( [ ] . some ( ( v ) => false ) )
65
+
66
+ res = await some ( [ ] , async ( v ) => {
67
+ await p
68
+ return true
69
+ } , 10 )
70
+ expect ( res ) . toBe ( [ ] . some ( ( v ) => true ) )
71
+ } )
72
+
73
+ test ( 'some first in time' , async ( ) => {
74
+ const ds = [ ...range ( 3 ) ] . map ( ( ) => new Deferred ( ) )
75
+ const p = some ( range ( 3 ) , async ( v , i ) => {
76
+ await ds [ i ]
77
+ return true
78
+ } , 3 )
79
+ ds [ 2 ] . resolve ( )
80
+ const res = await p
81
+ expect ( res ) . toBe ( true )
82
+ } )
83
+
84
+ test ( 'some infinite concurrency all no pass' , async ( ) => {
8
85
const callCount = { }
9
86
; [ ...range ( 3 ) ] . forEach ( ( i ) => { callCount [ i ] = 0 } )
10
87
const d = new Deferred ( )
@@ -14,7 +91,7 @@ test('some all no pass', async () => {
14
91
ds [ i ] . resolve ( )
15
92
await d . promise
16
93
return false
17
- } )
94
+ } , Number . POSITIVE_INFINITY )
18
95
await ds [ 2 ] . promise
19
96
expect ( callCount [ 0 ] ) . toBe ( 1 )
20
97
expect ( callCount [ 1 ] ) . toBe ( 1 )
@@ -27,7 +104,7 @@ test('some all no pass', async () => {
27
104
expect ( callCount [ 2 ] ) . toBe ( 1 )
28
105
} )
29
106
30
- test ( 'some some pass' , async ( ) => {
107
+ test ( 'some infinite concurrency some pass' , async ( ) => {
31
108
const callCount = { }
32
109
; [ ...range ( 3 ) ] . forEach ( ( i ) => { callCount [ i ] = 0 } )
33
110
const d = new Deferred ( )
@@ -41,7 +118,7 @@ test('some some pass', async () => {
41
118
} else {
42
119
return false
43
120
}
44
- } )
121
+ } , Number . POSITIVE_INFINITY )
45
122
await ds [ 2 ] . promise
46
123
expect ( callCount [ 0 ] ) . toBe ( 1 )
47
124
expect ( callCount [ 1 ] ) . toBe ( 1 )
@@ -53,3 +130,53 @@ test('some some pass', async () => {
53
130
expect ( callCount [ 1 ] ) . toBe ( 1 )
54
131
expect ( callCount [ 2 ] ) . toBe ( 1 )
55
132
} )
133
+
134
+ test ( 'some concurrency 1 all no pass' , async ( ) => {
135
+ const callCount = { }
136
+ ; [ ...range ( 3 ) ] . forEach ( ( i ) => { callCount [ i ] = 0 } )
137
+ const d = new Deferred ( )
138
+ const ds = [ ...range ( 3 ) ] . map ( ( ) => new Deferred ( ) )
139
+ const p = some ( [ ...range ( 3 ) ] , async ( v , i ) => {
140
+ callCount [ i ] += 1
141
+ ds [ i ] . resolve ( )
142
+ await d . promise
143
+ return false
144
+ } )
145
+ await ds [ 0 ] . promise
146
+ expect ( callCount [ 0 ] ) . toBe ( 1 )
147
+ expect ( callCount [ 1 ] ) . toBe ( 0 )
148
+ expect ( callCount [ 2 ] ) . toBe ( 0 )
149
+ d . resolve ( )
150
+ const res = await p
151
+ expect ( res ) . toBe ( false )
152
+ expect ( callCount [ 0 ] ) . toBe ( 1 )
153
+ expect ( callCount [ 1 ] ) . toBe ( 1 )
154
+ expect ( callCount [ 2 ] ) . toBe ( 1 )
155
+ } )
156
+
157
+ test ( 'some concurrency 1 some pass' , async ( ) => {
158
+ const callCount = { }
159
+ ; [ ...range ( 3 ) ] . forEach ( ( i ) => { callCount [ i ] = 0 } )
160
+ const d = new Deferred ( )
161
+ const ds = [ ...range ( 3 ) ] . map ( ( ) => new Deferred ( ) )
162
+ const p = some ( [ ...range ( 3 ) ] , async ( v , i ) => {
163
+ callCount [ i ] += 1
164
+ ds [ i ] . resolve ( )
165
+ await d . promise
166
+ if ( i === 1 ) {
167
+ return true
168
+ } else {
169
+ return false
170
+ }
171
+ } )
172
+ await ds [ 0 ] . promise
173
+ expect ( callCount [ 0 ] ) . toBe ( 1 )
174
+ expect ( callCount [ 1 ] ) . toBe ( 0 )
175
+ expect ( callCount [ 2 ] ) . toBe ( 0 )
176
+ d . resolve ( )
177
+ const res = await p
178
+ expect ( res ) . toBe ( true )
179
+ expect ( callCount [ 0 ] ) . toBe ( 1 )
180
+ expect ( callCount [ 1 ] ) . toBe ( 1 )
181
+ expect ( callCount [ 2 ] ) . toBe ( 0 )
182
+ } )
0 commit comments