@@ -1221,7 +1221,7 @@ describe('Consumer', () => {
1221
1221
VisibilityTimeout : 40
1222
1222
} )
1223
1223
) ;
1224
- sandbox . assert . calledTwice ( clearIntervalSpy ) ;
1224
+ sandbox . assert . calledOnce ( clearIntervalSpy ) ;
1225
1225
} ) ;
1226
1226
1227
1227
it ( 'passes in the correct visibility timeout for long running batch handler functions' , async ( ) => {
@@ -1305,7 +1305,7 @@ describe('Consumer', () => {
1305
1305
]
1306
1306
} )
1307
1307
) ;
1308
- sandbox . assert . calledTwice ( clearIntervalSpy ) ;
1308
+ sandbox . assert . calledOnce ( clearIntervalSpy ) ;
1309
1309
} ) ;
1310
1310
1311
1311
it ( 'emit error when changing visibility timeout fails' , async ( ) => {
@@ -1483,13 +1483,22 @@ describe('Consumer', () => {
1483
1483
} ) ;
1484
1484
1485
1485
it ( 'waits for in-flight messages before emitting stopped (within timeout)' , async ( ) => {
1486
+ sqs . send . withArgs ( mockReceiveMessage ) . resolves ( {
1487
+ Messages : [
1488
+ { MessageId : '1' , ReceiptHandle : 'receipt-handle-1' , Body : 'body-1' }
1489
+ ]
1490
+ } ) ;
1486
1491
const handleStop = sandbox . stub ( ) . returns ( null ) ;
1487
1492
const handleResponseProcessed = sandbox . stub ( ) . returns ( null ) ;
1493
+ const waitingForPollingComplete = sandbox . stub ( ) . returns ( null ) ;
1494
+ const waitingForPollingCompleteTimeoutExceeded = sandbox
1495
+ . stub ( )
1496
+ . returns ( null ) ;
1488
1497
1489
1498
// A slow message handler
1490
- handleMessage = sandbox . stub ( ) . callsFake ( async ( ) => {
1491
- await new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) ;
1492
- } ) ;
1499
+ handleMessage = sandbox
1500
+ . stub ( )
1501
+ . resolves ( new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) ) ;
1493
1502
1494
1503
consumer = new Consumer ( {
1495
1504
queueUrl : QUEUE_URL ,
@@ -1502,16 +1511,24 @@ describe('Consumer', () => {
1502
1511
1503
1512
consumer . on ( 'stopped' , handleStop ) ;
1504
1513
consumer . on ( 'response_processed' , handleResponseProcessed ) ;
1514
+ consumer . on ( 'waiting_for_polling_to_complete' , waitingForPollingComplete ) ;
1515
+ consumer . on (
1516
+ 'waiting_for_polling_to_complete_timeout_exceeded' ,
1517
+ waitingForPollingCompleteTimeoutExceeded
1518
+ ) ;
1505
1519
1506
1520
consumer . start ( ) ;
1507
- await clock . nextAsync ( ) ;
1521
+ await Promise . all ( [ clock . tickAsync ( 1 ) ] ) ;
1508
1522
consumer . stop ( ) ;
1509
1523
1510
1524
await clock . runAllAsync ( ) ;
1511
1525
1512
1526
sandbox . assert . calledOnce ( handleStop ) ;
1513
1527
sandbox . assert . calledOnce ( handleResponseProcessed ) ;
1514
1528
sandbox . assert . calledOnce ( handleMessage ) ;
1529
+ assert ( waitingForPollingComplete . callCount === 5 ) ;
1530
+ assert ( waitingForPollingCompleteTimeoutExceeded . callCount === 0 ) ;
1531
+
1515
1532
assert . ok ( handleMessage . calledBefore ( handleStop ) ) ;
1516
1533
1517
1534
// handleResponseProcessed is called after handleMessage, indicating
@@ -1520,13 +1537,22 @@ describe('Consumer', () => {
1520
1537
} ) ;
1521
1538
1522
1539
it ( 'waits for in-flight messages before emitting stopped (timeout reached)' , async ( ) => {
1540
+ sqs . send . withArgs ( mockReceiveMessage ) . resolves ( {
1541
+ Messages : [
1542
+ { MessageId : '1' , ReceiptHandle : 'receipt-handle-1' , Body : 'body-1' }
1543
+ ]
1544
+ } ) ;
1523
1545
const handleStop = sandbox . stub ( ) . returns ( null ) ;
1524
1546
const handleResponseProcessed = sandbox . stub ( ) . returns ( null ) ;
1547
+ const waitingForPollingComplete = sandbox . stub ( ) . returns ( null ) ;
1548
+ const waitingForPollingCompleteTimeoutExceeded = sandbox
1549
+ . stub ( )
1550
+ . returns ( null ) ;
1525
1551
1526
1552
// A slow message handler
1527
- handleMessage = sandbox . stub ( ) . callsFake ( async ( ) => {
1528
- await new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) ;
1529
- } ) ;
1553
+ handleMessage = sandbox
1554
+ . stub ( )
1555
+ . resolves ( new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) ) ;
1530
1556
1531
1557
consumer = new Consumer ( {
1532
1558
queueUrl : QUEUE_URL ,
@@ -1539,16 +1565,23 @@ describe('Consumer', () => {
1539
1565
1540
1566
consumer . on ( 'stopped' , handleStop ) ;
1541
1567
consumer . on ( 'response_processed' , handleResponseProcessed ) ;
1568
+ consumer . on ( 'waiting_for_polling_to_complete' , waitingForPollingComplete ) ;
1569
+ consumer . on (
1570
+ 'waiting_for_polling_to_complete_timeout_exceeded' ,
1571
+ waitingForPollingCompleteTimeoutExceeded
1572
+ ) ;
1542
1573
1543
1574
consumer . start ( ) ;
1544
- await clock . nextAsync ( ) ;
1575
+ await Promise . all ( [ clock . tickAsync ( 1 ) ] ) ;
1545
1576
consumer . stop ( ) ;
1546
1577
1547
1578
await clock . runAllAsync ( ) ;
1548
1579
1549
1580
sandbox . assert . calledOnce ( handleStop ) ;
1550
1581
sandbox . assert . calledOnce ( handleResponseProcessed ) ;
1551
1582
sandbox . assert . calledOnce ( handleMessage ) ;
1583
+ sandbox . assert . calledOnce ( waitingForPollingComplete ) ;
1584
+ sandbox . assert . calledOnce ( waitingForPollingCompleteTimeoutExceeded ) ;
1552
1585
assert ( handleMessage . calledBefore ( handleStop ) ) ;
1553
1586
1554
1587
// Stop was called before the message could be processed, because we reached timeout.
@@ -1768,33 +1801,5 @@ describe('Consumer', () => {
1768
1801
sandbox . assert . calledWithMatch ( loggerDebug , 'stopping' ) ;
1769
1802
sandbox . assert . calledWithMatch ( loggerDebug , 'stopped' ) ;
1770
1803
} ) ;
1771
-
1772
- it ( 'logs a debug event while the handler is processing, for every second' , async ( ) => {
1773
- const loggerDebug = sandbox . stub ( logger , 'debug' ) ;
1774
- const clearIntervalSpy = sinon . spy ( global , 'clearInterval' ) ;
1775
-
1776
- sqs . send . withArgs ( mockReceiveMessage ) . resolves ( {
1777
- Messages : [
1778
- { MessageId : '1' , ReceiptHandle : 'receipt-handle-1' , Body : 'body-1' }
1779
- ]
1780
- } ) ;
1781
- consumer = new Consumer ( {
1782
- queueUrl : QUEUE_URL ,
1783
- region : REGION ,
1784
- handleMessage : ( ) =>
1785
- new Promise ( ( resolve ) => setTimeout ( resolve , 4000 ) ) ,
1786
- sqs
1787
- } ) ;
1788
-
1789
- consumer . start ( ) ;
1790
- await Promise . all ( [ clock . tickAsync ( 5000 ) ] ) ;
1791
- sandbox . assert . calledOnce ( clearIntervalSpy ) ;
1792
- consumer . stop ( ) ;
1793
-
1794
- sandbox . assert . callCount ( loggerDebug , 15 ) ;
1795
- sandbox . assert . calledWith ( loggerDebug , 'handler_processing' , {
1796
- detail : 'The handler is still processing the message(s)...'
1797
- } ) ;
1798
- } ) ;
1799
1804
} ) ;
1800
1805
} ) ;
0 commit comments