-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
65 lines (52 loc) · 1.35 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bytes"
"net"
"strings"
"testing"
)
// this is a mock for the network connection that is read from and written to
type ConnectionMock struct {
*bytes.Buffer
*strings.Reader
}
func (c ConnectionMock) Read(p []byte) (n int, err error) {
return c.Reader.Read(p)
}
func (c ConnectionMock) Write(p []byte) (n int, err error) {
return c.Buffer.Write(p)
}
func (c ConnectionMock) Close() error {
return nil
}
func GetConnectionMock(inputString string) ConnectionMock {
inputStringReader := strings.NewReader(inputString)
var b bytes.Buffer
c := ConnectionMock{
Buffer: &b,
Reader: inputStringReader,
}
return c
}
func TestStartServer(t *testing.T) {
go startServer("6379")
conn, err := net.Dial("tcp", "localhost:6379")
if err != nil {
t.Errorf("error when connecting to GoKV server %s", err.Error())
}
defer conn.Close()
message := "*1\r\n$4\r\nPING\r\n"
_, err = conn.Write([]byte(message))
if err != nil {
t.Errorf("error when sending message over tcp connection %s", err.Error())
}
response := make([]byte, 1024)
_, err = conn.Read(response)
if err != nil {
t.Errorf("error when sending message over tcp connection %s", err.Error())
}
expectedResult := "PONG"
if !strings.Contains(string(response), expectedResult) {
t.Errorf("expected result of '%s', recieved '%s'", expectedResult, string(response))
}
}