Skip to content

Commit c49c3f1

Browse files
authored
adds grpc serving error tests (#102)
1 parent b7a5b80 commit c49c3f1

File tree

4 files changed

+164
-4
lines changed

4 files changed

+164
-4
lines changed

service/grpc/binding_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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

@@ -17,6 +18,27 @@ func testBindingHandler(ctx context.Context, in *common.BindingEvent) (out []byt
1718
return in.Data, nil
1819
}
1920

21+
func TestListInputBindings(t *testing.T) {
22+
server := getTestServer()
23+
err := server.AddBindingInvocationHandler("test1", testBindingHandler)
24+
assert.NoError(t, err)
25+
err = server.AddBindingInvocationHandler("test2", testBindingHandler)
26+
assert.NoError(t, err)
27+
resp, err := server.ListInputBindings(context.Background(), &empty.Empty{})
28+
assert.NoError(t, err)
29+
assert.NotNil(t, resp)
30+
assert.Lenf(t, resp.Bindings, 2, "expected 2 handlers")
31+
}
32+
33+
func TestBindingForErrors(t *testing.T) {
34+
server := getTestServer()
35+
err := server.AddBindingInvocationHandler("", nil)
36+
assert.Errorf(t, err, "expected error on nil method name")
37+
38+
err = server.AddBindingInvocationHandler("test", nil)
39+
assert.Errorf(t, err, "expected error on nil method handler")
40+
}
41+
2042
// go test -timeout 30s ./service/grpc -count 1 -run ^TestBinding$
2143
func TestBinding(t *testing.T) {
2244
ctx := context.Background()

service/grpc/invoke_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/dapr/go-sdk/dapr/proto/common/v1"
88
cc "github.com/dapr/go-sdk/service/common"
9+
"github.com/pkg/errors"
910
"github.com/stretchr/testify/assert"
1011
"google.golang.org/protobuf/types/known/anypb"
1112
)
@@ -21,14 +22,31 @@ func testInvokeHandler(ctx context.Context, in *cc.InvocationEvent) (out *cc.Con
2122
return
2223
}
2324

25+
func testInvokeHandlerWithError(ctx context.Context, in *cc.InvocationEvent) (out *cc.Content, err error) {
26+
return nil, errors.New("test error")
27+
}
28+
29+
func TestInvokeErrors(t *testing.T) {
30+
server := getTestServer()
31+
err := server.AddServiceInvocationHandler("", nil)
32+
assert.Error(t, err)
33+
err = server.AddServiceInvocationHandler("test", nil)
34+
assert.Error(t, err)
35+
}
36+
2437
// go test -timeout 30s ./service/grpc -count 1 -run ^TestInvoke$
2538
func TestInvoke(t *testing.T) {
2639
methodName := "test"
40+
methodNameWithError := "error"
2741
ctx := context.Background()
2842

2943
server := getTestServer()
3044
err := server.AddServiceInvocationHandler(methodName, testInvokeHandler)
3145
assert.Nil(t, err)
46+
47+
err = server.AddServiceInvocationHandler(methodNameWithError, testInvokeHandlerWithError)
48+
assert.Nil(t, err)
49+
3250
startTestServer(server)
3351

3452
t.Run("invoke without request", func(t *testing.T) {
@@ -61,5 +79,15 @@ func TestInvoke(t *testing.T) {
6179
assert.Equal(t, data, string(out.Data.Value))
6280
})
6381

82+
t.Run("invoke request with error", func(t *testing.T) {
83+
data := "hello there"
84+
dataContentType := "text/plain"
85+
in := &common.InvokeRequest{Method: methodNameWithError}
86+
in.Data = &anypb.Any{Value: []byte(data)}
87+
in.ContentType = dataContentType
88+
_, err := server.OnInvoke(ctx, in)
89+
assert.Error(t, err)
90+
})
91+
6492
stopTestServer(t, server)
6593
}

service/grpc/service_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ func TestServer(t *testing.T) {
1313
stopTestServer(t, server)
1414
}
1515

16+
func TestServerWithListener(t *testing.T) {
17+
server := NewServiceWithListener(bufconn.Listen(1024 * 1024))
18+
assert.NotNil(t, server)
19+
}
20+
21+
func TestService(t *testing.T) {
22+
_, err := NewService("")
23+
assert.Errorf(t, err, "expected error from lack of address")
24+
}
25+
1626
func getTestServer() *Server {
1727
return newService(bufconn.Listen(1024 * 1024))
1828
}

service/grpc/topic_test.go

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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$
2147
func 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

Comments
 (0)