client_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. donec := make(chan error)
  61. go func() {
  62. // without timeout, dial continues forever on ipv4 blackhole
  63. cfg := Config{
  64. Endpoints: []string{"http://254.0.0.1:12345"},
  65. DialTimeout: 2 * time.Second}
  66. c, err := New(cfg)
  67. if c != nil || err == nil {
  68. t.Errorf("new client should fail")
  69. }
  70. donec <- err
  71. }()
  72. time.Sleep(10 * time.Millisecond)
  73. select {
  74. case err := <-donec:
  75. t.Errorf("dial didn't wait (%v)", err)
  76. default:
  77. }
  78. select {
  79. case <-time.After(5 * time.Second):
  80. t.Errorf("failed to timeout dial on time")
  81. case err := <-donec:
  82. if err != grpc.ErrClientConnTimeout {
  83. t.Errorf("unexpected error %v, want %v", err, grpc.ErrClientConnTimeout)
  84. }
  85. }
  86. }
  87. func TestDialNoTimeout(t *testing.T) {
  88. cfg := Config{Endpoints: []string{"127.0.0.1:12345"}}
  89. c, err := New(cfg)
  90. if c == nil || err != nil {
  91. t.Fatalf("new client with DialNoWait should succeed, got %v", err)
  92. }
  93. c.Close()
  94. }
  95. func TestIsHaltErr(t *testing.T) {
  96. if !isHaltErr(nil, fmt.Errorf("etcdserver: some etcdserver error")) {
  97. t.Errorf(`error prefixed with "etcdserver: " should be Halted by default`)
  98. }
  99. if isHaltErr(nil, rpctypes.ErrGRPCStopped) {
  100. t.Errorf("error %v should not halt", rpctypes.ErrGRPCStopped)
  101. }
  102. if isHaltErr(nil, rpctypes.ErrGRPCNoLeader) {
  103. t.Errorf("error %v should not halt", rpctypes.ErrGRPCNoLeader)
  104. }
  105. ctx, cancel := context.WithCancel(context.TODO())
  106. if isHaltErr(ctx, nil) {
  107. t.Errorf("no error and active context should not be Halted")
  108. }
  109. cancel()
  110. if !isHaltErr(ctx, nil) {
  111. t.Errorf("cancel on context should be Halted")
  112. }
  113. }