client_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "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 blackhole so dial blocks
  42. c.SetEndpoints("http://254.0.0.1:12345")
  43. // issue Get to force redial attempts
  44. go c.Get(context.TODO(), "abc")
  45. // wait a little bit so client close is after dial starts
  46. time.Sleep(100 * time.Millisecond)
  47. donec := make(chan struct{})
  48. go func() {
  49. defer close(donec)
  50. c.Close()
  51. }()
  52. select {
  53. case <-time.After(5 * time.Second):
  54. t.Fatalf("failed to close")
  55. case <-donec:
  56. }
  57. }
  58. func TestDialTimeout(t *testing.T) {
  59. defer testutil.AfterTest(t)
  60. testCfgs := []Config{
  61. {
  62. Endpoints: []string{"http://254.0.0.1:12345"},
  63. DialTimeout: 2 * time.Second,
  64. },
  65. {
  66. Endpoints: []string{"http://254.0.0.1:12345"},
  67. DialTimeout: time.Second,
  68. Username: "abc",
  69. Password: "def",
  70. },
  71. }
  72. for i, cfg := range testCfgs {
  73. donec := make(chan error)
  74. go func() {
  75. // without timeout, dial continues forever on ipv4 blackhole
  76. c, err := New(cfg)
  77. if c != nil || err == nil {
  78. t.Errorf("#%d: new client should fail", i)
  79. }
  80. donec <- err
  81. }()
  82. time.Sleep(10 * time.Millisecond)
  83. select {
  84. case err := <-donec:
  85. t.Errorf("#%d: dial didn't wait (%v)", i, err)
  86. default:
  87. }
  88. select {
  89. case <-time.After(5 * time.Second):
  90. t.Errorf("#%d: failed to timeout dial on time", i)
  91. case err := <-donec:
  92. if err != grpc.ErrClientConnTimeout {
  93. t.Errorf("#%d: unexpected error %v, want %v", i, err, grpc.ErrClientConnTimeout)
  94. }
  95. }
  96. }
  97. }
  98. func TestDialNoTimeout(t *testing.T) {
  99. cfg := Config{Endpoints: []string{"127.0.0.1:12345"}}
  100. c, err := New(cfg)
  101. if c == nil || err != nil {
  102. t.Fatalf("new client with DialNoWait should succeed, got %v", err)
  103. }
  104. c.Close()
  105. }
  106. func TestIsHaltErr(t *testing.T) {
  107. if !isHaltErr(nil, fmt.Errorf("etcdserver: some etcdserver error")) {
  108. t.Errorf(`error prefixed with "etcdserver: " should be Halted by default`)
  109. }
  110. if isHaltErr(nil, rpctypes.ErrGRPCStopped) {
  111. t.Errorf("error %v should not halt", rpctypes.ErrGRPCStopped)
  112. }
  113. if isHaltErr(nil, rpctypes.ErrGRPCNoLeader) {
  114. t.Errorf("error %v should not halt", rpctypes.ErrGRPCNoLeader)
  115. }
  116. ctx, cancel := context.WithCancel(context.TODO())
  117. if isHaltErr(ctx, nil) {
  118. t.Errorf("no error and active context should not be Halted")
  119. }
  120. cancel()
  121. if !isHaltErr(ctx, nil) {
  122. t.Errorf("cancel on context should be Halted")
  123. }
  124. }