-
Notifications
You must be signed in to change notification settings - Fork 116
/
service_test.go
65 lines (57 loc) · 1.02 KB
/
service_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 (
"fmt"
"golang.org/x/net/context"
"google.golang.org/grpc"
"io"
"math/rand"
pb "proto"
"testing"
"time"
)
const (
address = "localhost:51000"
)
func TestGamePing(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
// Set up a connection to the server.
conn, err := grpc.Dial(address)
if err != nil {
t.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGameServiceClient(conn)
stream, err := c.Stream(context.Background())
if err != nil {
t.Fatal(err)
}
const N = 10
waitc := make(chan struct{})
go func() {
i := 0
for {
if i == N {
close(waitc)
return
}
in, err := stream.Recv()
if err == io.EOF {
return
}
if err != nil {
t.Fatal(err)
}
t.Logf("reply: %v", string(in.Message))
i++
}
}()
for i := 0; i < N; i++ {
v := r.Int31()
t.Logf("ping with:%v", v)
if err := stream.Send(&pb.Game_Frame{Type: pb.Game_Ping, Message: []byte(fmt.Sprint(v))}); err != nil {
t.Fatal(err)
return
}
}
<-waitc
}