@@ -7,19 +7,46 @@ import (
77
88 "github.com/dapr/go-sdk/dapr/proto/runtime/v1"
99 "github.com/dapr/go-sdk/service/common"
10+ "github.com/golang/protobuf/ptypes/empty"
1011 "github.com/stretchr/testify/assert"
1112)
1213
13- func eventHandler (ctx context.Context , event * common.TopicEvent ) (retry bool , err error ) {
14- if event == nil {
15- return true , errors .New ("nil event" )
14+ func TestTopicErrors (t * testing.T ) {
15+ server := getTestServer ()
16+ err := server .AddTopicEventHandler (nil , nil )
17+ assert .Errorf (t , err , "expected error on nil sub" )
18+
19+ sub := & common.Subscription {}
20+ err = server .AddTopicEventHandler (sub , nil )
21+ assert .Errorf (t , err , "expected error on invalid sub" )
22+
23+ sub .PubsubName = "messages"
24+ err = server .AddTopicEventHandler (sub , nil )
25+ assert .Errorf (t , err , "expected error on sub without topic" )
26+
27+ sub .Topic = "test"
28+ err = server .AddTopicEventHandler (sub , nil )
29+ assert .Errorf (t , err , "expected error on sub without handler" )
30+ }
31+
32+ func TestTopicSubscriptionList (t * testing.T ) {
33+ sub := & common.Subscription {
34+ PubsubName : "messages" ,
35+ Topic : "test" ,
1636 }
17- return false , nil
37+ server := getTestServer ()
38+ err := server .AddTopicEventHandler (sub , eventHandler )
39+ assert .Nil (t , err )
40+ resp , err := server .ListTopicSubscriptions (context .Background (), & empty.Empty {})
41+ assert .NoError (t , err )
42+ assert .NotNil (t , resp )
43+ assert .Lenf (t , resp .Subscriptions , 1 , "expected 1 handlers" )
1844}
1945
2046// go test -timeout 30s ./service/grpc -count 1 -run ^TestTopic$
2147func TestTopic (t * testing.T ) {
2248 ctx := context .Background ()
49+
2350 sub := & common.Subscription {
2451 PubsubName : "messages" ,
2552 Topic : "test" ,
@@ -28,6 +55,7 @@ func TestTopic(t *testing.T) {
2855
2956 err := server .AddTopicEventHandler (sub , eventHandler )
3057 assert .Nil (t , err )
58+
3159 startTestServer (server )
3260
3361 t .Run ("topic event without request" , func (t * testing.T ) {
@@ -60,3 +88,75 @@ func TestTopic(t *testing.T) {
6088
6189 stopTestServer (t , server )
6290}
91+
92+ func TestTopicWithErrors (t * testing.T ) {
93+ ctx := context .Background ()
94+
95+ sub1 := & common.Subscription {
96+ PubsubName : "messages" ,
97+ Topic : "test1" ,
98+ }
99+
100+ sub2 := & common.Subscription {
101+ PubsubName : "messages" ,
102+ Topic : "test2" ,
103+ }
104+ server := getTestServer ()
105+
106+ err := server .AddTopicEventHandler (sub1 , eventHandlerWithRetryError )
107+ assert .Nil (t , err )
108+
109+ err = server .AddTopicEventHandler (sub2 , eventHandlerWithError )
110+ assert .Nil (t , err )
111+
112+ startTestServer (server )
113+
114+ t .Run ("topic event for retry error" , func (t * testing.T ) {
115+ in := & runtime.TopicEventRequest {
116+ Id : "a123" ,
117+ Source : "test" ,
118+ Type : "test" ,
119+ SpecVersion : "v1.0" ,
120+ DataContentType : "text/plain" ,
121+ Data : []byte ("test" ),
122+ Topic : sub1 .Topic ,
123+ PubsubName : sub1 .PubsubName ,
124+ }
125+ resp , err := server .OnTopicEvent (ctx , in )
126+ assert .Error (t , err )
127+ assert .Equal (t , resp .GetStatus (), runtime .TopicEventResponse_RETRY )
128+ })
129+
130+ t .Run ("topic event for error" , func (t * testing.T ) {
131+ in := & runtime.TopicEventRequest {
132+ Id : "a123" ,
133+ Source : "test" ,
134+ Type : "test" ,
135+ SpecVersion : "v1.0" ,
136+ DataContentType : "text/plain" ,
137+ Data : []byte ("test" ),
138+ Topic : sub2 .Topic ,
139+ PubsubName : sub2 .PubsubName ,
140+ }
141+ resp , err := server .OnTopicEvent (ctx , in )
142+ assert .Error (t , err )
143+ assert .Equal (t , resp .GetStatus (), runtime .TopicEventResponse_DROP )
144+ })
145+
146+ stopTestServer (t , server )
147+ }
148+
149+ func eventHandler (ctx context.Context , event * common.TopicEvent ) (retry bool , err error ) {
150+ if event == nil {
151+ return true , errors .New ("nil event" )
152+ }
153+ return false , nil
154+ }
155+
156+ func eventHandlerWithRetryError (ctx context.Context , event * common.TopicEvent ) (retry bool , err error ) {
157+ return true , errors .New ("nil event" )
158+ }
159+
160+ func eventHandlerWithError (ctx context.Context , event * common.TopicEvent ) (retry bool , err error ) {
161+ return false , errors .New ("nil event" )
162+ }
0 commit comments