@@ -7,6 +7,7 @@ import { assert, expect } from 'chai';
77import fetch from 'cross-fetch' ;
88import { rejects } from 'node:assert' ;
99import { posix } from 'node:path' ;
10+ import PQueue from 'p-queue' ;
1011import queryString from 'query-string' ;
1112import { v4 } from 'uuid' ;
1213import { ArduinoPreferences } from '../../browser/arduino-preferences' ;
@@ -50,6 +51,11 @@ describe('create-api', () => {
5051 await cleanAllSketches ( ) ;
5152 } ) ;
5253
54+ afterEach ( async function ( ) {
55+ this . timeout ( timeout ) ;
56+ await cleanAllSketches ( ) ;
57+ } ) ;
58+
5359 function createContainer ( accessToken : string ) : Container {
5460 const container = new Container ( { defaultScope : 'Singleton' } ) ;
5561 container . load (
@@ -126,13 +132,14 @@ describe('create-api', () => {
126132
127133 async function cleanAllSketches ( ) : Promise < void > {
128134 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 ( ) ;
136143 sketches = await createApi . sketches ( ) ;
137144 expect ( sketches ) . to . be . empty ;
138145 }
@@ -348,19 +355,23 @@ describe('create-api', () => {
348355 diff < 0 ? '<' : diff > 0 ? '>' : '='
349356 } limit)`, async ( ) => {
350357 const content = 'void setup(){} void loop(){}' ;
351- const maxLimit = 50 ; // https://github.com/arduino/arduino-ide/pull/875
358+ const maxLimit = 10 ;
352359 const sketchCount = maxLimit + diff ;
353360 const sketchNames = [ ...Array ( sketchCount ) . keys ( ) ] . map ( ( ) => v4 ( ) ) ;
354361
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 ( ) ;
361372
362373 createApi . resetRequestRecording ( ) ;
363- const sketches = await createApi . sketches ( ) ;
374+ const sketches = await createApi . sketches ( maxLimit ) ;
364375 const allRequests = createApi . requestRecording . slice ( ) ;
365376
366377 expect ( sketches . length ) . to . be . equal ( sketchCount ) ;
0 commit comments