client_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  22. "github.com/coreos/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. testCfgs := []Config{
  72. {
  73. Endpoints: []string{"http://254.0.0.1:12345"},
  74. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  75. DialTimeout: 2 * time.Second,
  76. },
  77. {
  78. Endpoints: []string{"http://254.0.0.1:12345"},
  79. DialTimeout: time.Second,
  80. DialOptions: []grpc.DialOption{grpc.WithBlock()},
  81. Username: "abc",
  82. Password: "def",
  83. },
  84. }
  85. for i, cfg := range testCfgs {
  86. donec := make(chan error)
  87. go func() {
  88. // without timeout, dial continues forever on ipv4 black hole
  89. c, err := New(cfg)
  90. if c != nil || err == nil {
  91. t.Errorf("#%d: new client should fail", i)
  92. }
  93. donec <- err
  94. }()
  95. time.Sleep(10 * time.Millisecond)
  96. select {
  97. case err := <-donec:
  98. t.Errorf("#%d: dial didn't wait (%v)", i, err)
  99. default:
  100. }
  101. select {
  102. case <-time.After(5 * time.Second):
  103. t.Errorf("#%d: failed to timeout dial on time", i)
  104. case err := <-donec:
  105. if err != context.DeadlineExceeded {
  106. t.Errorf("#%d: unexpected error %v, want %v", i, err, context.DeadlineExceeded)
  107. }
  108. }
  109. }
  110. }
  111. func TestDialNoTimeout(t *testing.T) {
  112. cfg := Config{Endpoints: []string{"127.0.0.1:12345"}}
  113. c, err := New(cfg)
  114. if c == nil || err != nil {
  115. t.Fatalf("new client with DialNoWait should succeed, got %v", err)
  116. }
  117. c.Close()
  118. }
  119. func TestIsHaltErr(t *testing.T) {
  120. if !isHaltErr(nil, fmt.Errorf("etcdserver: some etcdserver error")) {
  121. t.Errorf(`error prefixed with "etcdserver: " should be Halted by default`)
  122. }
  123. if isHaltErr(nil, rpctypes.ErrGRPCStopped) {
  124. t.Errorf("error %v should not halt", rpctypes.ErrGRPCStopped)
  125. }
  126. if isHaltErr(nil, rpctypes.ErrGRPCNoLeader) {
  127. t.Errorf("error %v should not halt", rpctypes.ErrGRPCNoLeader)
  128. }
  129. ctx, cancel := context.WithCancel(context.TODO())
  130. if isHaltErr(ctx, nil) {
  131. t.Errorf("no error and active context should not be Halted")
  132. }
  133. cancel()
  134. if !isHaltErr(ctx, nil) {
  135. t.Errorf("cancel on context should be Halted")
  136. }
  137. }