Commit 14b0a125 authored by Sushant Mahajan's avatar Sushant Mahajan

added some test cases and formatted code

parent bf567f5b
......@@ -10,3 +10,5 @@ completed:
yet to complete:
-when sending commands from test case, read/write sync breaks
-test cases need to be made
Note: inorder to run test cases you need to move the client.go file somewhere else
......@@ -24,9 +24,9 @@ const (
NOREPLY = "noreply"
//
// //response
OK = "OK"
CRLF = "\r\n"
VALUE = "VALUE"
OK = "OK"
CRLF = "\r\n"
VALUE = "VALUE"
DELETED = "DELETED"
//errors
......@@ -106,7 +106,7 @@ func handleClient(conn net.Conn, table *KeyValueStore) {
defer conn.Close()
for {
if msg, ok := read(conn, 1024); ok {
if len(msg) == 0{
if len(msg) == 0 {
continue
}
parseInput(conn, string(msg), table)
......@@ -404,7 +404,7 @@ func performCas(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, i
e, _ := strconv.ParseUint(tokens[1], 10, 64)
ve, _ := strconv.ParseUint(tokens[2], 10, 64)
n, _ := strconv.ParseUint(tokens[3], 10, 64)
r := true
r := true
logger.Println(k, e, ve, n, r)
if len(tokens) == 5 && tokens[4] == NOREPLY {
......@@ -438,7 +438,7 @@ func performCas(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, i
return 0, 3, r //key not found
}
func performDelete(conn net.Conn, tokens []string, table *KeyValueStore) (bool) {
func performDelete(conn net.Conn, tokens []string, table *KeyValueStore) bool {
k := tokens[0]
defer table.Unlock()
......
package main
import (
"bytes"
"net"
"testing"
"time"
)
func TestSet(t *testing.T) {
go main()
conn, err := net.Dial("tcp", "localhost:5000")
if err != nil {
t.Errorf("Error connecting to server")
} else {
time.Sleep(time.Second*2)
conn.Write([]byte("set xyz 200 10\r\n"))
time.Sleep(time.Millisecond)
conn.Write([]byte("abcd\r\n"))
buffer := make([]byte, 1024)
conn.Read(buffer)
msg := string(buffer)
if msg == ERR_CMD_ERR+"\r\n" {
t.Errorf("Expected OK <version>")
}
}
}
func TestGet(t *testing.T) {
//go main()
conn, err := net.Dial("tcp", "localhost:5000")
if err != nil {
t.Errorf("Error connecting to server")
} else {
time.Sleep(time.Second)
conn.Write([]byte("set xyz 200 10\r\n"))
time.Sleep(time.Millisecond)
conn.Write([]byte("abcdefg\r\n"))
buffer := make([]byte, 1024)
conn.Read(buffer)
msg := string(buffer)
if msg == ERR_CMD_ERR+"\r\n" {
t.Errorf("Expected OK <version>")
}
conn.Write([]byte("get xyz\r\n"))
time.Sleep(time.Millisecond)
conn.Read(buffer)
msg = string(buffer)
if msg == ERR_CMD_ERR+"\r\n" {
t.Errorf("Expected key value")
}
conn.Write([]byte("get tuv\r\n"))
time.Sleep(time.Millisecond)
buffer = make([]byte, 1024)
conn.Read(buffer)
n := bytes.Index(buffer, []byte{0})
msg = string(buffer[:n])
if msg != ERR_NOT_FOUND+"\r\n" {
t.Errorf("Expected key value")
}
}
}
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