-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
88 lines (74 loc) · 1.97 KB
/
Copy pathserver_test.go
File metadata and controls
88 lines (74 loc) · 1.97 KB
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
84
85
86
87
88
package main
import (
"context"
"log"
"net"
"testing"
"github.com/KyloRilo/load-balancer/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
)
type testCase struct {
name string
in interface{}
out interface{}
err error
}
func server() (proto.LoadBalancerClient, func()) {
buffer := 101024 * 1024
lis := bufconn.Listen(buffer)
baseServer := grpc.NewServer()
proto.RegisterLoadBalancerServer(baseServer, NewServer())
log.Printf("server listening at %v", lis.Addr())
go func() {
if err := baseServer.Serve(lis); err != nil {
log.Printf("failed to serve: %v", err)
}
}()
conn, err := grpc.NewClient("", grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Printf("failed connecting to server")
}
go Balancer.healthCheck()
log.Print("Test server running...")
client := proto.NewLoadBalancerClient(conn)
return client, func() {
err := lis.Close()
if err != nil {
log.Printf("error, closing listener: %v", err)
}
baseServer.Stop()
}
}
func testValidator(t *testing.T, test testCase, out interface{}, err error) {
log.Println("Validating test case: ", test)
if err != nil && test.err != err {
t.Errorf("Detected Error mismatch => Wanted: %v, Received: %v", test.err, err)
}
if out != nil && test.out != out {
t.Errorf("Detected Resp mismatch => Wanted: %v, Received: %v", test.out, out)
}
}
func TestLbServer_AddConn(t *testing.T) {
log.Print("Init AddConn tests")
ctx := context.Background()
client, closer := server()
defer closer()
tests := []testCase{
{
name: "AddConnection_Succeeds",
in: &proto.AddConnReq{
Url: "google.com",
},
out: &proto.AddConnResp{},
},
}
log.Println("Init test cases: ", tests)
for _, test := range tests {
resp, err := client.AddConn(ctx, test.in.(*proto.AddConnReq))
testValidator(t, test, resp, err)
}
}