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