client_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "net/http/httptest"
  20. "os"
  21. "strings"
  22. "sync/atomic"
  23. "testing"
  24. "go.etcd.io/etcd/client"
  25. "go.etcd.io/etcd/integration"
  26. "go.etcd.io/etcd/pkg/testutil"
  27. )
  28. // TestV2NoRetryEOF tests destructive api calls won't retry on a disconnection.
  29. func TestV2NoRetryEOF(t *testing.T) {
  30. defer testutil.AfterTest(t)
  31. // generate an EOF response; specify address so appears first in sorted ep list
  32. lEOF := integration.NewListenerWithAddr(t, fmt.Sprintf("127.0.0.1:%05d", os.Getpid()))
  33. defer lEOF.Close()
  34. tries := uint32(0)
  35. go func() {
  36. for {
  37. conn, err := lEOF.Accept()
  38. if err != nil {
  39. return
  40. }
  41. atomic.AddUint32(&tries, 1)
  42. conn.Close()
  43. }
  44. }()
  45. eofURL := integration.URLScheme + "://" + lEOF.Addr().String()
  46. cli := integration.MustNewHTTPClient(t, []string{eofURL, eofURL}, nil)
  47. kapi := client.NewKeysAPI(cli)
  48. for i, f := range noRetryList(kapi) {
  49. startTries := atomic.LoadUint32(&tries)
  50. if err := f(); err == nil {
  51. t.Errorf("#%d: expected EOF error, got nil", i)
  52. }
  53. endTries := atomic.LoadUint32(&tries)
  54. if startTries+1 != endTries {
  55. t.Errorf("#%d: expected 1 try, got %d", i, endTries-startTries)
  56. }
  57. }
  58. }
  59. // TestV2NoRetryNoLeader tests destructive api calls won't retry if given an error code.
  60. func TestV2NoRetryNoLeader(t *testing.T) {
  61. defer testutil.AfterTest(t)
  62. lHTTP := integration.NewListenerWithAddr(t, fmt.Sprintf("127.0.0.1:%05d", os.Getpid()))
  63. eh := &errHandler{errCode: http.StatusServiceUnavailable}
  64. srv := httptest.NewUnstartedServer(eh)
  65. defer lHTTP.Close()
  66. defer srv.Close()
  67. srv.Listener = lHTTP
  68. go srv.Start()
  69. lHTTPURL := integration.URLScheme + "://" + lHTTP.Addr().String()
  70. cli := integration.MustNewHTTPClient(t, []string{lHTTPURL, lHTTPURL}, nil)
  71. kapi := client.NewKeysAPI(cli)
  72. // test error code
  73. for i, f := range noRetryList(kapi) {
  74. reqs := eh.reqs
  75. if err := f(); err == nil || !strings.Contains(err.Error(), "no leader") {
  76. t.Errorf("#%d: expected \"no leader\", got %v", i, err)
  77. }
  78. if eh.reqs != reqs+1 {
  79. t.Errorf("#%d: expected 1 request, got %d", i, eh.reqs-reqs)
  80. }
  81. }
  82. }
  83. // TestV2RetryRefuse tests destructive api calls will retry if a connection is refused.
  84. func TestV2RetryRefuse(t *testing.T) {
  85. defer testutil.AfterTest(t)
  86. cl := integration.NewCluster(t, 1)
  87. cl.Launch(t)
  88. defer cl.Terminate(t)
  89. // test connection refused; expect no error failover
  90. cli := integration.MustNewHTTPClient(t, []string{integration.URLScheme + "://refuseconn:123", cl.URL(0)}, nil)
  91. kapi := client.NewKeysAPI(cli)
  92. if _, err := kapi.Set(context.Background(), "/delkey", "def", nil); err != nil {
  93. t.Fatal(err)
  94. }
  95. for i, f := range noRetryList(kapi) {
  96. if err := f(); err != nil {
  97. t.Errorf("#%d: unexpected retry failure (%v)", i, err)
  98. }
  99. }
  100. }
  101. type errHandler struct {
  102. errCode int
  103. reqs int
  104. }
  105. func (eh *errHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  106. req.Body.Close()
  107. eh.reqs++
  108. w.WriteHeader(eh.errCode)
  109. }
  110. func noRetryList(kapi client.KeysAPI) []func() error {
  111. return []func() error{
  112. func() error {
  113. opts := &client.SetOptions{PrevExist: client.PrevNoExist}
  114. _, err := kapi.Set(context.Background(), "/setkey", "bar", opts)
  115. return err
  116. },
  117. func() error {
  118. _, err := kapi.Delete(context.Background(), "/delkey", nil)
  119. return err
  120. },
  121. }
  122. }