@@ -24,6 +24,7 @@ import (
2424 "net/http/httptest"
2525 "strings"
2626 "testing"
27+ "time"
2728
2829 cloudevents "github.com/cloudevents/sdk-go/v2"
2930 cehttp "github.com/cloudevents/sdk-go/v2/protocol/http"
@@ -210,6 +211,63 @@ func TestHandlerServeHttp(t *testing.T) {
210211 }
211212}
212213
214+ func TestHandleReplyEventIgnoresDuplicateReplyWithoutBlocking (t * testing.T ) {
215+ t .Parallel ()
216+
217+ rr := makeRequestReply ("my-request-reply" , "default" )
218+ inflight := cloudevents .NewEvent ()
219+ inflight .SetID ("1234567890" )
220+
221+ firstReply := inflight .Clone ()
222+ if err := SetCorrelationId (& firstReply , "replyid" , exampleKey , 0 ); err != nil {
223+ t .Fatalf ("failed to create first reply id: %v" , err )
224+ }
225+
226+ duplicateReply := inflight .Clone ()
227+ if err := SetCorrelationId (& duplicateReply , "replyid" , exampleKey , 0 ); err != nil {
228+ t .Fatalf ("failed to create duplicate reply id: %v" , err )
229+ }
230+
231+ keyStore := & AESKeyStore {}
232+ keyStore .addAesKey (rr .GetNamespacedName (), "key" , exampleKey )
233+
234+ handler := & IngressHandler {
235+ logger : zap .NewNop (),
236+ keyStore : keyStore ,
237+ entries : map [types.NamespacedName ]map [string ]* proxiedRequest {
238+ rr .GetNamespacedName (): {
239+ inflight .ID (): {
240+ responseWriter : httptest .NewRecorder (),
241+ replyEvent : make (chan * cloudevents.Event , 1 ),
242+ },
243+ },
244+ },
245+ }
246+
247+ // Fill the reply channel once to simulate an already delivered reply.
248+ pr , ok := handler .getEvent (inflight .ID (), rr )
249+ if ! ok {
250+ t .Fatal ("expected inflight request to exist" )
251+ }
252+ pr .replyEvent <- & firstReply
253+
254+ recorder := httptest .NewRecorder ()
255+ done := make (chan struct {})
256+
257+ go func () {
258+ handler .handleReplyEvent (recorder , & duplicateReply , rr )
259+ close (done )
260+ }()
261+
262+ select {
263+ case <- done :
264+ case <- time .After (2 * time .Second ):
265+ t .Fatal ("handleReplyEvent blocked on duplicate reply" )
266+ }
267+
268+ assert .Equal (t , http .StatusAccepted , recorder .Result ().StatusCode )
269+ }
270+
213271type testServerHandler struct {
214272 makeReplyEvent func (e * cloudevents.Event ) * cloudevents.Event
215273 callbackHandler http.Handler
0 commit comments