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

added some test cases and formatted code

parent bf567f5b
...@@ -10,3 +10,5 @@ completed: ...@@ -10,3 +10,5 @@ completed:
yet to complete: yet to complete:
-when sending commands from test case, read/write sync breaks -when sending commands from test case, read/write sync breaks
-test cases need to be made -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 ( ...@@ -24,9 +24,9 @@ const (
NOREPLY = "noreply" NOREPLY = "noreply"
// //
// //response // //response
OK = "OK" OK = "OK"
CRLF = "\r\n" CRLF = "\r\n"
VALUE = "VALUE" VALUE = "VALUE"
DELETED = "DELETED" DELETED = "DELETED"
//errors //errors
...@@ -106,7 +106,7 @@ func handleClient(conn net.Conn, table *KeyValueStore) { ...@@ -106,7 +106,7 @@ func handleClient(conn net.Conn, table *KeyValueStore) {
defer conn.Close() defer conn.Close()
for { for {
if msg, ok := read(conn, 1024); ok { if msg, ok := read(conn, 1024); ok {
if len(msg) == 0{ if len(msg) == 0 {
continue continue
} }
parseInput(conn, string(msg), table) parseInput(conn, string(msg), table)
...@@ -404,7 +404,7 @@ func performCas(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, i ...@@ -404,7 +404,7 @@ func performCas(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, i
e, _ := strconv.ParseUint(tokens[1], 10, 64) e, _ := strconv.ParseUint(tokens[1], 10, 64)
ve, _ := strconv.ParseUint(tokens[2], 10, 64) ve, _ := strconv.ParseUint(tokens[2], 10, 64)
n, _ := strconv.ParseUint(tokens[3], 10, 64) n, _ := strconv.ParseUint(tokens[3], 10, 64)
r := true r := true
logger.Println(k, e, ve, n, r) logger.Println(k, e, ve, n, r)
if len(tokens) == 5 && tokens[4] == NOREPLY { if len(tokens) == 5 && tokens[4] == NOREPLY {
...@@ -438,7 +438,7 @@ func performCas(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, i ...@@ -438,7 +438,7 @@ func performCas(conn net.Conn, tokens []string, table *KeyValueStore) (uint64, i
return 0, 3, r //key not found 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] k := tokens[0]
defer table.Unlock() 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