Better READONLY tests

This commit is contained in:
tidwall 2022-09-26 17:58:51 -07:00
parent 588207d162
commit 659160289c
3 changed files with 38 additions and 19 deletions

View File

@ -1,44 +1,50 @@
package server package server
import ( import (
"strings"
"time" "time"
"github.com/tidwall/resp" "github.com/tidwall/resp"
"github.com/tidwall/tile38/internal/log" "github.com/tidwall/tile38/internal/log"
) )
func (s *Server) cmdReadOnly(msg *Message) (res resp.Value, err error) { // READONLY yes|no
func (s *Server) cmdREADONLY(msg *Message) (resp.Value, error) {
start := time.Now() start := time.Now()
vs := msg.Args[1:]
var arg string
var ok bool
if vs, arg, ok = tokenval(vs); !ok || arg == "" { // >> Args
return NOMessage, errInvalidNumberOfArguments
args := msg.Args
if len(args) != 2 {
return retrerr(errInvalidNumberOfArguments)
} }
if len(vs) != 0 {
return NOMessage, errInvalidNumberOfArguments switch args[1] {
} case "yes", "no":
update := false
switch strings.ToLower(arg) {
default: default:
return NOMessage, errInvalidArgument(arg) return retrerr(errInvalidArgument(args[1]))
case "yes": }
// >> Operation
var updated bool
if args[1] == "yes" {
if !s.config.readOnly() { if !s.config.readOnly() {
update = true updated = true
s.config.setReadOnly(true) s.config.setReadOnly(true)
log.Info("read only") log.Info("read only")
} }
case "no": } else {
if s.config.readOnly() { if s.config.readOnly() {
update = true updated = true
s.config.setReadOnly(false) s.config.setReadOnly(false)
log.Info("read write") log.Info("read write")
} }
} }
if update { if updated {
s.config.write(false) s.config.write(false)
} }
// >> Response
return OKMessage(msg, start), nil return OKMessage(msg, start), nil
} }

View File

@ -1181,7 +1181,7 @@ func (s *Server) command(msg *Message, client *Client) (
case "replconf": case "replconf":
res, err = s.cmdReplConf(msg, client) res, err = s.cmdReplConf(msg, client)
case "readonly": case "readonly":
res, err = s.cmdReadOnly(msg) res, err = s.cmdREADONLY(msg)
case "stats": case "stats":
res, err = s.cmdSTATS(msg) res, err = s.cmdSTATS(msg)
case "server": case "server":

View File

@ -20,6 +20,7 @@ func subTestAOF(g *testGroup) {
g.regSubTest("AOF", aof_AOF_test) g.regSubTest("AOF", aof_AOF_test)
g.regSubTest("AOFMD5", aof_AOFMD5_test) g.regSubTest("AOFMD5", aof_AOFMD5_test)
g.regSubTest("AOFSHRINK", aof_AOFSHRINK_test) g.regSubTest("AOFSHRINK", aof_AOFSHRINK_test)
g.regSubTest("READONLY", aof_READONLY_test)
} }
func loadAOFAndClose(aof any) error { func loadAOFAndClose(aof any) error {
@ -273,3 +274,15 @@ func aof_AOFSHRINK_test(mc *mockServer) error {
} }
return err return err
} }
func aof_READONLY_test(mc *mockServer) error {
return mc.DoBatch(
Do("SET", "mykey", "myid", "POINT", "10", "10").OK(),
Do("READONLY", "yes").OK(),
Do("SET", "mykey", "myid", "POINT", "10", "10").Err("read only"),
Do("READONLY", "no").OK(),
Do("SET", "mykey", "myid", "POINT", "10", "10").OK(),
Do("READONLY").Err("wrong number of arguments for 'readonly' command"),
Do("READONLY", "maybe").Err("invalid argument 'maybe'"),
)
}