-
Notifications
You must be signed in to change notification settings - Fork 43
/
l_test.go
60 lines (56 loc) · 1.25 KB
/
l_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
package rpc
import (
"context"
"fmt"
"testing"
demosrv "github.com/wjoj/tool/rpc/demorpc"
demorpc "github.com/wjoj/tool/rpc/demorpc/pb"
"google.golang.org/grpc"
)
func TestCli(t *testing.T) {
cfg := &ConfigClient{
ServiceName: "demo",
NonBlock: true,
BalancerName: "round_robin",
ConTimeout: 5,
Endpoints: []string{
"127.0.0.1:5202",
"127.0.0.1:5200",
"127.0.0.1:5201",
},
}
err := cfg.Start(func(conn *PoolClient) {
c, _ := conn.Conn()
cli := demorpc.NewHelloClient(c.ClientConn)
for i := 0; i < 10; i++ {
req, err := cli.Info(context.Background(), &demorpc.Request{
Name: fmt.Sprintf("i:%+v", i),
})
if err != nil {
fmt.Printf("\nrpc client req err:%v", err)
} else {
fmt.Printf("\nrpc client req:%+v", req)
}
}
})
fmt.Printf("\ngrpc client err:%v", err)
}
func TestSrv(t *testing.T) {
adds := []int{5200, 5201, 5202}
for _, add := range adds {
cfg := &ConfigService{
Port: add,
ServiceName: "demo",
ConnectionTimeout: 5,
}
cfg.Start(func(srv *grpc.Server) {
demorpc.RegisterHelloServer(srv, &demosrv.Demo{
Addr: fmt.Sprintf("%v", add),
})
}, func(err error) {
fmt.Printf("\ngrpc service error:%v", err)
})
}
fmt.Printf("\n已启动")
select {}
}