@@ -6,13 +6,13 @@ import { range } from 'itertools'
6
6
7
7
test ( 'map base' , async ( ) => {
8
8
const arr = [ ...range ( 6 ) ]
9
- const res = await map ( arr , async ( x ) => x * 2 )
9
+ const res = await map ( arr , async ( x ) => x * 2 , 2 )
10
10
expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
11
11
} )
12
12
13
13
test ( 'map no async' , async ( ) => {
14
14
const arr = [ ...range ( 6 ) ]
15
- const res = await map ( arr , ( x ) => x * 2 )
15
+ const res = await map ( arr , ( x ) => x * 2 , 2 )
16
16
expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
17
17
} )
18
18
@@ -27,7 +27,132 @@ test('map concurrency', async () => {
27
27
ds [ x ] . resolve ( )
28
28
await d . promise
29
29
return x * 2
30
- } )
30
+ } , 2 )
31
+ await ds [ 1 ] . promise
32
+ expect ( called [ 0 ] ) . toBe ( 1 )
33
+ expect ( called [ 1 ] ) . toBe ( 1 )
34
+ expect ( called [ 2 ] ) . toBe ( 0 )
35
+ expect ( called [ 3 ] ) . toBe ( 0 )
36
+ expect ( called [ 4 ] ) . toBe ( 0 )
37
+ expect ( called [ 5 ] ) . toBe ( 0 )
38
+ d . resolve ( )
39
+ const res = await p
40
+ expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
41
+ expect ( called [ 0 ] ) . toBe ( 1 )
42
+ expect ( called [ 1 ] ) . toBe ( 1 )
43
+ expect ( called [ 2 ] ) . toBe ( 1 )
44
+ expect ( called [ 3 ] ) . toBe ( 1 )
45
+ expect ( called [ 4 ] ) . toBe ( 1 )
46
+ expect ( called [ 5 ] ) . toBe ( 1 )
47
+ } )
48
+
49
+ test ( 'map index & iterable' , async ( ) => {
50
+ const arr = [ ...range ( 6 ) ]
51
+ const res = await map ( arr , async ( x , index , iterable ) => {
52
+ expect ( index ) . toBe ( x )
53
+ expect ( iterable ) . toBe ( arr )
54
+ return x * 2
55
+ } , 2 )
56
+ expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
57
+ } )
58
+
59
+ test ( 'map one exception' , async ( ) => {
60
+ const arr = [ ...range ( 3 ) ]
61
+ const called = { }
62
+ arr . forEach ( ( v ) => { called [ v ] = 0 } )
63
+ try {
64
+ const d = new Deferred ( )
65
+ const p = map ( arr , async ( x ) => {
66
+ called [ x ] += 1
67
+ await d . promise
68
+ if ( x === 1 ) {
69
+ throw new Error ( 'test' )
70
+ }
71
+ return x * 2
72
+ } , 1 )
73
+ d . resolve ( )
74
+ await p
75
+ expect ( false ) . toBe ( true )
76
+ } catch ( e ) {
77
+ expect ( e . message ) . toBe ( 'test' )
78
+ }
79
+ expect ( called [ 0 ] ) . toBe ( 1 )
80
+ expect ( called [ 1 ] ) . toBe ( 1 )
81
+ expect ( called [ 2 ] ) . toBe ( 0 )
82
+ } )
83
+
84
+ test ( 'map all exception c 1' , async ( ) => {
85
+ const arr = [ ...range ( 3 ) ]
86
+ const called = { }
87
+ arr . forEach ( ( v ) => { called [ v ] = 0 } )
88
+ try {
89
+ const d = new Deferred ( )
90
+ const p = map ( arr , async ( x ) => {
91
+ called [ x ] += 1
92
+ await d . promise
93
+ throw new Error ( 'test' )
94
+ } , 1 )
95
+ d . resolve ( )
96
+ await p
97
+ expect ( false ) . toBe ( true )
98
+ } catch ( e ) {
99
+ expect ( e . message ) . toBe ( 'test' )
100
+ }
101
+ expect ( called [ 0 ] ) . toBe ( 1 )
102
+ expect ( called [ 1 ] ) . toBe ( 0 )
103
+ expect ( called [ 2 ] ) . toBe ( 0 )
104
+ } )
105
+
106
+ test ( 'map all exception c 2' , async ( ) => {
107
+ const arr = [ ...range ( 3 ) ]
108
+ const called = { }
109
+ arr . forEach ( ( v ) => { called [ v ] = 0 } )
110
+ const ds = arr . map ( ( ) => new Deferred ( ) )
111
+ try {
112
+ const d = new Deferred ( )
113
+ const p = map ( arr , async ( x ) => {
114
+ called [ x ] += 1
115
+ ds [ x ] . resolve ( )
116
+ await d . promise
117
+ throw new Error ( 'test' )
118
+ } , 2 )
119
+ await ds [ 0 ] . promise
120
+ await ds [ 1 ] . promise
121
+ d . resolve ( )
122
+ await p
123
+ expect ( false ) . toBe ( true )
124
+ } catch ( e ) {
125
+ expect ( e . message ) . toBe ( 'test' )
126
+ }
127
+ expect ( called [ 0 ] ) . toBe ( 1 )
128
+ expect ( called [ 1 ] ) . toBe ( 1 )
129
+ expect ( called [ 2 ] ) . toBe ( 0 )
130
+ } )
131
+
132
+ test ( 'map infinite concurrency base' , async ( ) => {
133
+ const arr = [ ...range ( 6 ) ]
134
+ const res = await map ( arr , async ( x ) => x * 2 , Number . POSITIVE_INFINITY )
135
+ expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
136
+ } )
137
+
138
+ test ( 'map infinite concurrency no async' , async ( ) => {
139
+ const arr = [ ...range ( 6 ) ]
140
+ const res = await map ( arr , ( x ) => x * 2 , Number . POSITIVE_INFINITY )
141
+ expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
142
+ } )
143
+
144
+ test ( 'map infinite concurrency concurrency' , async ( ) => {
145
+ const arr = [ ...range ( 6 ) ]
146
+ const called = { }
147
+ arr . forEach ( ( v ) => { called [ v ] = 0 } )
148
+ const d = new Deferred ( )
149
+ const ds = arr . map ( ( ) => new Deferred ( ) )
150
+ const p = map ( arr , async ( x ) => {
151
+ called [ x ] += 1
152
+ ds [ x ] . resolve ( )
153
+ await d . promise
154
+ return x * 2
155
+ } , Number . POSITIVE_INFINITY )
31
156
await ds [ 5 ] . promise
32
157
expect ( called [ 0 ] ) . toBe ( 1 )
33
158
expect ( called [ 1 ] ) . toBe ( 1 )
@@ -45,3 +170,45 @@ test('map concurrency', async () => {
45
170
expect ( called [ 4 ] ) . toBe ( 1 )
46
171
expect ( called [ 5 ] ) . toBe ( 1 )
47
172
} )
173
+
174
+ test ( 'map concurrency 1 base' , async ( ) => {
175
+ const arr = [ ...range ( 6 ) ]
176
+ const res = await map ( arr , async ( x ) => x * 2 )
177
+ expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
178
+ } )
179
+
180
+ test ( 'map concurrency 1 no async' , async ( ) => {
181
+ const arr = [ ...range ( 6 ) ]
182
+ const res = await map ( arr , ( x ) => x * 2 )
183
+ expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
184
+ } )
185
+
186
+ test ( 'map concurrency 1 concurrency' , async ( ) => {
187
+ const arr = [ ...range ( 6 ) ]
188
+ const called = { }
189
+ arr . forEach ( ( v ) => { called [ v ] = 0 } )
190
+ const d = new Deferred ( )
191
+ const ds = arr . map ( ( ) => new Deferred ( ) )
192
+ const p = map ( arr , async ( x ) => {
193
+ ds [ x ] . resolve ( )
194
+ called [ x ] += 1
195
+ await d . promise
196
+ return x * 2
197
+ } )
198
+ await ds [ 0 ] . promise
199
+ expect ( called [ 0 ] ) . toBe ( 1 )
200
+ expect ( called [ 1 ] ) . toBe ( 0 )
201
+ expect ( called [ 2 ] ) . toBe ( 0 )
202
+ expect ( called [ 3 ] ) . toBe ( 0 )
203
+ expect ( called [ 4 ] ) . toBe ( 0 )
204
+ expect ( called [ 5 ] ) . toBe ( 0 )
205
+ d . resolve ( )
206
+ const res = await p
207
+ expect ( res ) . toEqual ( [ 0 , 2 , 4 , 6 , 8 , 10 ] )
208
+ expect ( called [ 0 ] ) . toBe ( 1 )
209
+ expect ( called [ 1 ] ) . toBe ( 1 )
210
+ expect ( called [ 2 ] ) . toBe ( 1 )
211
+ expect ( called [ 3 ] ) . toBe ( 1 )
212
+ expect ( called [ 4 ] ) . toBe ( 1 )
213
+ expect ( called [ 5 ] ) . toBe ( 1 )
214
+ } )
0 commit comments