forked from rosedblabs/rosedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc_test.go
65 lines (59 loc) · 1.26 KB
/
grpc_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 rosedb
// run `bash gen_pb_go.sh` before you run this test
// run grpc server and listen to 127.0.0.1:5221
// just `cd cmd/server && go build main.go && ./main --config ../../config_grpc.toml`
import (
"context"
"github.com/roseduan/rosedb/cmd/proto"
"google.golang.org/grpc"
"log"
"testing"
"time"
)
var (
c proto.RosedbClient
conn *grpc.ClientConn = nil
err error
)
func GRPCSet() error {
conn, err = grpc.Dial("127.0.0.1:5300", grpc.WithInsecure())
if err != nil {
log.Printf("grpc dial err: %+v", err)
return err
}
c = proto.NewRosedbClient(conn)
return nil
}
func IsInit(t *testing.T) {
if conn == nil {
if et := GRPCSet(); et != nil {
t.Fatalf("error: %+v", et)
}
}
}
func TestGRPCSet(t *testing.T) {
IsInit(t)
rsp, et := c.Set(context.Background(), &proto.SetReq{
Key: []byte("test_grpc_set"),
Value: []byte("test_grpc_set" + time.Now().String()),
})
if et != nil {
t.Error(et)
}
if rsp.ErrorMsg != "" {
t.Errorf("error_msg: %s", rsp.ErrorMsg)
}
}
func TestGRPCGet(t *testing.T) {
IsInit(t)
rsp, et := c.Get(context.Background(), &proto.GetReq{
Key: []byte("test_grpc_set"),
})
if et != nil {
t.Error(et)
}
if rsp.ErrorMsg != "" {
t.Errorf("error_msg: %s", rsp.ErrorMsg)
}
log.Println(string(rsp.Dest))
}