@@ -133,6 +133,9 @@ describe('Workspace', () => {
133
133
} ) ;
134
134
} ) ;
135
135
136
+ /**
137
+ * Deprecated method - replaced by fetchWorkersInfo
138
+ */
136
139
describe ( '#fetchWorkers' , ( ) => {
137
140
const requestURL = 'Workspaces/WSxxx/Workers' ;
138
141
@@ -251,7 +254,132 @@ describe('Workspace', () => {
251
254
expect ( stub . withArgs ( url , API_V1 ) . calledOnce ) . to . be . true ;
252
255
} ) ;
253
256
} ) ;
257
+ } ) ;
258
+
259
+ describe ( '#fetchWorkersInfo' , ( ) => {
260
+ const requestURL = 'Workspaces/WSxxx/Workers' ;
261
+
262
+ let sandbox ;
263
+ beforeEach ( ( ) => {
264
+ sandbox = sinon . sandbox . create ( ) ;
265
+ } ) ;
266
+
267
+ afterEach ( ( ) => sandbox . restore ( ) ) ;
268
+
269
+ it ( 'should fetch workers info' , ( ) => {
270
+ const workspace = new Workspace ( adminToken ) ;
271
+ const requestParams = { PageSize : 1000 } ;
272
+ const stub = sandbox . stub ( Request . prototype , 'get' ) . withArgs ( requestURL , API_V1 , requestParams ) . returns ( Promise . resolve ( workerList ) ) ;
273
+
274
+ return workspace . fetchWorkersInfo ( ) . then ( workers => {
275
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParams ) . calledOnce ) . to . be . true ;
276
+ expect ( workers . size ) . to . equal ( workerList . contents . length ) ;
277
+ for ( const worker of workers . values ( ) ) {
278
+ expect ( worker ) . to . be . instanceOf ( WorkerContainer ) ;
279
+ }
280
+ } ) ;
281
+ } ) ;
282
+
283
+ it ( 'should fetch workers info with args' , ( ) => {
284
+ const workspace = new Workspace ( adminToken ) ;
285
+ const requestParams = { 'PageSize' : 1000 , 'FriendlyName' : 'test' , 'ActivitySid' : 'WAxxx' , 'ActivityName' : 'Idle' , 'Ordering' : 'DateUpdated:asc' , 'TargetWorkersExpression' : 'name IN [\'Alice\']' } ;
286
+ const stub = sandbox . stub ( Request . prototype , 'get' ) . withArgs ( requestURL , API_V1 , requestParams ) . returns ( Promise . resolve ( workerList ) ) ;
287
+
288
+ return workspace . fetchWorkersInfo ( requestParams ) . then ( workers => {
289
+ expect ( workers . size ) . to . equal ( workerList . contents . length ) ;
290
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParams ) . calledOnce ) . to . be . true ;
291
+ for ( const worker of workers . values ( ) ) {
292
+ expect ( worker ) . to . be . instanceOf ( WorkerContainer ) ;
293
+ }
294
+ } ) ;
295
+ } ) ;
296
+
297
+ it ( 'should paginate for the next page if needed' , ( ) => {
298
+ const requestParamsPage0 = { 'PageSize' : 1 , 'FriendlyName' : 'test' } ;
299
+ const requestParamsPage1 = { 'PageSize' : 1 , 'AfterSid' : 'WKxx1' , 'FriendlyName' : 'test' } ;
300
+
301
+ const stub = sandbox . stub ( Request . prototype , 'get' ) ;
302
+ stub . withArgs ( requestURL , API_V1 , requestParamsPage0 ) . returns ( Promise . resolve ( workerListPage0 ) ) ;
303
+ stub . withArgs ( requestURL , API_V1 , requestParamsPage1 ) . returns ( Promise . resolve ( workerListPage1 ) ) ;
304
+ const workspace = new Workspace ( adminToken , { pageSize : 1 } ) ;
254
305
306
+ return workspace . fetchWorkersInfo ( requestParamsPage0 ) . then ( ( workers ) => {
307
+ expect ( workers . size ) . to . equal ( workerListPage0 . total ) ;
308
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParamsPage0 ) . calledOnce ) . to . be . true ;
309
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParamsPage1 ) . calledOnce ) . to . be . true ;
310
+ } ) ;
311
+ } ) ;
312
+
313
+ it ( 'should paginate with ordering parameter' , ( ) => {
314
+ const requestParamsPage0 = { 'PageSize' : 1 , 'FriendlyName' : 'test' , 'Ordering' : 'DateUpdated:asc' } ;
315
+ const requestParamsPage1 = { 'PageSize' : 1 , 'NextToken' : 'WKxx1/2022-08-09T19:09:10.763Z' , 'FriendlyName' : 'test' , 'Ordering' : 'DateUpdated:asc' } ;
316
+
317
+ const stub = sandbox . stub ( Request . prototype , 'get' ) ;
318
+ stub . withArgs ( requestURL , API_V1 , requestParamsPage0 ) . returns ( Promise . resolve ( workerListPage0 ) ) ;
319
+ stub . withArgs ( requestURL , API_V1 , requestParamsPage1 ) . returns ( Promise . resolve ( workerListPage1 ) ) ;
320
+ const workspace = new Workspace ( adminToken , { pageSize : 1 } ) ;
321
+
322
+ return workspace . fetchWorkersInfo ( requestParamsPage0 ) . then ( ( workers ) => {
323
+ expect ( workers . size ) . to . equal ( workerListPage0 . total ) ;
324
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParamsPage0 ) . calledOnce ) . to . be . true ;
325
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParamsPage1 ) . calledOnce ) . to . be . true ;
326
+ } ) ;
327
+ } ) ;
328
+
329
+ it ( 'should fetch max workers' , async ( ) => {
330
+ const requestParamsPage0 = { 'PageSize' : 1 } ;
331
+ const requestParamsPage1 = { 'PageSize' : 1 , 'AfterSid' : 'WKxx1' } ;
332
+
333
+ const stub = sandbox . stub ( Request . prototype , 'get' ) ;
334
+ stub . withArgs ( requestURL , API_V1 , requestParamsPage0 ) . returns ( Promise . resolve ( workerListPage0 ) ) ;
335
+ stub . withArgs ( requestURL , API_V1 , requestParamsPage1 ) . returns ( Promise . resolve ( workerListPage1 ) ) ;
336
+ const workspace = new Workspace ( adminToken , { pageSize : 1 } ) ;
337
+ const maxWorkers = 1 ;
338
+
339
+ const partOfWorkers = await workspace . fetchWorkersInfo ( { MaxWorkers : maxWorkers } ) ;
340
+
341
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParamsPage0 ) . calledOnce ) . to . be . true ;
342
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParamsPage1 ) . calledOnce ) . to . be . false ;
343
+
344
+ const allWorkers = await workspace . fetchWorkersInfo ( ) ;
345
+
346
+ expect ( partOfWorkers . size ) . to . equal ( maxWorkers ) ;
347
+ expect ( partOfWorkers . size ) . to . be . lessThan ( allWorkers . size ) ;
348
+ } ) ;
349
+
350
+ it ( 'should fetch max workers and split the page if needed' , async ( ) => {
351
+ const requestParamsPage0 = { 'PageSize' : 2 } ;
352
+ const requestParamsPage1 = { 'PageSize' : 2 , 'AfterSid' : 'Wkxx2' } ;
353
+
354
+ const stub = sandbox . stub ( Request . prototype , 'get' ) ;
355
+ stub . withArgs ( requestURL , API_V1 , requestParamsPage0 ) . returns ( Promise . resolve ( workerList2Page0 ) ) ;
356
+ stub . withArgs ( requestURL , API_V1 , requestParamsPage1 ) . returns ( Promise . resolve ( workerList2Page1 ) ) ;
357
+ const workspace = new Workspace ( adminToken , { pageSize : 2 } ) ;
358
+ const maxWorkers = 1 ;
359
+
360
+ const partOfWorkers = await workspace . fetchWorkersInfo ( { MaxWorkers : maxWorkers } ) ;
361
+
362
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParamsPage0 ) . calledOnce ) . to . be . true ;
363
+ expect ( stub . withArgs ( requestURL , API_V1 , requestParamsPage1 ) . calledOnce ) . to . be . false ;
364
+
365
+ const allWorkers = await workspace . fetchWorkersInfo ( ) ;
366
+
367
+ expect ( partOfWorkers . size ) . to . equal ( maxWorkers ) ;
368
+ expect ( partOfWorkers . size ) . to . be . lessThan ( allWorkers . size ) ;
369
+ } ) ;
370
+
371
+ it ( 'should fetch worker with sid' , ( ) => {
372
+ const workspace = new Workspace ( adminToken ) ;
373
+ const workerInstance = workerList . contents [ 0 ] ;
374
+ const url = path . join ( requestURL , workerInstance . sid ) ;
375
+ const stub = sandbox . stub ( Request . prototype , 'get' ) . withArgs ( url , API_V1 ) . returns ( Promise . resolve ( workerInstance ) ) ;
376
+
377
+ return workspace . fetchWorkerInfo ( workerInstance . sid ) . then ( queue => {
378
+ expect ( queue . sid ) . to . equal ( workerInstance . sid ) ;
379
+ expect ( queue ) . to . be . instanceOf ( WorkerContainer ) ;
380
+ expect ( stub . withArgs ( url , API_V1 ) . calledOnce ) . to . be . true ;
381
+ } ) ;
382
+ } ) ;
255
383
} ) ;
256
384
257
385
describe ( '#fetchTasks' , ( ) => {
0 commit comments