| 103 | } |
| 104 | |
| 105 | func TestCheckerWithAgressiveTimeout(t *testing.T) { |
| 106 | // Listen on localhost, random port |
| 107 | srv, err := net.Listen("tcp", "localhost:0") |
| 108 | if err != nil { |
| 109 | t.Errorf("Couldn't start TCP test server with error: %v", err) |
| 110 | } |
| 111 | defer srv.Close() |
| 112 | |
| 113 | // Accept a future connection |
| 114 | go func() { |
| 115 | for { |
| 116 | conn, err := srv.Accept() |
| 117 | if err != nil { |
| 118 | break |
| 119 | } |
| 120 | conn.Close() |
| 121 | } |
| 122 | }() |
| 123 | |
| 124 | // Should know the host:port by now |
| 125 | endpt := srv.Addr().String() |
| 126 | testName := "TestTCP" |
| 127 | hc := Checker{Name: testName, URL: endpt, Attempts: 2, Timeout: 1 * time.Nanosecond} |
| 128 | |
| 129 | result, err := hc.Check() |
| 130 | if err != nil { |
| 131 | t.Errorf("Didn't expect an error: %v", err) |
| 132 | } |
| 133 | if got, want := len(result.Times), hc.Attempts; got != want { |
| 134 | t.Errorf("Expected %d attempts, got %d", want, got) |
| 135 | } |
| 136 | if got, want := result.Down, true; got != want { |
| 137 | t.Errorf("Expected result.Down=%v, got %v", want, got) |
| 138 | } |
| 139 | if got, want := result.Healthy, false; got != want { |
| 140 | t.Errorf("Expected result.Healthy=%v, got %v", want, got) |
| 141 | } |
| 142 | } |