retryinterceptor_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package serverinterceptors
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/retry"
  7. "google.golang.org/grpc/metadata"
  8. )
  9. func TestRetryInterceptor(t *testing.T) {
  10. t.Run("retries exceeded", func(t *testing.T) {
  11. interceptor := RetryInterceptor(2)
  12. ctx := metadata.NewIncomingContext(context.Background(),
  13. metadata.New(map[string]string{retry.AttemptMetadataKey: "3"}))
  14. resp, err := interceptor(ctx, nil, nil, func(ctx context.Context,
  15. req interface{}) (interface{}, error) {
  16. return nil, nil
  17. })
  18. assert.Error(t, err)
  19. assert.Nil(t, resp)
  20. })
  21. t.Run("reasonable retries", func(t *testing.T) {
  22. interceptor := RetryInterceptor(2)
  23. ctx := metadata.NewIncomingContext(context.Background(),
  24. metadata.New(map[string]string{retry.AttemptMetadataKey: "2"}))
  25. resp, err := interceptor(ctx, nil, nil, func(ctx context.Context,
  26. req interface{}) (interface{}, error) {
  27. return nil, nil
  28. })
  29. assert.NoError(t, err)
  30. assert.Nil(t, resp)
  31. })
  32. t.Run("no retries", func(t *testing.T) {
  33. interceptor := RetryInterceptor(0)
  34. resp, err := interceptor(context.Background(), nil, nil,
  35. func(ctx context.Context, req interface{}) (interface{}, error) {
  36. return nil, nil
  37. })
  38. assert.NoError(t, err)
  39. assert.Nil(t, resp)
  40. })
  41. }