client_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "testing"
  18. "time"
  19. "github.com/coreos/etcd/etcdserver"
  20. "golang.org/x/net/context"
  21. "google.golang.org/grpc"
  22. )
  23. func TestDialTimeout(t *testing.T) {
  24. donec := make(chan error)
  25. go func() {
  26. // without timeout, grpc keeps redialing if connection refused
  27. cfg := Config{
  28. Endpoints: []string{"localhost:12345"},
  29. DialTimeout: 2 * time.Second}
  30. c, err := New(cfg)
  31. if c != nil || err == nil {
  32. t.Errorf("new client should fail")
  33. }
  34. donec <- err
  35. }()
  36. time.Sleep(10 * time.Millisecond)
  37. select {
  38. case err := <-donec:
  39. t.Errorf("dial didn't wait (%v)", err)
  40. default:
  41. }
  42. select {
  43. case <-time.After(5 * time.Second):
  44. t.Errorf("failed to timeout dial on time")
  45. case err := <-donec:
  46. if err != grpc.ErrClientConnTimeout {
  47. t.Errorf("unexpected error %v, want %v", err, grpc.ErrClientConnTimeout)
  48. }
  49. }
  50. }
  51. func TestIsHaltErr(t *testing.T) {
  52. if !isHaltErr(nil, fmt.Errorf("etcdserver: some etcdserver error")) {
  53. t.Errorf(`error prefixed with "etcdserver: " should be Halted by default`)
  54. }
  55. if isHaltErr(nil, etcdserver.ErrStopped) {
  56. t.Errorf("error %v should not halt", etcdserver.ErrStopped)
  57. }
  58. if isHaltErr(nil, etcdserver.ErrNoLeader) {
  59. t.Errorf("error %v should not halt", etcdserver.ErrNoLeader)
  60. }
  61. ctx, cancel := context.WithCancel(context.TODO())
  62. if isHaltErr(ctx, nil) {
  63. t.Errorf("no error and active context should not be Halted")
  64. }
  65. cancel()
  66. if !isHaltErr(ctx, nil) {
  67. t.Errorf("cancel on context should be Halted")
  68. }
  69. }