client_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 clientv3
  15. import (
  16. "context"
  17. "fmt"
  18. "net"
  19. "testing"
  20. "time"
  21. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  22. "go.etcd.io/etcd/pkg/testutil"
  23. "google.golang.org/grpc"
  24. )
  25. func TestDialCancel(t *testing.T) {
  26. defer testutil.AfterTest(t)
  27. // accept first connection so client is created with dial timeout
  28. ln, err := net.Listen("unix", "dialcancel:12345")
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. defer ln.Close()
  33. ep := "unix://dialcancel:12345"
  34. cfg := Config{
  35. Endpoints: []string{ep},
  36. DialTimeout: 30 * time.Second}
  37. c, err := New(cfg)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. // connect to ipv4 black hole so dial blocks
  42. c.SetEndpoints("http://254.0.0.1:12345")
  43. // issue Get to force redial attempts
  44. getc := make(chan struct{})
  45. go func() {
  46. defer close(getc)
  47. // Get may hang forever on grpc's Stream.Header() if its
  48. // context is never canceled.
  49. c.Get(c.Ctx(), "abc")
  50. }()
  51. // wait a little bit so client close is after dial starts
  52. time.Sleep(100 * time.Millisecond)
  53. donec := make(chan struct{})
  54. go func() {
  55. defer close(donec)
  56. c.Close()
  57. }()
  58. select {
  59. case <-time.After(5 * time.Second):
  60. t.Fatalf("failed to close")
  61. case <-donec:
  62. }
  63. select {
  64. case <-time.After(5 * time.Second):
  65. t.Fatalf("get failed to exit")
  66. case <-getc:
  67. }
  68. }
  69. func TestDialTimeout(t *testing.T) {
  70. defer testutil.AfterTest(t)
  71. // grpc.WithBlock to block until connection up or timeout
  72. testCfgs := []Config{
  73. {
  74. Endpoints: []string{"http://254.0.0.1:12345"},
  75. DialTimeout: 2 * time.Second,
  76. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  77. },
  78. {
  79. Endpoints: []string{"http://254.0.0.1:12345"},
  80. DialTimeout: time.Second,
  81. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  82. Username: "abc",
  83. Password: "def",
  84. },
  85. }
  86. for i, cfg := range testCfgs {
  87. donec := make(chan error)
  88. go func() {
  89. // without timeout, dial continues forever on ipv4 black hole
  90. c, err := New(cfg)
  91. if c != nil || err == nil {
  92. t.Errorf("#%d: new client should fail", i)
  93. }
  94. donec <- err
  95. }()
  96. time.Sleep(10 * time.Millisecond)
  97. select {
  98. case err := <-donec:
  99. t.Errorf("#%d: dial didn't wait (%v)", i, err)
  100. default:
  101. }
  102. select {
  103. case <-time.After(5 * time.Second):
  104. t.Errorf("#%d: failed to timeout dial on time", i)
  105. case err := <-donec:
  106. if err != context.DeadlineExceeded {
  107. t.Errorf("#%d: unexpected error %v, want %v", i, err, context.DeadlineExceeded)
  108. }
  109. }
  110. }
  111. }
  112. func TestDialNoTimeout(t *testing.T) {
  113. cfg := Config{Endpoints: []string{"127.0.0.1:12345"}}
  114. c, err := New(cfg)
  115. if c == nil || err != nil {
  116. t.Fatalf("new client with DialNoWait should succeed, got %v", err)
  117. }
  118. c.Close()
  119. }
  120. func TestIsHaltErr(t *testing.T) {
  121. if !isHaltErr(nil, fmt.Errorf("etcdserver: some etcdserver error")) {
  122. t.Errorf(`error prefixed with "etcdserver: " should be Halted by default`)
  123. }
  124. if isHaltErr(nil, rpctypes.ErrGRPCStopped) {
  125. t.Errorf("error %v should not halt", rpctypes.ErrGRPCStopped)
  126. }
  127. if isHaltErr(nil, rpctypes.ErrGRPCNoLeader) {
  128. t.Errorf("error %v should not halt", rpctypes.ErrGRPCNoLeader)
  129. }
  130. ctx, cancel := context.WithCancel(context.TODO())
  131. if isHaltErr(ctx, nil) {
  132. t.Errorf("no error and active context should not be Halted")
  133. }
  134. cancel()
  135. if !isHaltErr(ctx, nil) {
  136. t.Errorf("cancel on context should be Halted")
  137. }
  138. }
  139. func TestCloseCtxClient(t *testing.T) {
  140. ctx := context.Background()
  141. c := NewCtxClient(ctx)
  142. err := c.Close()
  143. // Close returns ctx.toErr, a nil error means an open Done channel
  144. if err == nil {
  145. t.Errorf("failed to Close the client. %v", err)
  146. }
  147. }