-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvd_test.go
77 lines (72 loc) · 2.04 KB
/
kvd_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
66
67
68
69
70
71
72
73
74
75
76
package kvd
import (
"testing"
"google.golang.org/grpc"
"github.com/gnossen/kvd/client"
"github.com/gnossen/kvd/server"
pb "github.com/gnossen/kvd/kvd"
"github.com/golang/protobuf/proto"
)
func TestUnary(t *testing.T) {
server, lis := server.NewServer(1234)
go server.Serve(lis)
defer server.Stop()
defer lis.Close()
conn, err := grpc.Dial("localhost:1234", []grpc.DialOption{grpc.WithInsecure()}...)
if err != nil {
t.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
cl := pb.NewKeyValueStoreClient(conn)
record := client.Create(cl, "foo", "oof")
expected := pb.Record{Name: "foo", Value: "oof"}
if !proto.Equal(record, &expected) {
t.Fatalf("Expected '%v', got '%v'", expected, *record)
}
record = client.Get(cl, "foo")
if !proto.Equal(record, &expected) {
t.Fatalf("Expected '%v', got '%v'", expected, *record)
}
record = client.Update(cl, "foo", "bigoof")
expected = pb.Record{Name: "foo", Value: "bigoof"}
if !proto.Equal(record, &expected) {
t.Fatalf("Expected '%v', got '%v'", expected, *record)
}
record = client.Get(cl, "foo")
if !proto.Equal(record, &expected) {
t.Fatalf("Expected '%v', got '%v'", expected, *record)
}
}
func TestWatch(t *testing.T) {
server, lis := server.NewServer(1234)
go server.Serve(lis)
defer server.Stop()
defer lis.Close()
conn, err := grpc.Dial("localhost:1234", []grpc.DialOption{grpc.WithInsecure()}...)
if err != nil {
t.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
cl := pb.NewKeyValueStoreClient(conn)
c := client.Watch(cl, "foo", 3)
go func() {
client.Create(cl, "foo", "5")
client.Update(cl, "foo", "6")
client.Update(cl, "foo", "7")
}()
expected := pb.Record{Name: "foo", Value: "5"}
record := <-c
if !proto.Equal(record, &expected) {
t.Fatalf("Expected '%v', got '%v'", expected, *record)
}
expected.Value = "6"
record = <-c
if !proto.Equal(record, &expected) {
t.Fatalf("Expected '%v', got '%v'", expected, *record)
}
expected.Value = "7"
record = <-c
if !proto.Equal(record, &expected) {
t.Fatalf("Expected '%v', got '%v'", expected, *record)
}
}