Better HEALTHZ tests

This commit is contained in:
tidwall 2022-09-23 17:34:09 -07:00
parent 5bcef43894
commit 5c455cbe10
6 changed files with 78 additions and 23 deletions

View File

@ -1084,7 +1084,7 @@ func (s *Server) command(msg *Message, client *Client) (
case "server": case "server":
res, err = s.cmdServer(msg) res, err = s.cmdServer(msg)
case "healthz": case "healthz":
res, err = s.cmdHealthz(msg) res, err = s.cmdHEALTHZ(msg)
case "info": case "info":
res, err = s.cmdInfo(msg) res, err = s.cmdInfo(msg)
case "scan": case "scan":

View File

@ -95,22 +95,34 @@ func (s *Server) cmdSTATS(msg *Message) (resp.Value, error) {
return resp.ArrayValue(vals), nil return resp.ArrayValue(vals), nil
} }
func (s *Server) cmdHealthz(msg *Message) (res resp.Value, err error) { // HEALTHZ
func (s *Server) cmdHEALTHZ(msg *Message) (resp.Value, error) {
start := time.Now() start := time.Now()
// >> Args
args := msg.Args
if len(args) != 1 {
return retrerr(errInvalidNumberOfArguments)
}
// >> Operation
if s.config.followHost() != "" { if s.config.followHost() != "" {
m := make(map[string]interface{}) m := make(map[string]interface{})
s.basicStats(m) s.basicStats(m)
if fmt.Sprintf("%v", m["caught_up"]) != "true" { if fmt.Sprintf("%v", m["caught_up"]) != "true" {
return NOMessage, errors.New("not caught up") return retrerr(errors.New("not caught up"))
} }
} }
switch msg.OutputType {
case JSON: // >> Response
res = resp.StringValue(`{"ok":true,"elapsed":"` + time.Since(start).String() + "\"}")
case RESP: if msg.OutputType == JSON {
res = resp.SimpleStringValue("OK") return resp.StringValue(`{"ok":true,"elapsed":"` +
time.Since(start).String() + "\"}"), nil
} }
return res, nil return resp.SimpleStringValue("OK"), nil
} }
func (s *Server) cmdServer(msg *Message) (res resp.Value, err error) { func (s *Server) cmdServer(msg *Message) (res resp.Value, err error) {

View File

@ -122,9 +122,7 @@ func keys_KNN_random_test(mc *mockServer) error {
mc.Do("OUTPUT", "json") mc.Do("OUTPUT", "json")
defer mc.Do("OUTPUT", "resp") defer mc.Do("OUTPUT", "resp")
start := time.Now()
res, err := redis.String(mc.Do("NEARBY", "points", "LIMIT", N, "POINT", target[1], target[0])) res, err := redis.String(mc.Do("NEARBY", "points", "LIMIT", N, "POINT", target[1], target[0]))
println(time.Since(start).String())
if err != nil { if err != nil {
return err return err
} }

View File

@ -32,6 +32,8 @@ func subTestKeys(t *testing.T, mc *mockServer) {
runStep(t, mc, "WHEREEVAL", keys_WHEREEVAL_test) runStep(t, mc, "WHEREEVAL", keys_WHEREEVAL_test)
runStep(t, mc, "TYPE", keys_TYPE_test) runStep(t, mc, "TYPE", keys_TYPE_test)
runStep(t, mc, "FLUSHDB", keys_FLUSHDB_test) runStep(t, mc, "FLUSHDB", keys_FLUSHDB_test)
runStep(t, mc, "HEALTHZ", keys_HEALTHZ_test)
} }
func keys_BOUNDS_test(mc *mockServer) error { func keys_BOUNDS_test(mc *mockServer) error {
@ -540,3 +542,34 @@ func keys_FLUSHDB_test(mc *mockServer) error {
Do("FLUSHDB").JSON().OK(), Do("FLUSHDB").JSON().OK(),
) )
} }
func keys_HEALTHZ_test(mc *mockServer) error {
// // follow and wait
// str, err := redis.String(mc.Do("FOLLOW", "localhost", mc.alt.port))
// if err != nil {
// return err
// }
// if str != "OK" {
// return errors.New("not ok")
// }
// start := time.Now()
// for time.Since(start) < time.Second*5 {
// str, err = redis.String(mc.Do("HEALTHZ"))
// if str == "OK" {
// err = nil
// break
// }
// time.Sleep(time.Second / 4)
// }
// if err != nil {
// return err
// }
return mc.DoBatch(
Do("HEALTHZ").OK(),
Do("HEALTHZ").JSON().OK(),
// Do("FOLLOW", "no", "one").OK(),
Do("HEALTHZ", "arg").Err(`wrong number of arguments for 'healthz' command`),
)
}

View File

@ -38,9 +38,10 @@ type mockServer struct {
port int port int
conn redis.Conn conn redis.Conn
ioJSON bool ioJSON bool
// alt *mockServer
} }
func mockOpenServer(silent bool) (*mockServer, error) { func mockOpenServer(silent, metrics bool) (*mockServer, error) {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
port := rand.Int()%20000 + 20000 port := rand.Int()%20000 + 20000
dir := fmt.Sprintf("data-mock-%d", port) dir := fmt.Sprintf("data-mock-%d", port)
@ -56,11 +57,13 @@ func mockOpenServer(silent bool) (*mockServer, error) {
tlog.SetOutput(logOutput) tlog.SetOutput(logOutput)
go func() { go func() {
opts := server.Options{ opts := server.Options{
Host: "localhost", Host: "localhost",
Port: port, Port: port,
Dir: dir, Dir: dir,
UseHTTP: true, UseHTTP: true,
MetricsAddr: ":4321", }
if metrics {
opts.MetricsAddr = ":4321"
} }
if err := server.Serve(opts); err != nil { if err := server.Serve(opts); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -38,11 +38,20 @@ func TestAll(t *testing.T) {
os.Exit(1) os.Exit(1)
}() }()
mc, err := mockOpenServer(false) mc, err := mockOpenServer(false, true)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer mc.Close() defer mc.Close()
// mc2, err := mockOpenServer(false, false)
// if err != nil {
// t.Fatal(err)
// }
// defer mc2.Close()
// mc.alt = mc2
// mc2.alt = mc
runSubTest(t, "keys", mc, subTestKeys) runSubTest(t, "keys", mc, subTestKeys)
runSubTest(t, "json", mc, subTestJSON) runSubTest(t, "json", mc, subTestJSON)
runSubTest(t, "search", mc, subTestSearch) runSubTest(t, "search", mc, subTestSearch)
@ -71,10 +80,10 @@ func runStep(t *testing.T, mc *mockServer, name string, step func(mc *mockServer
mc.ResetConn() mc.ResetConn()
defer mc.ResetConn() defer mc.ResetConn()
// clear the database so the test is consistent // clear the database so the test is consistent
if err := mc.DoBatch([][]interface{}{ if err := mc.DoBatch(
{"OUTPUT", "resp"}, {"OK"}, Do("OUTPUT", "resp").OK(),
{"FLUSHDB"}, {"OK"}, Do("FLUSHDB").OK(),
}); err != nil { ); err != nil {
return err return err
} }
if err := step(mc); err != nil { if err := step(mc); err != nil {
@ -102,7 +111,7 @@ func BenchmarkAll(b *testing.B) {
os.Exit(1) os.Exit(1)
}() }()
mc, err := mockOpenServer(true) mc, err := mockOpenServer(true, true)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }