retrier_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "errors"
  8. "net/http"
  9. "sync/atomic"
  10. "testing"
  11. "time"
  12. )
  13. type testRetrier struct {
  14. Retrier
  15. N int64
  16. Err error
  17. }
  18. func (r *testRetrier) Retry(ctx context.Context, retry int, req *http.Request, resp *http.Response, err error) (time.Duration, bool, error) {
  19. atomic.AddInt64(&r.N, 1)
  20. if r.Err != nil {
  21. return 0, false, r.Err
  22. }
  23. return r.Retrier.Retry(ctx, retry, req, resp, err)
  24. }
  25. func TestStopRetrier(t *testing.T) {
  26. r := NewStopRetrier()
  27. wait, ok, err := r.Retry(context.TODO(), 1, nil, nil, nil)
  28. if want, got := 0*time.Second, wait; want != got {
  29. t.Fatalf("expected %v, got %v", want, got)
  30. }
  31. if want, got := false, ok; want != got {
  32. t.Fatalf("expected %v, got %v", want, got)
  33. }
  34. if err != nil {
  35. t.Fatalf("expected nil, got %v", err)
  36. }
  37. }
  38. func TestRetrier(t *testing.T) {
  39. var numFailedReqs int
  40. fail := func(r *http.Request) (*http.Response, error) {
  41. numFailedReqs += 1
  42. //return &http.Response{Request: r, StatusCode: 400}, nil
  43. return nil, errors.New("request failed")
  44. }
  45. tr := &failingTransport{path: "/fail", fail: fail}
  46. httpClient := &http.Client{Transport: tr}
  47. retrier := &testRetrier{
  48. Retrier: NewBackoffRetrier(NewSimpleBackoff(100, 100, 100, 100, 100)),
  49. }
  50. client, err := NewClient(
  51. SetHttpClient(httpClient),
  52. SetMaxRetries(5),
  53. SetHealthcheck(false),
  54. SetRetrier(retrier))
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. res, err := client.PerformRequest(context.TODO(), "GET", "/fail", nil, nil)
  59. if err == nil {
  60. t.Fatal("expected error")
  61. }
  62. if res != nil {
  63. t.Fatal("expected no response")
  64. }
  65. // Connection should be marked as dead after it failed
  66. if numFailedReqs != 5 {
  67. t.Errorf("expected %d failed requests; got: %d", 5, numFailedReqs)
  68. }
  69. if retrier.N != 5 {
  70. t.Errorf("expected %d Retrier calls; got: %d", 5, retrier.N)
  71. }
  72. }
  73. func TestRetrierWithError(t *testing.T) {
  74. var numFailedReqs int
  75. fail := func(r *http.Request) (*http.Response, error) {
  76. numFailedReqs += 1
  77. //return &http.Response{Request: r, StatusCode: 400}, nil
  78. return nil, errors.New("request failed")
  79. }
  80. tr := &failingTransport{path: "/fail", fail: fail}
  81. httpClient := &http.Client{Transport: tr}
  82. kaboom := errors.New("kaboom")
  83. retrier := &testRetrier{
  84. Err: kaboom,
  85. Retrier: NewBackoffRetrier(NewSimpleBackoff(100, 100, 100, 100, 100)),
  86. }
  87. client, err := NewClient(
  88. SetHttpClient(httpClient),
  89. SetMaxRetries(5),
  90. SetHealthcheck(false),
  91. SetRetrier(retrier))
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. res, err := client.PerformRequest(context.TODO(), "GET", "/fail", nil, nil)
  96. if err != kaboom {
  97. t.Fatalf("expected %v, got %v", kaboom, err)
  98. }
  99. if res != nil {
  100. t.Fatal("expected no response")
  101. }
  102. if numFailedReqs != 1 {
  103. t.Errorf("expected %d failed requests; got: %d", 1, numFailedReqs)
  104. }
  105. if retrier.N != 1 {
  106. t.Errorf("expected %d Retrier calls; got: %d", 1, retrier.N)
  107. }
  108. }
  109. func TestRetrierOnPerformRequest(t *testing.T) {
  110. var numFailedReqs int
  111. fail := func(r *http.Request) (*http.Response, error) {
  112. numFailedReqs += 1
  113. //return &http.Response{Request: r, StatusCode: 400}, nil
  114. return nil, errors.New("request failed")
  115. }
  116. tr := &failingTransport{path: "/fail", fail: fail}
  117. httpClient := &http.Client{Transport: tr}
  118. defaultRetrier := &testRetrier{
  119. Retrier: NewStopRetrier(),
  120. }
  121. requestRetrier := &testRetrier{
  122. Retrier: NewStopRetrier(),
  123. }
  124. client, err := NewClient(
  125. SetHttpClient(httpClient),
  126. SetHealthcheck(false),
  127. SetRetrier(defaultRetrier))
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. res, err := client.PerformRequestWithOptions(context.TODO(), PerformRequestOptions{
  132. Method: "GET",
  133. Path: "/fail",
  134. Retrier: requestRetrier,
  135. })
  136. if err == nil {
  137. t.Fatal("expected error")
  138. }
  139. if res != nil {
  140. t.Fatal("expected no response")
  141. }
  142. if want, have := int64(0), defaultRetrier.N; want != have {
  143. t.Errorf("defaultRetrier: expected %d calls; got: %d", want, have)
  144. }
  145. if want, have := int64(1), requestRetrier.N; want != have {
  146. t.Errorf("requestRetrier: expected %d calls; got: %d", want, have)
  147. }
  148. }