Better FOLLOW/MONITOR tests

This commit is contained in:
tidwall 2022-09-27 14:06:24 -07:00
parent 8e0bf1957d
commit 77b4efa55f
3 changed files with 104 additions and 22 deletions

View File

@ -21,6 +21,8 @@ func follower_follow_test(mc *mockServer) error {
Do("SET", "mykey", "truck4", "POINT", 10, 10).OK(), Do("SET", "mykey", "truck4", "POINT", 10, 10).OK(),
Do("SET", "mykey", "truck5", "POINT", 10, 10).OK(), Do("SET", "mykey", "truck5", "POINT", 10, 10).OK(),
Do("SET", "mykey", "truck6", "POINT", 10, 10).OK(), Do("SET", "mykey", "truck6", "POINT", 10, 10).OK(),
Do("CONFIG", "SET", "requirepass", "1234").OK(),
Do("AUTH", "1234").OK(),
) )
if err != nil { if err != nil {
return err return err
@ -31,34 +33,36 @@ func follower_follow_test(mc *mockServer) error {
Do("GET", "mykey2", "truck1").Str(`{"type":"Point","coordinates":[10,10]}`), Do("GET", "mykey2", "truck1").Str(`{"type":"Point","coordinates":[10,10]}`),
Do("GET", "mykey2", "truck2").Str(`{"type":"Point","coordinates":[10,10]}`), Do("GET", "mykey2", "truck2").Str(`{"type":"Point","coordinates":[10,10]}`),
Do("CONFIG", "SET", "leaderauth", "1234").OK(),
Do("FOLLOW", "localhost", mc.port).OK(), Do("FOLLOW", "localhost", mc.port).OK(),
Do("GET", "mykey2", "truck1").Err("catching up to leader"), Do("GET", "mykey", "truck1").Err("catching up to leader"),
Sleep(time.Second), Sleep(time.Second/2),
Do("GET", "mykey2", "truck1").Err(`<nil>`),
Do("GET", "mykey2", "truck2").Err(`<nil>`), Do("GET", "mykey", "truck1").Err(`{"type":"Point","coordinates":[10,10]}`),
Do("GET", "mykey", "truck2").Err(`{"type":"Point","coordinates":[10,10]}`),
) )
if err != nil { if err != nil {
return err return err
} }
// err = mc.DoBatch( err = mc.DoBatch(
// Do("SET", "mykey", "truck7", "POINT", 10, 10).OK(), Do("SET", "mykey", "truck7", "POINT", 10, 10).OK(),
// Do("SET", "mykey", "truck8", "POINT", 10, 10).OK(), Do("SET", "mykey", "truck8", "POINT", 10, 10).OK(),
// Do("SET", "mykey", "truck9", "POINT", 10, 10).OK(), Do("SET", "mykey", "truck9", "POINT", 10, 10).OK(),
// ) )
// if err != nil { if err != nil {
// return err return err
// } }
// err = mc2.DoBatch( err = mc2.DoBatch(
// Sleep(time.Second/2), Sleep(time.Second/2),
// Do("GET", "mykey1", "truck7").Str(`{"type":"Point","coordinates":[10,10]}`), Do("GET", "mykey", "truck7").Str(`{"type":"Point","coordinates":[10,10]}`),
// Do("GET", "mykey1", "truck8").Str(`{"type":"Point","coordinates":[10,10]}`), Do("GET", "mykey", "truck8").Str(`{"type":"Point","coordinates":[10,10]}`),
// Do("GET", "mykey1", "truck9").Str(`{"type":"Point","coordinates":[10,10]}`), Do("GET", "mykey", "truck9").Str(`{"type":"Point","coordinates":[10,10]}`),
// ) )
// if err != nil { if err != nil {
// return err return err
// } }
return nil return nil
} }

77
tests/monitor_test.go Normal file
View File

@ -0,0 +1,77 @@
package tests
import (
"fmt"
"strings"
"sync"
"github.com/gomodule/redigo/redis"
)
func subTestMonitor(g *testGroup) {
g.regSubTest("monitor", follower_monitor_test)
}
func follower_monitor_test(mc *mockServer) error {
N := 1000
ch := make(chan error)
var wg sync.WaitGroup
wg.Add(1)
go func() {
ch <- func() error {
conn, err := redis.Dial("tcp", fmt.Sprintf("localhost:%d", mc.port))
if err != nil {
wg.Done()
return err
}
defer conn.Close()
s, err := redis.String(conn.Do("MONITOR"))
if err != nil {
wg.Done()
return err
}
if s != "OK" {
wg.Done()
return fmt.Errorf("expected '%s', got '%s'", "OK", s)
}
wg.Done()
for i := 0; i < N; i++ {
s, err := redis.String(conn.Receive())
if err != nil {
return err
}
ex := fmt.Sprintf(`"mykey" "%d"`, i)
if !strings.Contains(s, ex) {
return fmt.Errorf("expected '%s', got '%s'", ex, s)
}
}
return nil
}()
}()
wg.Wait()
conn, err := redis.Dial("tcp", fmt.Sprintf("localhost:%d", mc.port))
if err != nil {
return err
}
defer conn.Close()
for i := 0; i < N; i++ {
s, err := redis.String(conn.Do("SET", "mykey", i, "POINT", 10, 10))
if err != nil {
return err
}
if s != "OK" {
return fmt.Errorf("expected '%s', got '%s'", "OK", s)
}
}
err = <-ch
if err != nil {
err = fmt.Errorf("monitor client: %w", err)
}
return err
}

View File

@ -54,8 +54,9 @@ func TestIntegration(t *testing.T) {
regTestGroup("info", subTestInfo) regTestGroup("info", subTestInfo)
regTestGroup("timeouts", subTestTimeout) regTestGroup("timeouts", subTestTimeout)
regTestGroup("metrics", subTestMetrics) regTestGroup("metrics", subTestMetrics)
regTestGroup("aof", subTestAOF)
regTestGroup("follower", subTestFollower) regTestGroup("follower", subTestFollower)
regTestGroup("aof", subTestAOF)
regTestGroup("monitor", subTestMonitor)
runTestGroups(t) runTestGroups(t)
} }