Commit 1058b2b9 authored by Sushant Mahajan's avatar Sushant Mahajan

added test cases for cas

parent 5f87d268
......@@ -259,6 +259,8 @@ func parseInput(conn net.Conn, msg string, table *KeyValueStore, ch chan []byte)
return
}
//fmt.Println(tokens)
//for efficient string concatenation
var buffer bytes.Buffer
switch tokens[0] {
......@@ -660,9 +662,10 @@ func main() {
//toLog = "s"
if toLog != "" {
logf, _ := os.OpenFile("serverlog.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
logf, _ := os.OpenFile("serverlog.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
defer logf.Close()
logger = log.New(logf, "SERVER: ", log.Ltime|log.Lshortfile)
//logger = log.New(os.Stdout, "SERVER: ", log.Ltime|log.Lshortfile)
} else {
logger = log.New(ioutil.Discard, "SERVER: ", log.Ldate)
}
......
......@@ -19,14 +19,9 @@ func TestSerial(t *testing.T) {
go main()
//give some time for server to initialize
time.Sleep(time.Second)
testSetCommand(t)
testIndividualCommands(t)
}
func testSetCommand(t *testing.T) {
testSetReplyExpected(t)
}
func testSetReplyExpected(t *testing.T) {
func testIndividualCommands(t *testing.T) {
conn, err := net.Dial("tcp", ":5000")
defer conn.Close()
time.Sleep(time.Millisecond)
......@@ -35,18 +30,75 @@ func testSetReplyExpected(t *testing.T) {
}
cases := []TestCasePair{
{[]byte("set a 200 10\r\n1234567890\r\n"), []byte("OK 2\r\n")}, //single length
{[]byte("set b 200 10\r\n12345\r\n890\r\n"), []byte("OK 3\r\n")}, //\r\n in middle
{[]byte("set c 0 10\r\n12345\r\n890\r\n"), []byte("OK 4\r\n")}, //perpetual key
{[]byte("set \n 200 10\r\n1234567890\r\n"), []byte("ERR_CMD_ERR\r\nERR_CMD_ERR\r\n")}, //newline key (error)
{[]byte("set d 200 10\r\n12345678901\r\n"), []byte("ERR_CMD_ERR\r\n")}, //value length greater (error)
{[]byte("set e 200 10\r\n1234\r6789\r\n"), []byte("ERR_CMD_ERR\r\n")}, //value length less (error)
/*test set command*/
{[]byte("set k 1 10" + CRLF + "1234567890" + CRLF), []byte("OK 2" + CRLF)}, //fast expiring
{[]byte("set l 1 10" + CRLF + "1234567890" + CRLF), []byte("OK 3" + CRLF)}, //fast expiring
{[]byte("set m 1 10" + CRLF + "1234567890" + CRLF), []byte("OK 4" + CRLF)}, //fast expiring
{[]byte("set n 1 10" + CRLF + "1234567890" + CRLF), []byte("OK 5" + CRLF)}, //fast expiring
{[]byte("set a 200 10" + CRLF + "1234567890" + CRLF), []byte("OK 6" + CRLF)}, //single length
{[]byte("set b 200 10" + CRLF + "12345" + CRLF + "890" + CRLF), []byte("OK 7" + CRLF)}, //"+CRLF+" in middle
{[]byte("set c 0 10" + CRLF + "12345" + CRLF + "890" + CRLF), []byte("OK 8" + CRLF)}, //perpetual key
{[]byte("set \n 200 10" + CRLF + "1234567890" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //newline key (error)
{[]byte("set d 200 10" + CRLF + "12345678901" + CRLF), []byte("ERR_CMD_ERR" + CRLF)}, //value length greater (error)
{[]byte("set e 200 10" + CRLF + "1234\r6789" + CRLF), []byte("ERR_CMD_ERR" + CRLF)}, //value length less (error)
//key length high (250 bytes)
{[]byte("set 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 200 10\r\n1234\r67890\r\n"), []byte("OK 5\r\n")},
{[]byte("set 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 200 10" + CRLF + "1234\r67890" + CRLF), []byte("OK 9" + CRLF)},
//version update
{[]byte("set changing 200 2" + CRLF + "12" + CRLF + "set changing 200 2" + CRLF + "12" + CRLF + "set changing 200 2" + CRLF + "12" + CRLF), []byte("OK 10" + CRLF + "OK 11" + CRLF + "OK 12" + CRLF)},
//key length high (251 bytes), error
{[]byte("set 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901 200 10\r\n1234\r67890\r\n"), []byte("ERR_CMD_ERR\r\nERR_CMD_ERR\r\n")},
{[]byte("set f -1 10\r\n1234\r6\r89\r\n"), []byte("ERR_CMD_ERR\r\nERR_CMD_ERR\r\n")}, //invalid expiry
{[]byte("set f 200 0\r\n1234\r6\r89\r\n"), []byte("ERR_CMD_ERR\r\nERR_CMD_ERR\r\n")}, //invalid value size
{[]byte("set 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901 200 10" + CRLF + "1234\r67890" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)},
{[]byte("set f -1 10" + CRLF + "1234\r6\r89" + CRLF), []byte(ERR_CMD_ERR + CRLF + ERR_CMD_ERR + CRLF)}, //invalid expiry (number)
{[]byte("set g xyz 10" + CRLF + "1234\r6\r890" + CRLF), []byte(ERR_CMD_ERR + CRLF + ERR_CMD_ERR + CRLF)}, //invalid expiry (string)
{[]byte("set h 200 -1" + CRLF + "1234\r6\r89" + CRLF), []byte(ERR_CMD_ERR + CRLF + ERR_CMD_ERR + CRLF)}, //invalid value size (number)
{[]byte("set i 200 xyz" + CRLF + "1234\r6\r89" + CRLF), []byte(ERR_CMD_ERR + CRLF + ERR_CMD_ERR + CRLF)}, //invalid value size (string)
{[]byte("set j 200 10 20" + CRLF + "1234\r6\r89" + CRLF), []byte(ERR_CMD_ERR + CRLF + ERR_CMD_ERR + CRLF)}, //invalid number of args >
{[]byte("set j 200" + CRLF + "1234\r6\r89" + CRLF), []byte(ERR_CMD_ERR + CRLF + ERR_CMD_ERR + CRLF)}, //invalid number of args <
/*test get command*/
{[]byte("get a" + CRLF), []byte("VALUE 10" + CRLF + "1234567890" + CRLF)}, //fetch key set earlier
{[]byte("get b" + CRLF), []byte("VALUE 10" + CRLF + "12345" + CRLF + "890" + CRLF)}, //fetch key with value having "+CRLF+" set earlier
{[]byte("get non_existant" + CRLF), []byte(ERR_NOT_FOUND + CRLF)}, //fetch non existant key
{[]byte("get k" + CRLF), []byte(ERR_NOT_FOUND + CRLF)}, //fetch expired key (k)
{[]byte("get a 2" + CRLF), []byte(ERR_CMD_ERR + CRLF)}, //invalid number of args >
{[]byte("get" + CRLF), []byte(ERR_CMD_ERR + CRLF)}, //invalid number of args <
//invalid key size
{[]byte("get 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901" + CRLF), []byte(ERR_CMD_ERR + CRLF)},
/*test delete command*/
//set some keys first
{[]byte("set d1 200 10" + CRLF + "1234567890" + CRLF), []byte("OK 13" + CRLF)}, //some key will be deleted
{[]byte("set d2 200 10" + CRLF + "1234567890" + CRLF), []byte("OK 14" + CRLF)}, //some key will be deleted
//deletion begins
{[]byte("delete a" + CRLF), []byte(DELETED + CRLF)}, //delete normal key
{[]byte("delete l" + CRLF), []byte(ERR_NOT_FOUND + CRLF)}, //delete expired key
{[]byte("delete non_existant" + CRLF), []byte(ERR_NOT_FOUND + CRLF)}, //delete non existant key
{[]byte("delete d2 12" + CRLF), []byte(ERR_CMD_ERR + CRLF)}, //invalid arguments >
{[]byte("delete" + CRLF), []byte(ERR_CMD_ERR + CRLF)}, //invalid arguments <
/*test cas command*/
//set some keys first
{[]byte("set c1 200 10" + CRLF + "1234567890" + CRLF), []byte("OK 15" + CRLF)}, //some key will be deleted
{[]byte("set c2 200 10" + CRLF + "1234567890" + CRLF), []byte("OK 16" + CRLF)}, //some key will be deleted
//set new value of version 15 key c1 and retrieve the result
{[]byte("cas c1 300 15 9" + CRLF + "123456789" + CRLF), []byte("OK 17" + CRLF)}, //cas key set earlier
{[]byte("get c1" + CRLF), []byte("VALUE 9" + CRLF + "123456789" + CRLF)}, //verify cas'ed key
{[]byte("cas m 2 4 5" + CRLF + "12345" + CRLF), []byte("ERR_NOT_FOUND" + CRLF)}, //cas expired key
{[]byte("cas non_existant 2 4 5" + CRLF + "12345" + CRLF), []byte("ERR_NOT_FOUND" + CRLF)}, //cas non-existant key
{[]byte("cas c2 100 5 5" + CRLF + "12345" + CRLF), []byte("ERR_VERSION" + CRLF)}, //cas incorrect version (16)
{[]byte("cas \n 300 15 9" + CRLF + "123456789" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid key
{[]byte("cas c2 -1 5 5" + CRLF + "12345" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid expiry (int)
{[]byte("cas c2 xyz 5 5" + CRLF + "12345" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid expiry (string)
{[]byte("cas c2 100 -5 5" + CRLF + "12345" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid version (int)
{[]byte("cas c2 100 xyz 5" + CRLF + "12345" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid version (string)
{[]byte("cas c2 100 5 -5" + CRLF + "12345" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid value (int)
{[]byte("cas c2 100 5 xyz" + CRLF + "12345" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid value (string)
{[]byte("cas c2 100 5 5 6" + CRLF + "12345" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid arg num >
{[]byte("cas c2 100 5" + CRLF + "12345" + CRLF), []byte("ERR_CMD_ERR" + CRLF + "ERR_CMD_ERR" + CRLF)}, //invalid arg num <
}
for i, e := range cases {
......@@ -54,7 +106,7 @@ func testSetReplyExpected(t *testing.T) {
conn.Write(e.command)
n, _ := conn.Read(buf)
if !bytes.Equal(buf[:n], e.expected) {
fmt.Println(buf[:n], e.expected, string(buf[:n]), string(e.expected))
fmt.Println(buf[:n], e.expected, "S:"+string(buf[:n]), "E:"+string(e.expected))
t.Errorf("Error occured for case:" + strconv.Itoa(i))
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment