Skip to content

Commit 6b270e6

Browse files
committed
cleanup
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
1 parent 24b43d5 commit 6b270e6

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

integration/main_test.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ func TestIntegration(t *testing.T) {
2626

2727
// Setup the httpbin upstream local server.
2828
httpbinHandler := httpbin.New()
29-
server := &http.Server{Addr: ":1234", Handler: httpbinHandler}
29+
server := &http.Server{Addr: ":1234", Handler: httpbinHandler,
30+
ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 5 * time.Second,
31+
WriteTimeout: 5 * time.Second,
32+
}
3033
go func() {
31-
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
34+
if err = server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
3235
t.Logf("HTTP server error: %v", err)
3336
}
3437
}()
35-
t.Cleanup(func() { _ = server.Close() })
38+
defer func() {
39+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
40+
defer cancel()
41+
_ = server.Shutdown(ctx)
42+
}()
3643

3744
// Health check to ensure the server is up before starting tests.
3845
require.Eventually(t, func() bool {
@@ -73,8 +80,7 @@ func TestIntegration(t *testing.T) {
7380
t.Cleanup(func() { require.NoError(t, cmd.Process.Signal(os.Interrupt)) })
7481
} else {
7582
// Now run Envoy with the env variable set for dynamic modules.
76-
ctx, cancel := context.WithCancel(context.Background())
77-
cmd := exec.CommandContext(ctx, "go", // nolint: gosec
83+
cmd := exec.Command("go", // nolint: gosec
7884
"tool", "func-e", "run",
7985
"-c", "envoy.yaml",
8086
"--log-level", "warn",
@@ -87,11 +93,14 @@ func TestIntegration(t *testing.T) {
8793
cmd.Stderr = os.Stderr
8894
cmd.Env = append(os.Environ(), "ENVOY_DYNAMIC_MODULES_SEARCH_PATH="+cwd)
8995
require.NoError(t, cmd.Start())
90-
t.Cleanup(func() {
91-
cancel()
96+
defer func() {
97+
// Send SIGTERM for graceful shutdown
98+
if err := cmd.Process.Signal(os.Interrupt); err != nil {
99+
t.Logf("failed to interrupt envoy: %v", err)
100+
}
92101
time.Sleep(3 * time.Second)
93-
require.NoError(t, cmd.Process.Signal(os.Kill))
94-
})
102+
require.NoError(t, cmd.Process.Kill())
103+
}()
95104
}
96105

97106
t.Run("http_access_logger", func(t *testing.T) {
@@ -260,13 +269,12 @@ func TestIntegration(t *testing.T) {
260269
defer func() {
261270
require.NoError(t, resp.Body.Close())
262271
}()
263-
272+
body, err := io.ReadAll(resp.Body)
273+
require.NoError(t, err)
264274
if resp.StatusCode != http.StatusUnauthorized {
265275
t.Logf("unexpected status code: %d", resp.StatusCode)
266276
return false
267277
}
268-
body, err := io.ReadAll(resp.Body)
269-
require.NoError(t, err)
270278
t.Logf("response: status=%d body=%s", resp.StatusCode, string(body))
271279
require.Contains(t, string(body), "Unauthorized by Go Module")
272280
return true

0 commit comments

Comments
 (0)