From f3ee19d0774fc965fdbf10f8dd64380d9bb51659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geyslan=20Greg=C3=B3rio?= Date: Mon, 30 Oct 2023 18:12:40 -0300 Subject: [PATCH] fix(tests): integration timeout handling Updated the Tracee integration tests to address a bug with the time.After usage. Previously, at each iteration of the loop, the time.After was reset whenever its corresponding select clause was reached. This behavior resulted in the creation of a new timer on each loop iteration without freeing the previous one, leading to a memory leak. The solution involved replacing time.After with a dedicated timeoutTicker, ensuring consistent timeout handling without unnecessary resource consumption. --- tests/integration/tracee.go | 53 ++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/tests/integration/tracee.go b/tests/integration/tracee.go index 3743bddff473..e7bb4fe8b8db 100644 --- a/tests/integration/tracee.go +++ b/tests/integration/tracee.go @@ -125,7 +125,6 @@ func startTracee(ctx context.Context, t *testing.T, cfg config.Config, output *c err = trc.Init(ctx) require.NoError(t, err) - t.Logf("started tracee...\n") go func() { err := trc.Run(ctx) require.NoError(t, err, "tracee run failed") @@ -149,17 +148,22 @@ func prepareCapture() *config.CaptureConfig { // wait for tracee to start (or timeout) // in case of timeout, the test will fail func waitForTraceeStart(t *testing.T, trc *tracee.Tracee) { - const checkTimeout = 10 * time.Second - ticker := time.NewTicker(100 * time.Millisecond) + const timeout = 10 * time.Second + + statusCheckTicker := time.NewTicker(100 * time.Millisecond) + defer statusCheckTicker.Stop() + timeoutTicker := time.NewTicker(timeout) + defer timeoutTicker.Stop() for { select { - case <-ticker.C: + case <-statusCheckTicker.C: if trc.Running() { + t.Logf(">>> started tracee ...") return } - case <-time.After(checkTimeout): - t.Logf("timed out on running tracee\n") + case <-timeoutTicker.C: + t.Logf("timed out on waiting for tracee to start") t.FailNow() } } @@ -168,18 +172,22 @@ func waitForTraceeStart(t *testing.T, trc *tracee.Tracee) { // wait for tracee to stop (or timeout) // in case of timeout, the test will continue since all tests already passed func waitForTraceeStop(t *testing.T, trc *tracee.Tracee) { - const checkTimeout = 10 * time.Second - ticker := time.NewTicker(100 * time.Millisecond) + const timeout = 10 * time.Second + + statusCheckTicker := time.NewTicker(100 * time.Millisecond) + defer statusCheckTicker.Stop() + timeoutTicker := time.NewTicker(timeout) + defer timeoutTicker.Stop() for { select { - case <-ticker.C: + case <-statusCheckTicker.C: if !trc.Running() { - t.Logf("stopped tracee\n") + t.Logf("<<< stopped tracee") return } - case <-time.After(checkTimeout): - t.Logf("timed out on stopping tracee\n") + case <-timeoutTicker.C: + t.Logf("timed out on stopping tracee") return } } @@ -188,18 +196,27 @@ func waitForTraceeStop(t *testing.T, trc *tracee.Tracee) { // wait for tracee buffer to fill up with expected number of events (or timeout) // in case of timeout, the test will fail func waitForTraceeOutputEvents(t *testing.T, actual *eventBuffer, now time.Time, expectedEvts int, failOnTimeout bool) { - const checkTimeout = 5 * time.Second - ticker := time.NewTicker(100 * time.Millisecond) + const timeout = 5 * time.Second + + statusCheckTicker := time.NewTicker(100 * time.Millisecond) + defer statusCheckTicker.Stop() + timeoutTicker := time.NewTicker(timeout) + defer timeoutTicker.Stop() + + t.Logf("waiting for %d event(s) for %s", expectedEvts, timeout.String()) + defer t.Logf("done waiting for %d event(s)", expectedEvts) for { select { - case <-ticker.C: - if actual.len() >= expectedEvts { + case <-statusCheckTicker.C: + len := actual.len() + t.Logf("got %d event(s) so far", len) + if len >= expectedEvts { return } - case <-time.After(checkTimeout): + case <-timeoutTicker.C: if failOnTimeout { - t.Logf("timed out on output\n") + t.Logf("timed out on waiting for %d event(s)", expectedEvts) t.FailNow() } return