-
Notifications
You must be signed in to change notification settings - Fork 1
/
something_test.go
83 lines (66 loc) · 1.76 KB
/
something_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
77
78
79
80
81
82
83
package main_test
import (
"context"
"testing"
"google.golang.org/grpc"
pb "github.com/ramonberrutti/grpc-streaming-example/protogen"
)
type sometringUnary struct {
pb.UnimplementedSomethingUnaryServer
}
func (s *sometringUnary) GetSomething(ctx context.Context, req *pb.SomethingRequest) (*pb.SomethingResponse, error) {
return &pb.SomethingResponse{
Message: "Hello " + req.Name,
}, nil
}
func TestSomethingUnary(t *testing.T) {
conn := newServer(t, func(s grpc.ServiceRegistrar) {
pb.RegisterSomethingUnaryServer(s, &sometringUnary{})
})
client := pb.NewSomethingUnaryClient(conn)
response, err := client.GetSomething(
context.Background(),
&pb.SomethingRequest{
Name: "test",
},
)
if err != nil {
t.Fatalf("failed to get something: %v", err)
}
if response.Message != "Hello test" {
t.Errorf("unexpected response: %v", response.Message)
}
}
type sometringStream struct {
pb.UnimplementedSomethingStreamServer
}
func (s *sometringStream) GetSomething(req *pb.SomethingRequest, stream pb.SomethingStream_GetSomethingServer) error {
if err := stream.Send(&pb.SomethingResponse{
Message: "Hello " + req.Name,
}); err != nil {
return err
}
return nil
}
func TestSomethingStream(t *testing.T) {
conn := newServer(t, func(s grpc.ServiceRegistrar) {
pb.RegisterSomethingStreamServer(s, &sometringStream{})
})
client := pb.NewSomethingStreamClient(conn)
stream, err := client.GetSomething(
context.Background(),
&pb.SomethingRequest{
Name: "test",
},
)
if err != nil {
t.Fatalf("failed to get something stream: %v", err)
}
response, err := stream.Recv()
if err != nil {
t.Fatalf("failed to receive response: %v", err)
}
if response.Message != "Hello test" {
t.Errorf("unexpected response: %v", response.Message)
}
}