mirror of https://github.com/dapr/go-sdk.git
adds grpc serving error tests (#102)
This commit is contained in:
parent
b7a5b80a89
commit
c49c3f1894
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/dapr/go-sdk/dapr/proto/runtime/v1"
|
||||
"github.com/dapr/go-sdk/service/common"
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -17,6 +18,27 @@ func testBindingHandler(ctx context.Context, in *common.BindingEvent) (out []byt
|
|||
return in.Data, nil
|
||||
}
|
||||
|
||||
func TestListInputBindings(t *testing.T) {
|
||||
server := getTestServer()
|
||||
err := server.AddBindingInvocationHandler("test1", testBindingHandler)
|
||||
assert.NoError(t, err)
|
||||
err = server.AddBindingInvocationHandler("test2", testBindingHandler)
|
||||
assert.NoError(t, err)
|
||||
resp, err := server.ListInputBindings(context.Background(), &empty.Empty{})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, resp)
|
||||
assert.Lenf(t, resp.Bindings, 2, "expected 2 handlers")
|
||||
}
|
||||
|
||||
func TestBindingForErrors(t *testing.T) {
|
||||
server := getTestServer()
|
||||
err := server.AddBindingInvocationHandler("", nil)
|
||||
assert.Errorf(t, err, "expected error on nil method name")
|
||||
|
||||
err = server.AddBindingInvocationHandler("test", nil)
|
||||
assert.Errorf(t, err, "expected error on nil method handler")
|
||||
}
|
||||
|
||||
// go test -timeout 30s ./service/grpc -count 1 -run ^TestBinding$
|
||||
func TestBinding(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/dapr/go-sdk/dapr/proto/common/v1"
|
||||
cc "github.com/dapr/go-sdk/service/common"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
@ -21,14 +22,31 @@ func testInvokeHandler(ctx context.Context, in *cc.InvocationEvent) (out *cc.Con
|
|||
return
|
||||
}
|
||||
|
||||
func testInvokeHandlerWithError(ctx context.Context, in *cc.InvocationEvent) (out *cc.Content, err error) {
|
||||
return nil, errors.New("test error")
|
||||
}
|
||||
|
||||
func TestInvokeErrors(t *testing.T) {
|
||||
server := getTestServer()
|
||||
err := server.AddServiceInvocationHandler("", nil)
|
||||
assert.Error(t, err)
|
||||
err = server.AddServiceInvocationHandler("test", nil)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// go test -timeout 30s ./service/grpc -count 1 -run ^TestInvoke$
|
||||
func TestInvoke(t *testing.T) {
|
||||
methodName := "test"
|
||||
methodNameWithError := "error"
|
||||
ctx := context.Background()
|
||||
|
||||
server := getTestServer()
|
||||
err := server.AddServiceInvocationHandler(methodName, testInvokeHandler)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = server.AddServiceInvocationHandler(methodNameWithError, testInvokeHandlerWithError)
|
||||
assert.Nil(t, err)
|
||||
|
||||
startTestServer(server)
|
||||
|
||||
t.Run("invoke without request", func(t *testing.T) {
|
||||
|
@ -61,5 +79,15 @@ func TestInvoke(t *testing.T) {
|
|||
assert.Equal(t, data, string(out.Data.Value))
|
||||
})
|
||||
|
||||
t.Run("invoke request with error", func(t *testing.T) {
|
||||
data := "hello there"
|
||||
dataContentType := "text/plain"
|
||||
in := &common.InvokeRequest{Method: methodNameWithError}
|
||||
in.Data = &anypb.Any{Value: []byte(data)}
|
||||
in.ContentType = dataContentType
|
||||
_, err := server.OnInvoke(ctx, in)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
stopTestServer(t, server)
|
||||
}
|
||||
|
|
|
@ -13,6 +13,16 @@ func TestServer(t *testing.T) {
|
|||
stopTestServer(t, server)
|
||||
}
|
||||
|
||||
func TestServerWithListener(t *testing.T) {
|
||||
server := NewServiceWithListener(bufconn.Listen(1024 * 1024))
|
||||
assert.NotNil(t, server)
|
||||
}
|
||||
|
||||
func TestService(t *testing.T) {
|
||||
_, err := NewService("")
|
||||
assert.Errorf(t, err, "expected error from lack of address")
|
||||
}
|
||||
|
||||
func getTestServer() *Server {
|
||||
return newService(bufconn.Listen(1024 * 1024))
|
||||
}
|
||||
|
|
|
@ -7,19 +7,46 @@ import (
|
|||
|
||||
"github.com/dapr/go-sdk/dapr/proto/runtime/v1"
|
||||
"github.com/dapr/go-sdk/service/common"
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func eventHandler(ctx context.Context, event *common.TopicEvent) (retry bool, err error) {
|
||||
if event == nil {
|
||||
return true, errors.New("nil event")
|
||||
func TestTopicErrors(t *testing.T) {
|
||||
server := getTestServer()
|
||||
err := server.AddTopicEventHandler(nil, nil)
|
||||
assert.Errorf(t, err, "expected error on nil sub")
|
||||
|
||||
sub := &common.Subscription{}
|
||||
err = server.AddTopicEventHandler(sub, nil)
|
||||
assert.Errorf(t, err, "expected error on invalid sub")
|
||||
|
||||
sub.PubsubName = "messages"
|
||||
err = server.AddTopicEventHandler(sub, nil)
|
||||
assert.Errorf(t, err, "expected error on sub without topic")
|
||||
|
||||
sub.Topic = "test"
|
||||
err = server.AddTopicEventHandler(sub, nil)
|
||||
assert.Errorf(t, err, "expected error on sub without handler")
|
||||
}
|
||||
|
||||
func TestTopicSubscriptionList(t *testing.T) {
|
||||
sub := &common.Subscription{
|
||||
PubsubName: "messages",
|
||||
Topic: "test",
|
||||
}
|
||||
return false, nil
|
||||
server := getTestServer()
|
||||
err := server.AddTopicEventHandler(sub, eventHandler)
|
||||
assert.Nil(t, err)
|
||||
resp, err := server.ListTopicSubscriptions(context.Background(), &empty.Empty{})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, resp)
|
||||
assert.Lenf(t, resp.Subscriptions, 1, "expected 1 handlers")
|
||||
}
|
||||
|
||||
// go test -timeout 30s ./service/grpc -count 1 -run ^TestTopic$
|
||||
func TestTopic(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
sub := &common.Subscription{
|
||||
PubsubName: "messages",
|
||||
Topic: "test",
|
||||
|
@ -28,6 +55,7 @@ func TestTopic(t *testing.T) {
|
|||
|
||||
err := server.AddTopicEventHandler(sub, eventHandler)
|
||||
assert.Nil(t, err)
|
||||
|
||||
startTestServer(server)
|
||||
|
||||
t.Run("topic event without request", func(t *testing.T) {
|
||||
|
@ -60,3 +88,75 @@ func TestTopic(t *testing.T) {
|
|||
|
||||
stopTestServer(t, server)
|
||||
}
|
||||
|
||||
func TestTopicWithErrors(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
sub1 := &common.Subscription{
|
||||
PubsubName: "messages",
|
||||
Topic: "test1",
|
||||
}
|
||||
|
||||
sub2 := &common.Subscription{
|
||||
PubsubName: "messages",
|
||||
Topic: "test2",
|
||||
}
|
||||
server := getTestServer()
|
||||
|
||||
err := server.AddTopicEventHandler(sub1, eventHandlerWithRetryError)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = server.AddTopicEventHandler(sub2, eventHandlerWithError)
|
||||
assert.Nil(t, err)
|
||||
|
||||
startTestServer(server)
|
||||
|
||||
t.Run("topic event for retry error", func(t *testing.T) {
|
||||
in := &runtime.TopicEventRequest{
|
||||
Id: "a123",
|
||||
Source: "test",
|
||||
Type: "test",
|
||||
SpecVersion: "v1.0",
|
||||
DataContentType: "text/plain",
|
||||
Data: []byte("test"),
|
||||
Topic: sub1.Topic,
|
||||
PubsubName: sub1.PubsubName,
|
||||
}
|
||||
resp, err := server.OnTopicEvent(ctx, in)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, resp.GetStatus(), runtime.TopicEventResponse_RETRY)
|
||||
})
|
||||
|
||||
t.Run("topic event for error", func(t *testing.T) {
|
||||
in := &runtime.TopicEventRequest{
|
||||
Id: "a123",
|
||||
Source: "test",
|
||||
Type: "test",
|
||||
SpecVersion: "v1.0",
|
||||
DataContentType: "text/plain",
|
||||
Data: []byte("test"),
|
||||
Topic: sub2.Topic,
|
||||
PubsubName: sub2.PubsubName,
|
||||
}
|
||||
resp, err := server.OnTopicEvent(ctx, in)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, resp.GetStatus(), runtime.TopicEventResponse_DROP)
|
||||
})
|
||||
|
||||
stopTestServer(t, server)
|
||||
}
|
||||
|
||||
func eventHandler(ctx context.Context, event *common.TopicEvent) (retry bool, err error) {
|
||||
if event == nil {
|
||||
return true, errors.New("nil event")
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func eventHandlerWithRetryError(ctx context.Context, event *common.TopicEvent) (retry bool, err error) {
|
||||
return true, errors.New("nil event")
|
||||
}
|
||||
|
||||
func eventHandlerWithError(ctx context.Context, event *common.TopicEvent) (retry bool, err error) {
|
||||
return false, errors.New("nil event")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue