| 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 |
"bytes" |
| 21 |
"context" |
| 22 |
"io" |
| 23 |
"net/http" |
| 24 |
"net/http/httptest" |
| 25 |
"strconv" |
| 26 |
"sync" |
| 27 |
"testing" |
| 28 |
"time" |
| 29 |
|
| 30 |
"sigs.k8s.io/apiserver-network-proxy/tests/framework" |
| 31 |
) |
| 32 |
|
| 33 |
type simpleServer struct { |
| 34 |
mu sync.Mutex |
| 35 |
} |
| 36 |
|
| 37 |
func (s *simpleServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 38 |
s.mu.Lock() |
| 39 |
defer s.mu.Unlock() |
| 40 |
|
| 41 |
time.Sleep(time.Millisecond) |
| 42 |
|
| 43 |
bytes, err := io.ReadAll(req.Body) |
| 44 |
if err != nil { |
| 45 |
w.Write([]byte(err.Error())) |
| 46 |
} |
| 47 |
w.Write(bytes) |
| 48 |
} |
| 49 |
|
| 50 |
// TODO: test http-connect as well. |
| 51 |
func getTestClient(front string, t *testing.T) *http.Client { |
| 52 |
ctx := context.Background() |
| 53 |
tunnel, err := createSingleUseGrpcTunnel(ctx, front) |
| 54 |
if err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
|
| 58 |
return &http.Client{ |
| 59 |
Transport: &http.Transport{ |
| 60 |
DialContext: tunnel.DialContext, |
| 61 |
}, |
| 62 |
Timeout: framework.ForeverTestTimeout, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestConcurrentClientRequest(t *testing.T) { |
| 67 |
const numConcurrentRequests = 100 |
| 68 |
s := httptest.NewServer(&simpleServer{}) |
| 69 |
defer s.Close() |
| 70 |
|
| 71 |
ps := runGRPCProxyServerWithServerCount(t, 1) |
| 72 |
defer ps.Stop() |
| 73 |
|
| 74 |
// Run two agents |
| 75 |
a1 := runAgent(t, ps.AgentAddr()) |
| 76 |
a2 := runAgent(t, ps.AgentAddr()) |
| 77 |
defer a1.Stop() |
| 78 |
defer a2.Stop() |
| 79 |
waitForConnectedServerCount(t, 1, a1) |
| 80 |
waitForConnectedServerCount(t, 1, a2) |
| 81 |
|
| 82 |
var wg sync.WaitGroup |
| 83 |
wg.Add(numConcurrentRequests) |
| 84 |
for i := 0; i < numConcurrentRequests; i++ { |
| 85 |
id := i |
| 86 |
go func() { |
| 87 |
defer wg.Done() |
| 88 |
client1 := getTestClient(ps.FrontAddr(), t) |
| 89 |
|
| 90 |
r, err := client1.Post(s.URL, "text/plain", bytes.NewBufferString(strconv.Itoa(id))) |
| 91 |
if err != nil { |
| 92 |
t.Error(err) |
| 93 |
return |
| 94 |
} |
| 95 |
data, err := io.ReadAll(r.Body) |
| 96 |
if err != nil { |
| 97 |
t.Error(err) |
| 98 |
} |
| 99 |
r.Body.Close() |
| 100 |
|
| 101 |
if string(data) != strconv.Itoa(id) { |
| 102 |
t.Errorf("expect %d; got %s", id, string(data)) |
| 103 |
} |
| 104 |
}() |
| 105 |
} |
| 106 |
wg.Wait() |
| 107 |
} |