22// and the Mozilla Public License, version 2.0.
33// Copyright (c) 2017-2024 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
44
5+ using System ;
56using System . Collections . Generic ;
67using System . Threading . Tasks ;
78using RabbitMQ . AMQP . Client ;
@@ -31,7 +32,7 @@ public async Task BatchAcceptDisposition()
3132 {
3233 Assert . NotNull ( batch ) ;
3334 batch . Add ( context ) ;
34- if ( batch . Size ( ) != batchSize )
35+ if ( batch . Count ( ) != batchSize )
3536 {
3637 return Task . CompletedTask ;
3738 }
@@ -70,7 +71,7 @@ public async Task BatchDiscardDisposition()
7071 {
7172 Assert . NotNull ( batch ) ;
7273 batch . Add ( context ) ;
73- if ( batch . Size ( ) != batchSize )
74+ if ( batch . Count ( ) != batchSize )
7475 {
7576 return Task . CompletedTask ;
7677 }
@@ -109,7 +110,7 @@ public async Task BatchDiscardAnnotationDisposition()
109110 {
110111 Assert . NotNull ( batch ) ;
111112 batch . Add ( context ) ;
112- if ( batch . Size ( ) != batchSize )
113+ if ( batch . Count ( ) != batchSize )
113114 {
114115 return Task . CompletedTask ;
115116 }
@@ -151,7 +152,7 @@ public async Task BatchRequeueDisposition()
151152 {
152153 Assert . NotNull ( batch ) ;
153154 batch . Add ( context ) ;
154- if ( batch . Size ( ) != batchSize )
155+ if ( batch . Count ( ) != batchSize )
155156 {
156157 return Task . CompletedTask ;
157158 }
@@ -188,7 +189,7 @@ public async Task BatchRequeueAnnotationsDisposition()
188189 {
189190 Assert . NotNull ( batch ) ;
190191 batch . Add ( context ) ;
191- if ( batch . Size ( ) != batchSize )
192+ if ( batch . Count ( ) != batchSize )
192193 {
193194 return Task . CompletedTask ;
194195 }
@@ -198,23 +199,68 @@ public async Task BatchRequeueAnnotationsDisposition()
198199
199200 const string annotationKey1 = "x-opt-annotation1-key" ;
200201 const string annotationValue1 = "annotation1-value" ;
201-
202+ Assert . Equal ( batchSize , batch . Count ( ) ) ;
202203 batch . Requeue ( new Dictionary < string , object > ( )
203204 {
204205 { annotationKey , annotationValue } , { annotationKey1 , annotationValue1 }
205206 } ) ;
207+ Assert . Equal ( 0 , batch . Count ( ) ) ;
208+
206209 tcs . SetResult ( true ) ;
207210
208211 return Task . CompletedTask ;
209212 } )
210213 . BuildAndStartAsync ( ) ;
211214
212215 Assert . NotNull ( consumer ) ;
213- await tcs . Task ;
216+ await tcs . Task . WaitAsync ( TimeSpan . FromSeconds ( 20 ) ) ;
214217 await consumer . CloseAsync ( ) ;
215218 await WaitUntilQueueMessageCount ( queueSpec , 18 ) ;
219+ await queueSpec . DeleteAsync ( ) ;
220+ }
221+
222+ [ Fact ]
223+ public async Task MixBatchAcceptAndDiscardDisposition ( )
224+ {
225+ Assert . NotNull ( _connection ) ;
226+ Assert . NotNull ( _management ) ;
227+
228+ IQueueSpecification queueSpec = _management . Queue ( ) . Name ( _queueName ) ;
229+ await queueSpec . DeclareAsync ( ) ;
230+ const int batchSize = 18 ;
231+ await PublishAsync ( queueSpec , batchSize * 2 ) ;
232+ BatchDeliveryContext batch = new ( ) ;
233+ TaskCompletionSource < bool > tcs = new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
234+ bool acceptNext = true ; // Flag to alternate between accept and discard
235+ IConsumer consumer = await _connection . ConsumerBuilder ( )
236+ . Queue ( queueSpec )
237+ . MessageHandler ( ( context , _ ) =>
238+ {
239+ Assert . NotNull ( batch ) ;
240+ batch . Add ( context ) ;
241+ if ( batch . Count ( ) == batchSize && acceptNext )
242+ {
243+ Assert . Equal ( batchSize , batch . Count ( ) ) ;
244+ batch . Accept ( ) ;
245+ acceptNext = false ; // Switch to discard next
246+ }
247+ else if ( batch . Count ( ) == batchSize && ! acceptNext )
248+ {
249+ Assert . Equal ( batchSize , batch . Count ( ) ) ;
250+ batch . Discard ( ) ;
251+ tcs . SetResult ( true ) ;
252+ }
253+ return Task . CompletedTask ;
254+ } )
255+ . BuildAndStartAsync ( ) ;
256+
257+ Assert . NotNull ( consumer ) ;
258+ await tcs . Task ;
259+
216260 Assert . Equal ( 0 , consumer . UnsettledMessageCount ) ;
261+ await WaitUntilQueueMessageCount ( queueSpec , 0 ) ;
217262 await queueSpec . DeleteAsync ( ) ;
263+ await consumer . CloseAsync ( ) ;
218264 }
219265 }
220266}
0 commit comments