@@ -24,7 +24,7 @@ import defer = require('p-defer');
24
24
25
25
import * as messageTypes from '../src/message-queues' ;
26
26
import { BatchError } from '../src/message-queues' ;
27
- import { AckError , Message , Subscriber } from '../src/subscriber' ;
27
+ import { Message , Subscriber } from '../src/subscriber' ;
28
28
import { DebugMessage } from '../src/debug' ;
29
29
30
30
class FakeClient {
@@ -99,36 +99,6 @@ class ModAckQueue extends messageTypes.ModAckQueue {
99
99
}
100
100
}
101
101
102
- // This discount polyfill for Promise.allSettled can be removed after we drop Node 12.
103
- type AllSettledResult < T , U > = {
104
- status : 'fulfilled' | 'rejected' ;
105
- value ?: T ;
106
- reason ?: U ;
107
- } ;
108
- function allSettled < T , U > (
109
- proms : Promise < T > [ ]
110
- ) : Promise < AllSettledResult < T , U > [ ] > {
111
- const checkedProms = proms . map ( ( r : Promise < T > ) =>
112
- r
113
- . then (
114
- ( value : T ) =>
115
- ( {
116
- status : 'fulfilled' ,
117
- value,
118
- } ) as AllSettledResult < T , U >
119
- )
120
- . catch (
121
- ( error : U ) =>
122
- ( {
123
- status : 'rejected' ,
124
- reason : error ,
125
- } ) as AllSettledResult < T , U >
126
- )
127
- ) ;
128
-
129
- return Promise . all ( checkedProms ) ;
130
- }
131
-
132
102
describe ( 'MessageQueues' , ( ) => {
133
103
const sandbox = sinon . createSandbox ( ) ;
134
104
@@ -190,6 +160,15 @@ describe('MessageQueues', () => {
190
160
assert . strictEqual ( stub . callCount , 1 ) ;
191
161
} ) ;
192
162
163
+ it ( 'should flush the queue if at byte capacity' , ( ) => {
164
+ const stub = sandbox . stub ( messageQueue , 'flush' ) ;
165
+
166
+ messageQueue . bytes = messageTypes . MAX_BATCH_BYTES - 10 ;
167
+ messageQueue . add ( new FakeMessage ( ) as Message ) ;
168
+
169
+ assert . strictEqual ( stub . callCount , 1 ) ;
170
+ } ) ;
171
+
193
172
it ( 'should schedule a flush if needed' , ( ) => {
194
173
const clock = sandbox . useFakeTimers ( ) ;
195
174
const stub = sandbox . stub ( messageQueue , 'flush' ) ;
@@ -244,6 +223,13 @@ describe('MessageQueues', () => {
244
223
assert . strictEqual ( messageQueue . numPendingRequests , 0 ) ;
245
224
} ) ;
246
225
226
+ it ( 'should remove the bytes of messages from the queue' , ( ) => {
227
+ messageQueue . add ( new FakeMessage ( ) as Message ) ;
228
+ messageQueue . flush ( ) ;
229
+
230
+ assert . strictEqual ( messageQueue . bytes , 0 ) ;
231
+ } ) ;
232
+
247
233
it ( 'should send the batch' , ( ) => {
248
234
const message = new FakeMessage ( ) ;
249
235
const deadline = 10 ;
@@ -498,7 +484,7 @@ describe('MessageQueues', () => {
498
484
( r : messageTypes . QueuedMessage ) => r . responsePromise ! . promise
499
485
) ;
500
486
await ackQueue . flush ( ) ;
501
- const results = await allSettled ( proms ) ;
487
+ const results = await Promise . allSettled ( proms ) ;
502
488
const oneSuccess = { status : 'fulfilled' , value : undefined } ;
503
489
assert . deepStrictEqual ( results , [ oneSuccess , oneSuccess , oneSuccess ] ) ;
504
490
} ) ;
@@ -522,7 +508,7 @@ describe('MessageQueues', () => {
522
508
proms . shift ( ) ;
523
509
await ackQueue . flush ( ) ;
524
510
525
- const results = await allSettled < void , AckError > ( proms ) ;
511
+ const results = await Promise . allSettled < void > ( proms ) ;
526
512
assert . strictEqual ( results [ 0 ] . status , 'rejected' ) ;
527
513
assert . strictEqual ( results [ 0 ] . reason ?. errorCode , 'OTHER' ) ;
528
514
assert . strictEqual ( results [ 1 ] . status , 'rejected' ) ;
@@ -552,7 +538,7 @@ describe('MessageQueues', () => {
552
538
] ;
553
539
await ackQueue . flush ( ) ;
554
540
555
- const results = await allSettled < void , AckError > ( proms ) ;
541
+ const results = await Promise . allSettled < void > ( proms ) ;
556
542
assert . strictEqual ( results [ 0 ] . status , 'rejected' ) ;
557
543
assert . strictEqual ( results [ 0 ] . reason ?. errorCode , 'INVALID' ) ;
558
544
@@ -789,7 +775,7 @@ describe('MessageQueues', () => {
789
775
( r : messageTypes . QueuedMessage ) => r . responsePromise ! . promise
790
776
) ;
791
777
await modAckQueue . flush ( ) ;
792
- const results = await allSettled ( proms ) ;
778
+ const results = await Promise . allSettled ( proms ) ;
793
779
const oneSuccess = { status : 'fulfilled' , value : undefined } ;
794
780
assert . deepStrictEqual ( results , [ oneSuccess , oneSuccess , oneSuccess ] ) ;
795
781
} ) ;
@@ -815,7 +801,7 @@ describe('MessageQueues', () => {
815
801
proms . shift ( ) ;
816
802
await modAckQueue . flush ( ) ;
817
803
818
- const results = await allSettled < void , AckError > ( proms ) ;
804
+ const results = await Promise . allSettled < void > ( proms ) ;
819
805
assert . strictEqual ( results [ 0 ] . status , 'rejected' ) ;
820
806
assert . strictEqual ( results [ 0 ] . reason ?. errorCode , 'OTHER' ) ;
821
807
assert . strictEqual ( results [ 1 ] . status , 'rejected' ) ;
@@ -847,7 +833,7 @@ describe('MessageQueues', () => {
847
833
] ;
848
834
await modAckQueue . flush ( ) ;
849
835
850
- const results = await allSettled < void , AckError > ( proms ) ;
836
+ const results = await Promise . allSettled < void > ( proms ) ;
851
837
assert . strictEqual ( results [ 0 ] . status , 'rejected' ) ;
852
838
assert . strictEqual ( results [ 0 ] . reason ?. errorCode , 'INVALID' ) ;
853
839
0 commit comments