Debian Codegraph @

tests/concurrent_test.go

raw
1 /*
2 Copyright 2022 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package tests
18
19 import (
20 "context"
21 "io"
22 "net/http"
23 "net/http/httptest"
24 "sync"
25 "testing"
26
27 "sigs.k8s.io/apiserver-network-proxy/tests/framework"
28 )
29
30 func TestProxy_ConcurrencyGRPC(t *testing.T) {
31 ctx := context.Background()
32 length := 1 << 20
33 chunks := 10
34 server := httptest.NewServer(newSizedServer(length, chunks))
35 defer server.Close()
36
37 ps := runGRPCProxyServer(t)
38 defer ps.Stop()
39
40 a := runAgent(t, ps.AgentAddr())
41 defer a.Stop()
42 waitForConnectedServerCount(t, 1, a)
43
44 var wg sync.WaitGroup
45 verify := func() {
46 defer wg.Done()
47
48 // run test client
49 tunnel, err := createSingleUseGrpcTunnel(ctx, ps.FrontAddr())
50 if err != nil {
51 t.Error(err)
52 return
53 }
54
55 c := &http.Client{
56 Transport: &http.Transport{
57 DialContext: tunnel.DialContext,
58 },
59 }
60
61 r, err := c.Get(server.URL)
62 if err != nil {
63 t.Error(err)
64 return
65 }
66
67 data, err := io.ReadAll(r.Body)
68 if err != nil {
69 t.Error(err)
70 return
71 }
72 defer r.Body.Close()
73
74 if len(data) != length*chunks {
75 t.Errorf("expect data length %d; got %d", length*chunks, len(data))
76 }
77 }
78
79 concurrency := 10
80 wg.Add(concurrency)
81 for i := 0; i < concurrency; i++ {
82 go verify()
83 }
84 wg.Wait()
85 }
86
87 func TestProxy_ConcurrencyHTTP(t *testing.T) {
88 ctx := context.Background()
89 length := 1 << 20
90 chunks := 10
91 server := httptest.NewServer(newSizedServer(length, chunks))
92 defer server.Close()
93
94 ps := runHTTPConnProxyServer(t)
95 defer ps.Stop()
96
97 a := runAgent(t, ps.AgentAddr())
98 defer a.Stop()
99 waitForConnectedServerCount(t, 1, a)
100
101 // run test clients
102 var wg sync.WaitGroup
103 verify := func() {
104 defer wg.Done()
105 tunnel, err := createHTTPConnectClient(ctx, ps.FrontAddr(), server.URL)
106 if err != nil {
107 t.Error(err)
108 return
109 }
110 data, err := clientRequest(tunnel, server.URL)
111 if err != nil {
112 t.Error(err)
113 } else if len(data) != length*chunks {
114 t.Errorf("expect data length %d; got %d", length*chunks, len(data))
115 }
116 }
117
118 concurrency := 10
119 wg.Add(concurrency)
120 for i := 0; i < concurrency; i++ {
121 go verify()
122 }
123 wg.Wait()
124 }
125
126 // This test verifies that when one stream between a proxy agent and the proxy
127 // server terminates, the proxy server does not terminate other frontends
128 // supported by the same proxy agent but on different streams.
129 func TestAgent_MultipleConn(t *testing.T) {
130 testcases := []struct {
131 name string
132 proxyServerFunction func(testing.TB) framework.ProxyServer
133 clientFunction func(context.Context, string, string) (*http.Client, error)
134 }{
135 {
136 name: "grpc",
137 proxyServerFunction: runGRPCProxyServer,
138 clientFunction: createGrpcTunnelClient,
139 },
140 {
141 name: "http-connect",
142 proxyServerFunction: runHTTPConnProxyServer,
143 clientFunction: createHTTPConnectClient,
144 },
145 }
146
147 for _, tc := range testcases {
148 t.Run(tc.name, func(t *testing.T) {
149 ctx := context.Background()
150 waitServer := newWaitingServer()
151 server := httptest.NewServer(waitServer)
152 defer server.Close()
153
154 ps := tc.proxyServerFunction(t)
155 defer ps.Stop()
156
157 ai1 := runAgentWithID(t, "multipleAgentConn", ps.AgentAddr())
158 defer ai1.Stop()
159 waitForConnectedServerCount(t, 1, ai1)
160
161 // run test client
162 c, err := tc.clientFunction(ctx, ps.FrontAddr(), server.URL)
163 if err != nil {
164 t.Fatal(err)
165 }
166
167 fcnStopCh := make(chan struct{})
168
169 go func() {
170 _, err := clientRequest(c, server.URL)
171 if err != nil {
172 t.Errorf("expected no error on proxy request, got %v", err)
173 }
174 close(fcnStopCh)
175 }()
176 <-waitServer.requestReceivedCh
177
178 // Running an agent with the same ID simulates a second connection from the same agent.
179 // This simulates the scenario where a proxy agent established connections with HA proxy server
180 // and creates multiple connections with the same proxy server
181 ai2 := runAgentWithID(t, "multipleAgentConn", ps.AgentAddr())
182 defer ai2.Stop()
183 waitForConnectedServerCount(t, 1, ai2)
184 // Wait for the server to run cleanup routine
185 waitForConnectedAgentCount(t, 1, ps)
186 close(waitServer.respondCh)
187
188 <-fcnStopCh
189 })
190 }
191 }