Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.16](backport #5950) [proxytest] Wait all requests to finish before closing the server #5970

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions testing/proxytest/proxytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Proxy struct {
// proxiedRequests is a "request log" for every request the proxy receives.
proxiedRequests []string
proxiedRequestsMu sync.Mutex
requestsWG *sync.WaitGroup

opts options
log *slog.Logger
Expand Down Expand Up @@ -164,8 +165,9 @@ func New(t *testing.T, optns ...Option) *Proxy {
lv = slog.LevelDebug
}
p := Proxy{
opts: opts,
client: opts.client,
requestsWG: &sync.WaitGroup{},
opts: opts,
client: opts.client,
log: slog.New(slog.NewTextHandler(logfWriter(opts.logFn), &slog.HandlerOptions{
Level: lv,
})),
Expand All @@ -176,6 +178,12 @@ func New(t *testing.T, optns ...Option) *Proxy {

p.Server = httptest.NewUnstartedServer(
http.HandlerFunc(func(ww http.ResponseWriter, r *http.Request) {
// Sometimes, on CI obviously, the last log happens after the test
// finishes. See https://github.com/elastic/elastic-agent/issues/5869.
// Therefore, let's add an extra layer to try to avoid that.
p.requestsWG.Add(1)
defer p.requestsWG.Done()

w := &proxyResponseWriter{w: ww}

requestID := uuid.Must(uuid.NewV4()).String()
Expand All @@ -187,7 +195,7 @@ func New(t *testing.T, optns ...Option) *Proxy {

p.ServeHTTP(w, rrr)

opts.logFn(fmt.Sprintf("[%s] DONE %d - %s %s %s %s\n",
p.log.Info(fmt.Sprintf("[%s] DONE %d - %s %s %s %s\n",
requestID, w.statusCode, r.Method, r.URL, r.Proto, r.RemoteAddr))
}),
)
Expand Down Expand Up @@ -245,6 +253,15 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.serveHTTP(w, r)
}

func (p *Proxy) Close() {
// Sometimes, on CI obviously, the last log happens after the test
// finishes. See https://github.com/elastic/elastic-agent/issues/5869.
// So, manually wait all ongoing requests to finish.
p.requestsWG.Wait()

p.Server.Close()
}

func (p *Proxy) serveHTTP(w http.ResponseWriter, r *http.Request) {
resp, err := p.processRequest(r)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions testing/proxytest/proxytest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ func TestProxy(t *testing.T) {
func TestHTTPSProxy(t *testing.T) {
targetHost := "not-a-server.co"
proxy, client, target := prepareMTLSProxyAndTargetServer(t, targetHost)
defer proxy.Close()
defer target.Close()
t.Cleanup(func() {
proxy.Close()
target.Close()
})

tcs := []struct {
name string
Expand Down