chainclientinterceptors_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package internal
  2. import (
  3. "context"
  4. "sync/atomic"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "google.golang.org/grpc"
  8. )
  9. func TestWithStreamClientInterceptors(t *testing.T) {
  10. opts := WithStreamClientInterceptors()
  11. assert.NotNil(t, opts)
  12. }
  13. func TestWithUnaryClientInterceptors(t *testing.T) {
  14. opts := WithUnaryClientInterceptors()
  15. assert.NotNil(t, opts)
  16. }
  17. func TestChainStreamClientInterceptors_zero(t *testing.T) {
  18. interceptors := chainStreamClientInterceptors()
  19. _, err := interceptors(context.Background(), nil, new(grpc.ClientConn), "/foo",
  20. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  21. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  22. return nil, nil
  23. })
  24. assert.Nil(t, err)
  25. }
  26. func TestChainStreamClientInterceptors_one(t *testing.T) {
  27. var called int32
  28. interceptors := chainStreamClientInterceptors(func(ctx context.Context, desc *grpc.StreamDesc,
  29. cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (
  30. grpc.ClientStream, error) {
  31. atomic.AddInt32(&called, 1)
  32. return nil, nil
  33. })
  34. _, err := interceptors(context.Background(), nil, new(grpc.ClientConn), "/foo",
  35. func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
  36. opts ...grpc.CallOption) (grpc.ClientStream, error) {
  37. return nil, nil
  38. })
  39. assert.Nil(t, err)
  40. assert.Equal(t, int32(1), atomic.LoadInt32(&called))
  41. }