@@ -18,11 +18,13 @@ import (
18
18
type mockProvider struct {
19
19
createCount int32
20
20
* destregistry.BaseProvider
21
+ publishDelay time.Duration
21
22
}
22
23
23
24
type mockPublisher struct {
24
- id int64
25
- closed bool
25
+ id int64
26
+ closed bool
27
+ publishDelay time.Duration
26
28
}
27
29
28
30
var mockPublisherID int64
@@ -90,10 +92,20 @@ func (p *mockProvider) Validate(ctx context.Context, dest *models.Destination) e
90
92
91
93
func (p * mockProvider ) CreatePublisher (ctx context.Context , dest * models.Destination ) (destregistry.Publisher , error ) {
92
94
atomic .AddInt32 (& p .createCount , 1 )
93
- return newMockPublisher (), nil
95
+ pub := newMockPublisher ()
96
+ pub .publishDelay = p .publishDelay
97
+ return pub , nil
98
+ }
99
+
100
+ func (p * mockPublisher ) Publish (ctx context.Context , event * models.Event ) error {
101
+ select {
102
+ case <- time .After (p .publishDelay ):
103
+ return nil
104
+ case <- ctx .Done ():
105
+ return ctx .Err ()
106
+ }
94
107
}
95
108
96
- func (p * mockPublisher ) Publish (ctx context.Context , event * models.Event ) error { return nil }
97
109
func (p * mockPublisher ) Close () error {
98
110
p .closed = true
99
111
return nil
@@ -576,3 +588,57 @@ func TestObfuscateDestination(t *testing.T) {
576
588
assert .Equal (t , "****" , obfuscated .Credentials ["token" ]) // 3 chars
577
589
assert .Equal (t , "****" , obfuscated .Credentials ["code" ]) // 4 chars
578
590
}
591
+
592
+ func TestPublishEventTimeout (t * testing.T ) {
593
+ timeout := 100 * time .Millisecond
594
+ logger := testutil .CreateTestLogger (t )
595
+
596
+ t .Run ("should not return timeout error when publish completes within timeout" , func (t * testing.T ) {
597
+ registry := destregistry .NewRegistry (& destregistry.Config {
598
+ DeliveryTimeout : timeout ,
599
+ }, logger )
600
+
601
+ provider , err := newMockProvider ()
602
+ require .NoError (t , err )
603
+ provider .publishDelay = timeout / 2
604
+ err = registry .RegisterProvider ("test" , provider )
605
+ require .NoError (t , err )
606
+
607
+ destination := & models.Destination {
608
+ Type : "test" ,
609
+ }
610
+ event := & models.Event {}
611
+
612
+ err = registry .PublishEvent (context .Background (), destination , event )
613
+ assert .NoError (t , err )
614
+ })
615
+
616
+ t .Run ("should return timeout error when publish exceeds timeout" , func (t * testing.T ) {
617
+ registry := destregistry .NewRegistry (& destregistry.Config {
618
+ DeliveryTimeout : timeout ,
619
+ }, logger )
620
+
621
+ provider , err := newMockProvider ()
622
+ require .NoError (t , err )
623
+ provider .publishDelay = timeout * 2
624
+ err = registry .RegisterProvider ("test" , provider )
625
+ require .NoError (t , err )
626
+
627
+ destination := & models.Destination {
628
+ Type : "test" ,
629
+ }
630
+ event := & models.Event {}
631
+
632
+ err = registry .PublishEvent (context .Background (), destination , event )
633
+ assert .Error (t , err )
634
+
635
+ var publishErr * destregistry.ErrDestinationPublishAttempt
636
+ assert .ErrorAs (t , err , & publishErr )
637
+ assert .Equal (t , "test" , publishErr .Provider )
638
+
639
+ data , ok := publishErr .Data .(map [string ]interface {})
640
+ assert .True (t , ok , "Expected Data to be map[string]interface{}" )
641
+ assert .Equal (t , "timeout" , data ["error" ])
642
+ assert .Equal (t , timeout .String (), data ["timeout" ])
643
+ })
644
+ }
0 commit comments