Bladeren bron

add more tests

kevin 4 jaren geleden
bovenliggende
commit
9b8595a85e
3 gewijzigde bestanden met toevoegingen van 99 en 0 verwijderingen
  1. 30 0
      rpcx/internal/client_test.go
  2. 16 0
      rpcx/internal/rpcserver_test.go
  3. 53 0
      rpcx/internal/server_test.go

+ 30 - 0
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)
+}

+ 16 - 0
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)
+}

+ 53 - 0
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)
+}