| 7 | ) |
| 8 | |
| 9 | func TestChecker(t *testing.T) { |
| 10 | // Listen on localhost, random port |
| 11 | srv, err := net.Listen("tcp", "localhost:8382") |
| 12 | if err != nil { |
| 13 | t.Errorf("Couldn't start TCP test server with error: %v", err) |
| 14 | } |
| 15 | defer srv.Close() |
| 16 | |
| 17 | // Accept a future connection |
| 18 | go func() { |
| 19 | for { |
| 20 | conn, err := srv.Accept() |
| 21 | if err != nil { |
| 22 | break |
| 23 | } |
| 24 | conn.Close() |
| 25 | } |
| 26 | }() |
| 27 | |
| 28 | // Should know the host:port by now |
| 29 | endpt := srv.Addr().String() |
| 30 | testName := "TestDNS" |
| 31 | hc := Checker{Name: testName, URL: endpt, Attempts: 2} |
| 32 | |
| 33 | // Try an up server |
| 34 | result, err := hc.Check() |
| 35 | if err != nil { |
| 36 | t.Errorf("Didn't expect an error: %v", err) |
| 37 | } |
| 38 | if got, want := result.Title, testName; got != want { |
| 39 | t.Errorf("Expected result.Title='%s', got '%s'", want, got) |
| 40 | } |
| 41 | if got, want := result.Endpoint, endpt; got != want { |
| 42 | t.Errorf("Expected result.Endpoint='%s', got '%s'", want, got) |
| 43 | } |
| 44 | if got, want := result.Down, false; got != want { |
| 45 | t.Errorf("Expected result.Down=%v, got %v", want, got) |
| 46 | } |
| 47 | if got, want := result.Degraded, false; got != want { |
| 48 | t.Errorf("Expected result.Degraded=%v, got %v", want, got) |
| 49 | } |
| 50 | if got, want := result.Healthy, true; got != want { |
| 51 | t.Errorf("Expected result.Healthy=%v, got %v", want, got) |
| 52 | } |
| 53 | if got, want := len(result.Times), hc.Attempts; got != want { |
| 54 | t.Errorf("Expected %d attempts, got %d", want, got) |
| 55 | } |
| 56 | ts := time.Unix(0, result.Timestamp) |
| 57 | if time.Since(ts) > 5*time.Second { |
| 58 | t.Errorf("Expected timestamp to be recent, got %s", ts) |
| 59 | } |
| 60 | |
| 61 | // Try various different down criteria |
| 62 | result, err = hc.Check() |
| 63 | if err != nil { |
| 64 | t.Errorf("Didn't expect an error: %v", err) |
| 65 | } |
| 66 | if got, want := result.Healthy, true; got != want { |