diff --git a/rpcx/internal/client_test.go b/rpcx/internal/client_test.go new file mode 100644 index 00000000..00b4875c --- /dev/null +++ b/rpcx/internal/client_test.go @@ -0,0 +1,30 @@ +package internal + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "google.golang.org/grpc" +) + +func TestWithDialOption(t *testing.T) { + var options ClientOptions + agent := grpc.WithUserAgent("chrome") + opt := WithDialOption(agent) + opt(&options) + assert.Contains(t, options.DialOptions, agent) +} + +func TestWithTimeout(t *testing.T) { + var options ClientOptions + opt := WithTimeout(time.Second) + opt(&options) + assert.Equal(t, time.Second, options.Timeout) +} + +func TestBuildDialOptions(t *testing.T) { + agent := grpc.WithUserAgent("chrome") + opts := buildDialOptions(WithDialOption(agent)) + assert.Contains(t, opts, agent) +} diff --git a/rpcx/internal/rpcserver_test.go b/rpcx/internal/rpcserver_test.go new file mode 100644 index 00000000..9c1a9b9e --- /dev/null +++ b/rpcx/internal/rpcserver_test.go @@ -0,0 +1,16 @@ +package internal + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/stat" +) + +func TestWithMetrics(t *testing.T) { + metrics := stat.NewMetrics("foo") + opt := WithMetrics(metrics) + var options rpcServerOptions + opt(&options) + assert.Equal(t, metrics, options.metrics) +} diff --git a/rpcx/internal/server_test.go b/rpcx/internal/server_test.go new file mode 100644 index 00000000..c4daf9f1 --- /dev/null +++ b/rpcx/internal/server_test.go @@ -0,0 +1,53 @@ +package internal + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/stat" + "google.golang.org/grpc" +) + +func TestBaseRpcServer_AddOptions(t *testing.T) { + metrics := stat.NewMetrics("foo") + server := newBaseRpcServer("foo", metrics) + server.SetName("bar") + var opt grpc.EmptyServerOption + server.AddOptions(opt) + assert.Contains(t, server.options, opt) +} + +func TestBaseRpcServer_AddStreamInterceptors(t *testing.T) { + metrics := stat.NewMetrics("foo") + server := newBaseRpcServer("foo", metrics) + server.SetName("bar") + var vals []int + f := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { + vals = append(vals, 1) + return nil + } + server.AddStreamInterceptors(f) + for _, each := range server.streamInterceptors { + assert.Nil(t, each(nil, nil, nil, nil)) + } + assert.ElementsMatch(t, []int{1}, vals) +} + +func TestBaseRpcServer_AddUnaryInterceptors(t *testing.T) { + metrics := stat.NewMetrics("foo") + server := newBaseRpcServer("foo", metrics) + server.SetName("bar") + var vals []int + f := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) ( + resp interface{}, err error) { + vals = append(vals, 1) + return nil, nil + } + server.AddUnaryInterceptors(f) + for _, each := range server.unaryInterceptors { + _, err := each(context.Background(), nil, nil, nil) + assert.Nil(t, err) + } + assert.ElementsMatch(t, []int{1}, vals) +}