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