server_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package internal
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/stat"
  7. "google.golang.org/grpc"
  8. )
  9. func TestBaseRpcServer_AddOptions(t *testing.T) {
  10. metrics := stat.NewMetrics("foo")
  11. server := newBaseRpcServer("foo", metrics)
  12. server.SetName("bar")
  13. var opt grpc.EmptyServerOption
  14. server.AddOptions(opt)
  15. assert.Contains(t, server.options, opt)
  16. }
  17. func TestBaseRpcServer_AddStreamInterceptors(t *testing.T) {
  18. metrics := stat.NewMetrics("foo")
  19. server := newBaseRpcServer("foo", metrics)
  20. server.SetName("bar")
  21. var vals []int
  22. f := func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
  23. vals = append(vals, 1)
  24. return nil
  25. }
  26. server.AddStreamInterceptors(f)
  27. for _, each := range server.streamInterceptors {
  28. assert.Nil(t, each(nil, nil, nil, nil))
  29. }
  30. assert.ElementsMatch(t, []int{1}, vals)
  31. }
  32. func TestBaseRpcServer_AddUnaryInterceptors(t *testing.T) {
  33. metrics := stat.NewMetrics("foo")
  34. server := newBaseRpcServer("foo", metrics)
  35. server.SetName("bar")
  36. var vals []int
  37. f := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (
  38. resp interface{}, err error) {
  39. vals = append(vals, 1)
  40. return nil, nil
  41. }
  42. server.AddUnaryInterceptors(f)
  43. for _, each := range server.unaryInterceptors {
  44. _, err := each(context.Background(), nil, nil, nil)
  45. assert.Nil(t, err)
  46. }
  47. assert.ElementsMatch(t, []int{1}, vals)
  48. }