@@ -7,6 +7,7 @@ import { assert, expect } from 'chai';
7
7
import fetch from 'cross-fetch' ;
8
8
import { rejects } from 'node:assert' ;
9
9
import { posix } from 'node:path' ;
10
+ import PQueue from 'p-queue' ;
10
11
import queryString from 'query-string' ;
11
12
import { v4 } from 'uuid' ;
12
13
import { ArduinoPreferences } from '../../browser/arduino-preferences' ;
@@ -50,6 +51,11 @@ describe('create-api', () => {
50
51
await cleanAllSketches ( ) ;
51
52
} ) ;
52
53
54
+ afterEach ( async function ( ) {
55
+ this . timeout ( timeout ) ;
56
+ await cleanAllSketches ( ) ;
57
+ } ) ;
58
+
53
59
function createContainer ( accessToken : string ) : Container {
54
60
const container = new Container ( { defaultScope : 'Singleton' } ) ;
55
61
container . load (
@@ -126,13 +132,14 @@ describe('create-api', () => {
126
132
127
133
async function cleanAllSketches ( ) : Promise < void > {
128
134
let sketches = await createApi . sketches ( ) ;
129
- // Cannot delete the sketches with `await Promise.all` as all delete promise successfully resolve, but the sketch is not deleted from the server.
130
- await sketches
131
- . map ( ( { path } ) => createApi . deleteSketch ( path ) )
132
- . reduce ( async ( acc , curr ) => {
133
- await acc ;
134
- return curr ;
135
- } , Promise . resolve ( ) ) ;
135
+ const deleteExecutionQueue = new PQueue ( {
136
+ concurrency : 5 ,
137
+ autoStart : true ,
138
+ } ) ;
139
+ sketches . forEach ( ( { path } ) =>
140
+ deleteExecutionQueue . add ( ( ) => createApi . deleteSketch ( path ) )
141
+ ) ;
142
+ await deleteExecutionQueue . onIdle ( ) ;
136
143
sketches = await createApi . sketches ( ) ;
137
144
expect ( sketches ) . to . be . empty ;
138
145
}
@@ -348,19 +355,23 @@ describe('create-api', () => {
348
355
diff < 0 ? '<' : diff > 0 ? '>' : '='
349
356
} limit)`, async ( ) => {
350
357
const content = 'void setup(){} void loop(){}' ;
351
- const maxLimit = 50 ; // https://github.com/arduino/arduino-ide/pull/875
358
+ const maxLimit = 10 ;
352
359
const sketchCount = maxLimit + diff ;
353
360
const sketchNames = [ ...Array ( sketchCount ) . keys ( ) ] . map ( ( ) => v4 ( ) ) ;
354
361
355
- await sketchNames
356
- . map ( ( name ) => createApi . createSketch ( toPosix ( name ) , content ) )
357
- . reduce ( async ( acc , curr ) => {
358
- await acc ;
359
- return curr ;
360
- } , Promise . resolve ( ) as Promise < unknown > ) ;
362
+ const createExecutionQueue = new PQueue ( {
363
+ concurrency : 5 ,
364
+ autoStart : true ,
365
+ } ) ;
366
+ sketchNames . forEach ( ( name ) =>
367
+ createExecutionQueue . add ( ( ) =>
368
+ createApi . createSketch ( toPosix ( name ) , content )
369
+ )
370
+ ) ;
371
+ await createExecutionQueue . onIdle ( ) ;
361
372
362
373
createApi . resetRequestRecording ( ) ;
363
- const sketches = await createApi . sketches ( ) ;
374
+ const sketches = await createApi . sketches ( maxLimit ) ;
364
375
const allRequests = createApi . requestRecording . slice ( ) ;
365
376
366
377
expect ( sketches . length ) . to . be . equal ( sketchCount ) ;
0 commit comments