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
...@@ -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)
...@@ -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