client_test.go 1.8 KB

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