-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontactServicebufconn_test.go
75 lines (68 loc) · 1.53 KB
/
contactServicebufconn_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
package main
import (
"context"
"log"
"net"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
"interview/proto"
"interview/services/contacts"
"interview/services/interests"
"interview/services/network"
"interview/services/users"
"interview/services/view"
)
//
const bufSize = 1024 * 1024
//
var lis *bufconn.Listener
//
//
func init() {
lis = bufconn.Listen(bufSize)
grpcServer := grpc.NewServer()
proto.RegisterViewNetworkServiceServer(grpcServer, &view.Server{})
proto.RegisterNetworkServiceServer(grpcServer, &network.Server{})
proto.RegisterUserServiceServer(grpcServer, &users.Server{})
proto.RegisterInterestsServiceServer(grpcServer, &interests.Server{})
proto.RegisterContactServiceServer(grpcServer, &contacts.Server{})
go func() {
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("Server exited with error: %v", err)
}
}()
}
//
//
func BufDialer(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
//
func TestContactServiceUsingBufcon(t *testing.T) {
//
ctx := context.Background()
//
conn, err := grpc.DialContext(ctx, "", grpc.WithContextDialer(BufDialer), grpc.WithInsecure())
if err != nil {
t.Errorf("Failed to dail %v ", err)
}
defer conn.Close()
//
client := proto.NewContactServiceClient(conn)
//
//
req := &proto.TwoUserKeys{
User1: &proto.UserKey{
Key: 2,
},
User2: &proto.UserKey{
Key: 3,
},
}
//
//
if _, err := client.GetCommonContacts(context.Background(), req); err != nil {
t.Errorf("Failed to find common contacts %v", err)
}
}