@@ -71,7 +71,7 @@ suite("CachedAsyncIterable", function() {
71
71
} ) ;
72
72
} ) ;
73
73
74
- suite ( "sync iteration over cached elements" , function ( ) {
74
+ suite . skip ( "sync iteration over cached elements" , function ( ) {
75
75
let o1 , o2 ;
76
76
77
77
suiteSetup ( function ( ) {
@@ -125,7 +125,8 @@ suite("CachedAsyncIterable", function() {
125
125
126
126
const iterable = new CachedAsyncIterable ( generate ( ) ) ;
127
127
await iterable . touchNext ( ) ;
128
- assert . deepEqual ( [ ...iterable ] , [ o1 ] ) ;
128
+ let x = [ ...iterable ] ;
129
+ assert . deepEqual ( [ ...iterable ] , [ o1 ] )
129
130
} ) ;
130
131
131
132
test ( "async iterable with all cached elements" , async function ( ) {
@@ -175,20 +176,41 @@ suite("CachedAsyncIterable", function() {
175
176
let counter = 0 ;
176
177
177
178
async function * generate ( ) {
178
- while ( true ) {
179
- counter ++ ;
180
- yield await Promise . resolve ( ) ;
181
- }
179
+ while ( true ) {
180
+ counter ++ ;
181
+ yield null ;
182
+ }
182
183
}
183
184
184
185
// We're testing that if the first call to asyncIterator has been
185
186
// made, but the value of it has not been returned yet,
186
- // the consequitive call returns the same Promise rather than,
187
+ // the consecutive call returns the same Promise rather than,
187
188
// attempting to fetch the next item from the iterator.
188
189
const iterable = new CachedAsyncIterable ( generate ( ) ) ;
189
- iterable [ Symbol . asyncIterator ] ( ) . next ( ) ;
190
- await iterable [ Symbol . asyncIterator ] ( ) . next ( ) ;
190
+ const [ val1 , val2 ] = await Promise . all ( [
191
+ iterable [ Symbol . asyncIterator ] ( ) . next ( ) ,
192
+ iterable [ Symbol . asyncIterator ] ( ) . next ( ) ,
193
+ ] ) ;
191
194
assert . equal ( counter , 1 ) ;
195
+ assert . equal ( val1 , val2 ) ;
196
+ } ) ;
197
+
198
+ test ( "iterable's next can be called multiple times in parallel" , async function ( ) {
199
+ let counter = 0 ;
200
+
201
+ async function * generate ( ) {
202
+ while ( true ) {
203
+ counter ++ ;
204
+ yield null ;
205
+ }
206
+ }
207
+
208
+ const iterable = new CachedAsyncIterable ( generate ( ) ) ;
209
+ const iterator = iterable [ Symbol . asyncIterator ] ( ) ;
210
+ let val1 = await iterator . next ( ) ;
211
+ let val2 = await iterator . next ( ) ;
212
+ assert . equal ( counter , 2 ) ;
213
+ assert . notEqual ( val1 , val2 ) ;
192
214
} ) ;
193
215
} ) ;
194
216
@@ -293,5 +315,30 @@ suite("CachedAsyncIterable", function() {
293
315
await iterable . touchNext ( ) ,
294
316
{ value : undefined , done : true } ) ;
295
317
} ) ;
318
+
319
+ test ( "touchNext can be called multiple times in parallel" , async function ( ) {
320
+ let counter = 0 ;
321
+
322
+ async function * generate ( ) {
323
+ let value = 5 ;
324
+ while ( value -- > 0 ) {
325
+ counter ++ ;
326
+ yield await Promise . resolve ( value ) ;
327
+ }
328
+ }
329
+
330
+ // We're testing that if the first call to asyncIterator has been
331
+ // made, but the value of it has not been returned yet,
332
+ // the consequitive call returns the same Promise rather than,
333
+ // attempting to fetch the next item from the iterator.
334
+ const iterable = new CachedAsyncIterable ( generate ( ) ) ;
335
+ await Promise . all ( [
336
+ iterable . touchNext ( 2 ) ,
337
+ iterable [ Symbol . asyncIterator ] ( ) . next ( ) ,
338
+ iterable . touchNext ( 2 ) ,
339
+ iterable [ Symbol . asyncIterator ] ( ) . next ( ) ,
340
+ ] ) ;
341
+ assert . equal ( counter , 4 ) ;
342
+ } ) ;
296
343
} ) ;
297
344
} ) ;
0 commit comments