@@ -24,10 +24,67 @@ public void ValidateAnnotations()
2424
2525 const string correctAnnotationKey = "x-otp-annotation-key" ;
2626 // This should not throw an exception because the annotation key starts with "x-"
27- Utils . ValidateMessageAnnotations ( new Dictionary < string , object >
28- {
29- { correctAnnotationKey , annotationValue }
30- } ) ;
27+ Utils . ValidateMessageAnnotations ( new Dictionary < string , object > { { correctAnnotationKey , annotationValue } } ) ;
28+ }
29+
30+
31+ [ Fact ]
32+ public async Task RequeuedMessageWithAnnotationShouldContainAnnotationsOnRedelivery ( )
33+ {
34+ Assert . NotNull ( _connection ) ;
35+ Assert . NotNull ( _management ) ;
36+ TaskCompletionSource < bool > tcsRequeue =
37+ new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
38+
39+ const string annotationKey = "x-opt-annotation-key" ;
40+ const string annotationValue = "annotation-value" ;
41+
42+ const string annotationKey1 = "x-opt-annotation1-key" ;
43+ const string annotationValue1 = "annotation1-value" ;
44+
45+ int requeueCount = 0 ;
46+
47+
48+ await _management . Queue ( ) . Type ( QueueType . QUORUM ) . Name ( _queueName ) . DeclareAsync ( ) ;
49+ List < IMessage > messages = [ ] ;
50+ IPublisher publisher = await _connection . PublisherBuilder ( ) . Queue ( _queueName ) . BuildAsync ( ) ;
51+ IConsumer consumer = await _connection . ConsumerBuilder ( ) . MessageHandler (
52+ async ( context , message ) =>
53+ {
54+ messages . Add ( message ) ;
55+ if ( requeueCount == 0 )
56+ {
57+ requeueCount ++ ;
58+ await context . RequeueAsync ( new Dictionary < string , object >
59+ {
60+ { annotationKey , annotationValue } , { annotationKey1 , annotationValue1 }
61+ } ) ;
62+ }
63+ else
64+ {
65+ await context . AcceptAsync ( ) ;
66+ tcsRequeue . SetResult ( true ) ;
67+ }
68+ }
69+ ) . Queue ( _queueName ) . BuildAndStartAsync ( ) ;
70+
71+ IMessage message = new AmqpMessage ( $ "message") ;
72+ PublishResult pr = await publisher . PublishAsync ( message ) ;
73+
74+ Assert . Equal ( OutcomeState . Accepted , pr . Outcome . State ) ;
75+
76+ await tcsRequeue . Task . WaitAsync ( TimeSpan . FromSeconds ( 5 ) ) ;
77+
78+ Assert . Equal ( 2 , messages . Count ) ;
79+ Assert . Null ( messages [ 0 ] . Annotation ( annotationKey ) ) ;
80+ Assert . Null ( messages [ 0 ] . Annotation ( annotationKey1 ) ) ;
81+ Assert . Null ( messages [ 0 ] . Annotation ( "x-delivery-count" ) ) ;
82+
83+ Assert . Equal ( messages [ 1 ] . Annotation ( annotationKey ) , annotationValue ) ;
84+ Assert . Equal ( messages [ 1 ] . Annotation ( annotationKey1 ) , annotationValue1 ) ;
85+ Assert . NotNull ( messages [ 1 ] . Annotation ( "x-delivery-count" ) ) ;
86+
87+ await consumer . CloseAsync ( ) ;
3188 }
3289
3390 [ Fact ]
@@ -67,7 +124,6 @@ public async Task DiscardedMessageWithAnnotationsShouldBeDeadLeadLetteredAndCont
67124 } ) . Queue ( dlqQueueName ) . BuildAndStartAsync ( ) ;
68125
69126 IMessage mResult = await tcsDl . Task . WaitAsync ( TimeSpan . FromSeconds ( 5 ) ) ;
70-
71127 Assert . NotNull ( mResult ) ;
72128 Assert . Equal ( mResult . Annotation ( annotationKey ) , annotationValue ) ;
73129 await dlConsumer . CloseAsync ( ) ;
0 commit comments