Skip to content

Commit e2a8b6f

Browse files
author
Chao Xu
committed
fixing tests
1 parent 57d5d3e commit e2a8b6f

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

pkg/agent/agentclient/client_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ import (
1717
func TestServeData_HTTP(t *testing.T) {
1818
var err error
1919
var stream agent.AgentService_ConnectClient
20+
stopCh := make(chan struct{})
2021
client := &AgentClient{
2122
connContext: make(map[int64]*connContext),
23+
stopCh: stopCh,
2224
}
2325
client.stream, stream = pipe2()
24-
stopCh := make(chan struct{})
2526

2627
// Start agent
27-
go client.Serve(stopCh)
28+
go client.Serve()
2829
defer close(stopCh)
2930

3031
// Start test http server as remote service
@@ -112,14 +113,15 @@ func TestServeData_HTTP(t *testing.T) {
112113

113114
func TestClose_Client(t *testing.T) {
114115
var stream agent.AgentService_ConnectClient
116+
stopCh := make(chan struct{})
115117
client := &AgentClient{
116118
connContext: make(map[int64]*connContext),
119+
stopCh: stopCh,
117120
}
118121
client.stream, stream = pipe2()
119-
stopCh := make(chan struct{})
120122

121123
// Start agent
122-
go client.Serve(stopCh)
124+
go client.Serve()
123125
defer close(stopCh)
124126

125127
// Start test http server as remote service

pkg/agent/agentclient/stream.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ func (c *RedialableAgentClient) probe() {
109109
case <-c.stopCh:
110110
return
111111
case <-time.After(c.Interval):
112+
if c.conn == nil {
113+
continue
114+
}
112115
// health check
113-
if c.conn != nil && c.conn.GetState() == connectivity.Ready {
116+
if c.conn.GetState() == connectivity.Ready {
114117
continue
115118
} else {
116119
klog.Infof("Connection state %v", c.conn.GetState())
@@ -256,7 +259,7 @@ func (c *RedialableAgentClient) reconnect() {
256259
}
257260
switch {
258261
case r.serverID == c.serverID:
259-
klog.Info("reconnected to %s", serverID)
262+
klog.Infof("reconnected to %s", r.serverID)
260263
c.conn = r.grpcConn
261264
c.stream = r.agentServiceClient
262265
c.doneReconnect(nil)
@@ -308,6 +311,7 @@ func serverCount(stream agent.AgentService_ConnectClient) (int, error) {
308311
}
309312

310313
func serverID(stream agent.AgentService_ConnectClient) (string, error) {
314+
// TODO: this is a blocking call. Add a timeout?
311315
md, err := stream.Header()
312316
if err != nil {
313317
return "", err

pkg/agent/agentclient/stream_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/google/uuid"
1011
"google.golang.org/grpc"
12+
"google.golang.org/grpc/metadata"
1113
"k8s.io/klog"
1214
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
15+
"sigs.k8s.io/apiserver-network-proxy/proto/header"
1316
)
1417

1518
func TestReconnectExits(t *testing.T) {
@@ -19,7 +22,7 @@ func TestReconnectExits(t *testing.T) {
1922

2023
time.Sleep(time.Millisecond)
2124

22-
client, err := NewRedialableAgentClient("localhost:8899", grpc.WithInsecure())
25+
client, err := NewRedialableAgentClient("localhost:8899", uuid.New().String(), &ClientSet{}, grpc.WithInsecure())
2326
if err != nil {
2427
t.Fatal(err)
2528
}
@@ -83,6 +86,11 @@ func newTestServer(addr string) *testServer {
8386
func (s *testServer) Connect(stream agent.AgentService_ConnectServer) error {
8487
stopCh := make(chan error)
8588

89+
h := metadata.Pairs(header.ServerID, "", header.ServerCount, "1")
90+
if err := stream.SendHeader(h); err != nil {
91+
return err
92+
}
93+
8694
// Recv only
8795
go func() {
8896
for {

tests/proxy_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/google/uuid"
1516
"google.golang.org/grpc"
1617
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/agentclient"
1718
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/agentserver"
@@ -227,7 +228,7 @@ func runGRPCProxyServer() (proxy, func(), error) {
227228
var err error
228229
var lis, lis2 net.Listener
229230

230-
server := agentserver.NewProxyServer()
231+
server := agentserver.NewProxyServer(uuid.New().String(), 0)
231232
grpcServer := grpc.NewServer()
232233
agentServer := grpc.NewServer()
233234
cleanup := func() {
@@ -262,7 +263,7 @@ func runGRPCProxyServer() (proxy, func(), error) {
262263

263264
func runHTTPConnProxyServer() (proxy, func(), error) {
264265
var proxy proxy
265-
server := agentserver.NewProxyServer()
266+
server := agentserver.NewProxyServer(uuid.New().String(), 0)
266267
agentServer := grpc.NewServer()
267268

268269
agent.RegisterAgentServiceServer(agentServer, server)
@@ -304,12 +305,8 @@ func runHTTPConnProxyServer() (proxy, func(), error) {
304305
}
305306

306307
func runAgent(addr string, stopCh <-chan struct{}) error {
307-
client, err := agentclient.NewAgentClient(addr, grpc.WithInsecure())
308-
if err != nil {
309-
return err
310-
}
311-
312-
go client.Serve(stopCh)
308+
client := agentclient.NewAgentClientSet(addr, uuid.New().String(), 5*time.Second, grpc.WithInsecure())
309+
go client.Serve()
313310

314311
return nil
315312
}

0 commit comments

Comments
 (0)