client_test.go 3.6 KB

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