2016-09-07 02:01:27 -07:00
|
|
|
package health
|
|
|
|
|
|
2017-11-13 09:52:40 +00:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
2021-10-13 15:30:31 +08:00
|
|
|
"io"
|
2017-11-13 09:52:40 +00:00
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
2018-01-18 10:40:09 +00:00
|
|
|
"time"
|
2017-11-13 09:52:40 +00:00
|
|
|
)
|
|
|
|
|
|
2016-09-07 02:01:27 -07:00
|
|
|
func TestHealth(t *testing.T) {
|
2022-04-13 19:09:03 +02:00
|
|
|
h := &health{Addr: ":0"}
|
2017-08-27 21:33:38 +01:00
|
|
|
|
2018-01-10 11:41:22 +00:00
|
|
|
if err := h.OnStartup(); err != nil {
|
2016-09-07 02:01:27 -07:00
|
|
|
t.Fatalf("Unable to startup the health server: %v", err)
|
|
|
|
|
}
|
2018-04-21 17:43:02 +01:00
|
|
|
defer h.OnFinalShutdown()
|
2016-09-07 02:01:27 -07:00
|
|
|
|
2019-05-04 21:06:04 +01:00
|
|
|
address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), "/health")
|
2016-09-07 02:01:27 -07:00
|
|
|
|
|
|
|
|
response, err := http.Get(address)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Unable to query %s: %v", address, err)
|
|
|
|
|
}
|
2023-02-09 19:29:49 +08:00
|
|
|
if response.StatusCode != http.StatusOK {
|
2016-09-07 02:01:27 -07:00
|
|
|
t.Errorf("Invalid status code: expecting '200', got '%d'", response.StatusCode)
|
|
|
|
|
}
|
2021-10-13 15:30:31 +08:00
|
|
|
content, err := io.ReadAll(response.Body)
|
2016-09-07 02:01:27 -07:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Unable to get response body from %s: %v", address, err)
|
|
|
|
|
}
|
2017-08-27 21:33:38 +01:00
|
|
|
response.Body.Close()
|
|
|
|
|
|
2019-08-26 18:31:24 +08:00
|
|
|
if string(content) != http.StatusText(http.StatusOK) {
|
2016-09-07 02:01:27 -07:00
|
|
|
t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-18 10:40:09 +00:00
|
|
|
|
|
|
|
|
func TestHealthLameduck(t *testing.T) {
|
2022-04-13 19:09:03 +02:00
|
|
|
h := &health{Addr: ":0", lameduck: 250 * time.Millisecond}
|
2018-01-18 10:40:09 +00:00
|
|
|
|
|
|
|
|
if err := h.OnStartup(); err != nil {
|
|
|
|
|
t.Fatalf("Unable to startup the health server: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-21 17:43:02 +01:00
|
|
|
h.OnFinalShutdown()
|
2018-01-18 10:40:09 +00:00
|
|
|
}
|