client_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. "testing"
  17. "time"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  19. )
  20. func TestDialTimeout(t *testing.T) {
  21. donec := make(chan error)
  22. go func() {
  23. // without timeout, grpc keeps redialing if connection refused
  24. cfg := Config{
  25. Endpoints: []string{"localhost:12345"},
  26. DialTimeout: 2 * time.Second}
  27. c, err := New(cfg)
  28. if c != nil || err == nil {
  29. t.Errorf("new client should fail")
  30. }
  31. donec <- err
  32. }()
  33. time.Sleep(10 * time.Millisecond)
  34. select {
  35. case err := <-donec:
  36. t.Errorf("dial didn't wait (%v)", err)
  37. default:
  38. }
  39. select {
  40. case <-time.After(5 * time.Second):
  41. t.Errorf("failed to timeout dial on time")
  42. case err := <-donec:
  43. if err != grpc.ErrClientConnTimeout {
  44. t.Errorf("unexpected error %v, want %v", err, grpc.ErrClientConnTimeout)
  45. }
  46. }
  47. }