@@ -14,6 +14,7 @@ import {
14
14
DescribeLogStreamsCommand ,
15
15
PutLogEventsCommand ,
16
16
} from '@aws-sdk/client-cloudwatch-logs' ;
17
+ import { loggers } from 'winston' ;
17
18
18
19
const credentials = {
19
20
accessKeyId : 'accessKeyId' ,
@@ -178,33 +179,52 @@ describe('AWSCloudWatchProvider', () => {
178
179
} ) ;
179
180
180
181
describe ( 'pushLogs test' , ( ) => {
182
+ let provider ;
183
+ let mockInitiateLogPushInterval ;
184
+ beforeEach ( ( ) => {
185
+ provider = new AWSCloudWatchProvider ( testConfig ) ;
186
+ mockInitiateLogPushInterval = jest
187
+ . spyOn ( provider as any , '_initiateLogPushInterval' )
188
+ . mockImplementation ( ) ;
189
+ } ) ;
190
+ afterEach ( ( ) => {
191
+ jest . clearAllMocks ( ) ;
192
+ } ) ;
181
193
it ( 'should add the provided logs to the log queue' , ( ) => {
182
- const provider = new AWSCloudWatchProvider ( testConfig ) ;
183
194
provider . pushLogs ( [ { message : 'hello' , timestamp : 1111 } ] ) ;
184
195
185
196
let logQueue = provider . getLogQueue ( ) ;
186
-
187
197
expect ( logQueue ) . toHaveLength ( 1 ) ;
188
198
189
199
provider . pushLogs ( [
190
- {
191
- message : 'goodbye' ,
192
- timestamp : 1112 ,
193
- } ,
194
- {
195
- message : 'ohayou' ,
196
- timestamp : 1113 ,
197
- } ,
198
- {
199
- message : 'konbanwa' ,
200
- timestamp : 1114 ,
201
- } ,
200
+ { message : 'goodbye' , timestamp : 1112 } ,
201
+ { message : 'ohayou' , timestamp : 1113 } ,
202
+ { message : 'konbanwa' , timestamp : 1114 } ,
202
203
] ) ;
203
204
204
205
logQueue = provider . getLogQueue ( ) ;
205
-
206
206
expect ( logQueue ) . toHaveLength ( 4 ) ;
207
207
} ) ;
208
+ it ( 'should reset retry mechanism when _maxRetriesReached is true' , ( ) => {
209
+ provider [ '_maxRetriesReached' ] = true ;
210
+ provider [ '_retryCount' ] = 6 ;
211
+
212
+ provider . pushLogs ( [ { message : 'test' , timestamp : Date . now ( ) } ] ) ;
213
+
214
+ expect ( provider [ '_retryCount' ] ) . toBe ( 0 ) ;
215
+ expect ( provider [ '_maxRetriesReached' ] ) . toBe ( false ) ;
216
+ expect ( mockInitiateLogPushInterval ) . toHaveBeenCalledTimes ( 2 ) ;
217
+ } ) ;
218
+ it ( 'should not reset retry mechanism when _maxRetriesReached is false' , ( ) => {
219
+ provider [ '_maxRetriesReached' ] = false ;
220
+ provider [ '_retryCount' ] = 3 ;
221
+
222
+ provider . pushLogs ( [ { message : 'test' , timestamp : Date . now ( ) } ] ) ;
223
+
224
+ expect ( provider [ '_retryCount' ] ) . toBe ( 3 ) ;
225
+ expect ( provider [ '_maxRetriesReached' ] ) . toBe ( false ) ;
226
+ expect ( mockInitiateLogPushInterval ) . toHaveBeenCalledTimes ( 1 ) ;
227
+ } ) ;
208
228
} ) ;
209
229
210
230
describe ( '_safeUploadLogEvents test' , ( ) => {
@@ -397,4 +417,88 @@ describe('AWSCloudWatchProvider', () => {
397
417
} ) ;
398
418
} ) ;
399
419
} ) ;
420
+ describe ( '_initiateLogPushInterval' , ( ) => {
421
+ let provider : AWSCloudWatchProvider ;
422
+ let safeUploadLogEventsSpy : jest . SpyInstance ;
423
+ let getDocUploadPermissibilitySpy : jest . SpyInstance ;
424
+ let setIntervalSpy : jest . SpyInstance ;
425
+
426
+ beforeEach ( ( ) => {
427
+ jest . useFakeTimers ( ) ;
428
+ provider = new AWSCloudWatchProvider ( testConfig ) ;
429
+ safeUploadLogEventsSpy = jest . spyOn (
430
+ provider as any ,
431
+ '_safeUploadLogEvents'
432
+ ) ;
433
+ getDocUploadPermissibilitySpy = jest . spyOn (
434
+ provider as any ,
435
+ '_getDocUploadPermissibility'
436
+ ) ;
437
+ setIntervalSpy = jest . spyOn ( global , 'setInterval' ) ;
438
+ } ) ;
439
+
440
+ afterEach ( ( ) => {
441
+ jest . useRealTimers ( ) ;
442
+ jest . restoreAllMocks ( ) ;
443
+ } ) ;
444
+
445
+ it ( 'should clear existing timer and set a new one' , ( ) => {
446
+ ( provider as any ) . _timer = setInterval ( ( ) => { } , 1000 ) ;
447
+ ( provider as any ) . _initiateLogPushInterval ( ) ;
448
+
449
+ expect ( setIntervalSpy ) . toHaveBeenCalledTimes ( 1 ) ;
450
+ } ) ;
451
+
452
+ it ( 'should not upload logs if max retries are reached' , async ( ) => {
453
+ ( provider as any ) . _maxRetriesReached = true ;
454
+ ( provider as any ) . _initiateLogPushInterval ( ) ;
455
+
456
+ jest . advanceTimersByTime ( 2000 ) ;
457
+ await Promise . resolve ( ) ;
458
+
459
+ expect ( safeUploadLogEventsSpy ) . not . toHaveBeenCalled ( ) ;
460
+ } ) ;
461
+
462
+ it ( 'should upload logs if conditions are met' , async ( ) => {
463
+ getDocUploadPermissibilitySpy . mockReturnValue ( true ) ;
464
+ safeUploadLogEventsSpy . mockResolvedValue ( undefined ) ;
465
+
466
+ ( provider as any ) . _initiateLogPushInterval ( ) ;
467
+
468
+ jest . advanceTimersByTime ( 2000 ) ;
469
+ await Promise . resolve ( ) ;
470
+
471
+ expect ( safeUploadLogEventsSpy ) . toHaveBeenCalledTimes ( 1 ) ;
472
+ expect ( ( provider as any ) . _retryCount ) . toBe ( 0 ) ;
473
+ } ) ;
474
+
475
+ it ( 'should increment retry count on error' , async ( ) => {
476
+ getDocUploadPermissibilitySpy . mockReturnValue ( true ) ;
477
+ safeUploadLogEventsSpy . mockRejectedValue ( new Error ( 'Test error' ) ) ;
478
+
479
+ ( provider as any ) . _initiateLogPushInterval ( ) ;
480
+
481
+ jest . advanceTimersByTime ( 2000 ) ;
482
+ await Promise . resolve ( ) ;
483
+
484
+ expect ( ( provider as any ) . _retryCount ) . toBe ( 0 ) ;
485
+ } ) ;
486
+
487
+ it ( 'should stop retrying after max retries' , async ( ) => {
488
+ getDocUploadPermissibilitySpy . mockReturnValue ( true ) ;
489
+ safeUploadLogEventsSpy . mockRejectedValue ( new Error ( 'Test error' ) ) ;
490
+ ( provider as any ) . _maxRetries = 3 ;
491
+
492
+ ( provider as any ) . _initiateLogPushInterval ( ) ;
493
+
494
+ for ( let i = 0 ; i < 4 ; i ++ ) {
495
+ jest . advanceTimersByTime ( 2000 ) ;
496
+ await Promise . resolve ( ) ; // Allow any pending promise to resolve
497
+ }
498
+
499
+ expect ( ( provider as any ) . _retryCount ) . toBe ( 2 ) ;
500
+
501
+ expect ( safeUploadLogEventsSpy ) . toHaveBeenCalledTimes ( 4 ) ;
502
+ } ) ;
503
+ } ) ;
400
504
} ) ;
0 commit comments