| 94 | } |
| 95 | |
| 96 | func TestDialTimeout(t *testing.T) { |
| 97 | testutil.RegisterLeakDetection(t) |
| 98 | |
| 99 | wantError := context.DeadlineExceeded |
| 100 | |
| 101 | // grpc.WithBlock to block until connection up or timeout |
| 102 | testCfgs := []Config{ |
| 103 | { |
| 104 | Endpoints: []string{"http://254.0.0.1:12345"}, |
| 105 | DialTimeout: 2 * time.Second, |
| 106 | DialOptions: []grpc.DialOption{grpc.WithBlock()}, |
| 107 | }, |
| 108 | { |
| 109 | Endpoints: []string{"http://254.0.0.1:12345"}, |
| 110 | DialTimeout: time.Second, |
| 111 | DialOptions: []grpc.DialOption{grpc.WithBlock()}, |
| 112 | Username: "abc", |
| 113 | Password: "def", |
| 114 | }, |
| 115 | } |
| 116 | |
| 117 | for i, cfg := range testCfgs { |
| 118 | donec := make(chan error, 1) |
| 119 | go func(cfg Config, i int) { |
| 120 | // without timeout, dial continues forever on ipv4 black hole |
| 121 | c, err := NewClient(t, cfg) |
| 122 | if c != nil || err == nil { |
| 123 | t.Errorf("#%d: new client should fail", i) |
| 124 | } |
| 125 | donec <- err |
| 126 | }(cfg, i) |
| 127 | |
| 128 | time.Sleep(10 * time.Millisecond) |
| 129 | |
| 130 | select { |
| 131 | case err := <-donec: |
| 132 | t.Errorf("#%d: dial didn't wait (%v)", i, err) |
| 133 | default: |
| 134 | } |
| 135 | |
| 136 | select { |
| 137 | case <-time.After(5 * time.Second): |
| 138 | t.Errorf("#%d: failed to timeout dial on time", i) |
| 139 | case err := <-donec: |
| 140 | if err.Error() != wantError.Error() { |
| 141 | t.Errorf("#%d: unexpected error '%v', want '%v'", i, err, wantError) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestDialNoTimeout(t *testing.T) { |
| 148 | cfg := Config{Endpoints: []string{"127.0.0.1:12345"}} |