@@ -18,6 +18,52 @@ var _ = Describe("RpcClient", func() {
1818 publisher * Publisher
1919 )
2020
21+ var pongRpcServer = func (ctx context.Context , publisher * Publisher , consumer * Consumer ) {
22+ for {
23+ select {
24+ case <- ctx .Done ():
25+ return
26+ default :
27+ // Receive a message from the server consumer
28+ receivedMessage , err := consumer .Receive (ctx )
29+ if err != nil {
30+ // Exit if we can't receive messages (e.g.,
31+ // consumer is closed)
32+ GinkgoWriter .Printf ("Error receiving message: %v\n " , err )
33+ return
34+ }
35+
36+ msg := receivedMessage .Message ()
37+ if msg == nil {
38+ GinkgoWriter .Printf ("Received nil message\n " )
39+ continue
40+ }
41+
42+ // Create response with "Pong: " prefix
43+ responseData := "Pong: " + string (msg .GetData ())
44+ replyMessage := amqp .NewMessage ([]byte (responseData ))
45+
46+ // Copy correlation ID and reply-to from request
47+ if msg .Properties != nil {
48+ if replyMessage .Properties == nil {
49+ replyMessage .Properties = & amqp.MessageProperties {}
50+ }
51+ replyMessage .Properties .CorrelationID =
52+ msg .Properties .MessageID
53+ }
54+
55+ // Send reply to the specified reply-to address
56+ if msg .Properties != nil && msg .Properties .ReplyTo != nil {
57+ replyMessage .Properties .To = msg .Properties .ReplyTo
58+ }
59+
60+ copyApplicationProperties (msg , replyMessage )
61+
62+ publisher .Publish (ctx , replyMessage )
63+ }
64+ }
65+ }
66+
2167 BeforeEach (func () {
2268 queueName = generateNameWithDateTime (CurrentSpecReport ().LeafNodeText )
2369 var err error
@@ -37,49 +83,7 @@ var _ = Describe("RpcClient", func() {
3783
3884 It ("should send a request and receive replies" , func (ctx SpecContext ) {
3985 // Server goroutine to handle incoming requests
40- go func () {
41- for {
42- select {
43- case <- ctx .Done ():
44- return
45- default :
46- // Receive a message from the server consumer
47- receivedMessage , err := consumer .Receive (ctx )
48- if err != nil {
49- // Exit if we can't receive messages (e.g.,
50- // consumer is closed)
51- GinkgoWriter .Printf ("Error receiving message: %v\n " , err )
52- return
53- }
54-
55- msg := receivedMessage .Message ()
56- if msg == nil {
57- GinkgoWriter .Printf ("Received nil message\n " )
58- continue
59- }
60-
61- // Create response with "Pong: " prefix
62- responseData := "Pong: " + string (msg .GetData ())
63- replyMessage := amqp .NewMessage ([]byte (responseData ))
64-
65- // Copy correlation ID and reply-to from request
66- if msg .Properties != nil {
67- if replyMessage .Properties == nil {
68- replyMessage .Properties = & amqp.MessageProperties {}
69- }
70- replyMessage .Properties .CorrelationID =
71- msg .Properties .MessageID
72- }
73-
74- // Send reply to the specified reply-to address
75- if msg .Properties != nil && msg .Properties .ReplyTo != nil {
76- replyMessage .Properties .To = msg .Properties .ReplyTo
77- }
78-
79- publisher .Publish (ctx , replyMessage )
80- }
81- }
82- }()
86+ go pongRpcServer (ctx , publisher , consumer )
8387
8488 client , err := conn .NewRpcClient (ctx , & RpcClientOptions {
8589 RequestQueueName : queueName ,
@@ -103,4 +107,46 @@ var _ = Describe("RpcClient", func() {
103107 }
104108 Ω (client .Close (ctx )).Should (Succeed ())
105109 })
110+
111+ It ("uses a custom correlation id extractor and post processor" , func (ctx SpecContext ) {
112+ go pongRpcServer (ctx , publisher , consumer )
113+ client , err := conn .NewRpcClient (ctx , & RpcClientOptions {
114+ RequestQueueName : queueName ,
115+ CorrelationIdExtractor : func (message * amqp.Message ) any {
116+ return message .ApplicationProperties ["correlationId" ]
117+ },
118+ RequestPostProcessor : func (request * amqp.Message , correlationID any ) * amqp.Message {
119+ if request .ApplicationProperties == nil {
120+ request .ApplicationProperties = make (map [string ]any )
121+ }
122+ request .ApplicationProperties ["correlationId" ] = correlationID
123+ if request .Properties == nil {
124+ request .Properties = & amqp.MessageProperties {}
125+ }
126+ request .Properties .MessageID = correlationID
127+
128+ return request
129+ },
130+ })
131+ Ω (err ).ShouldNot (HaveOccurred ())
132+ DeferCleanup (func (ctx SpecContext ) {
133+ // Closing twice in case the test fails and the 'happy path' close is not called
134+ _ = client .Close (ctx )
135+ })
136+
137+ request := client .Message ([]byte ("Using a custom correlation id extractor and post processor" ))
138+ request .ApplicationProperties = map [string ]any {"this-property" : "should-be-preserved" }
139+ replyCh , err := client .Publish (ctx , request )
140+ Ω (err ).ShouldNot (HaveOccurred ())
141+
142+ actualReply := & amqp.Message {}
143+ Eventually (replyCh ).
144+ Within (time .Second ).
145+ WithPolling (time .Millisecond * 100 ).
146+ Should (Receive (& actualReply ))
147+ Expect (actualReply .GetData ()).To (BeEquivalentTo ("Pong: Using a custom correlation id extractor and post processor" ))
148+ Expect (actualReply .ApplicationProperties ).To (HaveKey ("correlationId" ))
149+ Expect (actualReply .ApplicationProperties ).To (HaveKeyWithValue ("this-property" , "should-be-preserved" ))
150+ Ω (client .Close (ctx )).Should (Succeed ())
151+ })
106152})
0 commit comments