retryinterceptor_test.go 1.3 KB

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