tile38/tests/client_test.go

142 lines
4.4 KiB
Go
Raw Permalink Normal View History

package tests
import (
"errors"
"fmt"
2022-09-24 16:22:58 +03:00
"strings"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson"
2022-09-24 23:41:36 +03:00
"github.com/tidwall/pretty"
)
2022-09-26 23:26:46 +03:00
func subTestClient(g *testGroup) {
g.regSubTest("OUTPUT", client_OUTPUT_test)
g.regSubTest("CLIENT", client_CLIENT_test)
}
2022-09-24 16:22:58 +03:00
func client_OUTPUT_test(mc *mockServer) error {
if err := mc.DoBatch(
// tests removal of "elapsed" member.
2022-09-28 00:19:57 +03:00
Do("OUTPUT", "json", "yaml").Err(`wrong number of arguments for 'output' command`),
2022-09-24 16:22:58 +03:00
Do("OUTPUT", "json").Str(`{"ok":true}`),
2022-09-28 00:19:57 +03:00
Do("OUTPUT").JSON().Str(`{"ok":true,"output":"json"}`),
Do("OUTPUT").Str(`resp`), // this is due to the internal Do test
2022-09-24 16:22:58 +03:00
Do("OUTPUT", "resp").OK(),
2022-09-28 00:19:57 +03:00
Do("OUTPUT", "yaml").Err(`invalid argument 'yaml'`),
Do("OUTPUT").Str(`resp`),
Do("OUTPUT").JSON().Str(`{"ok":true,"output":"json"}`),
2022-09-24 16:22:58 +03:00
); err != nil {
return err
}
2022-09-24 16:22:58 +03:00
// run direct commands
if _, err := mc.Do("OUTPUT", "json"); err != nil {
return err
}
res, err := mc.Do("CLIENT", "list")
if err != nil {
return err
}
bres, ok := res.([]byte)
if !ok {
return errors.New("Failed to type assert CLIENT response")
}
sres := string(bres)
if !gjson.Valid(sres) {
return errors.New("CLIENT response was invalid")
}
info := gjson.Get(sres, "list").String()
if !gjson.Valid(info) {
return errors.New("CLIENT.list response was invalid")
}
return nil
}
2022-09-24 16:22:58 +03:00
func client_CLIENT_test(mc *mockServer) error {
numConns := 20
var conns []redis.Conn
2022-09-24 16:22:58 +03:00
defer func() {
for i := range conns {
conns[i].Close()
}
}()
for i := 0; i <= numConns; i++ {
conn, err := redis.Dial("tcp", fmt.Sprintf(":%d", mc.port))
if err != nil {
return err
}
conns = append(conns, conn)
}
2022-09-24 23:41:36 +03:00
_, err := conns[1].Do("CLIENT", "setname", "cl1")
if err != nil {
return err
}
_, err = conns[2].Do("CLIENT", "setname", "cl2")
if err != nil {
return err
}
if _, err := mc.Do("OUTPUT", "JSON"); err != nil {
return err
}
res, err := mc.Do("CLIENT", "list")
if err != nil {
return err
}
bres, ok := res.([]byte)
if !ok {
return errors.New("Failed to type assert CLIENT response")
}
2022-09-24 23:41:36 +03:00
sres := string(pretty.Pretty(bres))
if int(gjson.Get(sres, "list.#").Int()) < numConns {
return errors.New("Invalid number of connections")
}
2022-09-24 16:22:58 +03:00
2022-09-24 23:41:36 +03:00
client13ID := gjson.Get(sres, "list.13.id").String()
client14Addr := gjson.Get(sres, "list.14.addr").String()
client15Addr := gjson.Get(sres, "list.15.addr").String()
2022-09-24 16:22:58 +03:00
return mc.DoBatch(
Do("CLIENT", "list").JSON().Func(func(s string) error {
if int(gjson.Get(s, "list.#").Int()) < numConns {
return errors.New("Invalid number of connections")
}
return nil
}),
Do("CLIENT", "list").Func(func(s string) error {
if len(strings.Split(strings.TrimSpace(s), "\n")) < numConns {
return errors.New("Invalid number of connections")
}
return nil
}),
Do("CLIENT").Err(`wrong number of arguments for 'client' command`),
Do("CLIENT", "hello").Err(`Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME)`),
Do("CLIENT", "list", "arg3").Err(`wrong number of arguments for 'client' command`),
Do("CLIENT", "getname", "arg3").Err(`wrong number of arguments for 'client' command`),
Do("CLIENT", "getname").JSON().Str(`{"ok":true,"name":""}`),
Do("CLIENT", "getname").Str(``),
2022-09-24 23:41:36 +03:00
Do("CLIENT", "setname", "abc").OK(),
Do("CLIENT", "getname").Str(`abc`),
Do("CLIENT", "getname").JSON().Str(`{"ok":true,"name":"abc"}`),
Do("CLIENT", "setname", "abc", "efg").Err(`wrong number of arguments for 'client' command`),
Do("CLIENT", "setname", " abc ").Err(`Client names cannot contain spaces, newlines or special characters.`),
Do("CLIENT", "setname", "abcd").JSON().OK(),
Do("CLIENT", "kill", "name", "abcd").Err("No such client"),
Do("CLIENT", "getname").Str(`abcd`),
Do("CLIENT", "kill").Err(`wrong number of arguments for 'client' command`),
Do("CLIENT", "kill", "").Err(`No such client`),
Do("CLIENT", "kill", "abcd").Err(`No such client`),
Do("CLIENT", "kill", "id", client13ID).OK(),
Do("CLIENT", "kill", "id").Err("wrong number of arguments for 'client' command"),
Do("CLIENT", "kill", client14Addr).OK(),
Do("CLIENT", "kill", client14Addr, "yikes").Err("wrong number of arguments for 'client' command"),
Do("CLIENT", "kill", "addr").Err("wrong number of arguments for 'client' command"),
Do("CLIENT", "kill", "addr", client15Addr).JSON().OK(),
Do("CLIENT", "kill", "addr", client14Addr, "yikes").Err("wrong number of arguments for 'client' command"),
Do("CLIENT", "kill", "id", "1000").Err("No such client"),
2022-09-24 16:22:58 +03:00
)
}