From cab5bd829492179b28e21eeea7ae12d593d99f1d Mon Sep 17 00:00:00 2001 From: Guy Arbitman Date: Wed, 27 Nov 2024 16:50:58 +0100 Subject: [PATCH] service discovery: tests: test binary lives until terminated --- .../servicediscovery/apm/detect_nix_test.go | 1 + .../apm/testutil/instrumented/instrumented.go | 24 +++++++++++++++++-- .../testutil/instrumented2/instrumented2.go | 22 +++++++++++++++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/pkg/collector/corechecks/servicediscovery/apm/detect_nix_test.go b/pkg/collector/corechecks/servicediscovery/apm/detect_nix_test.go index 2eba2551928389..898e1dcaa9c306 100644 --- a/pkg/collector/corechecks/servicediscovery/apm/detect_nix_test.go +++ b/pkg/collector/corechecks/servicediscovery/apm/detect_nix_test.go @@ -307,6 +307,7 @@ func TestGoDetector(t *testing.T) { require.NoError(t, cmd.Start()) t.Cleanup(func() { _ = cmd.Process.Kill() + cmd.Wait() }) require.Eventually(t, func() bool { if cmd.Process.Pid == 0 { diff --git a/pkg/collector/corechecks/servicediscovery/apm/testutil/instrumented/instrumented.go b/pkg/collector/corechecks/servicediscovery/apm/testutil/instrumented/instrumented.go index 74b92599e66cbc..fbfb58b7abb5b2 100644 --- a/pkg/collector/corechecks/servicediscovery/apm/testutil/instrumented/instrumented.go +++ b/pkg/collector/corechecks/servicediscovery/apm/testutil/instrumented/instrumented.go @@ -8,12 +8,32 @@ package main import ( - "time" + "os" + "os/signal" + "syscall" "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" ) func main() { + // Start the tracer tracer.Start() - time.Sleep(time.Second * 20) + + // Create a channel to listen for OS signals + signalChan := make(chan os.Signal, 1) + done := make(chan bool, 1) + + // Notify on SIGINT and SIGTERM + signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT) + + go func() { + <-signalChan // Wait for a termination signal + done <- true + }() + + // Print a message and wait for a signal + println("Running... Press Ctrl+C to exit.") + + <-done // Block until a signal is received + println("Gracefully shutting down.") } diff --git a/pkg/collector/corechecks/servicediscovery/apm/testutil/instrumented2/instrumented2.go b/pkg/collector/corechecks/servicediscovery/apm/testutil/instrumented2/instrumented2.go index b52588d9684a84..ffb835858dc6f5 100644 --- a/pkg/collector/corechecks/servicediscovery/apm/testutil/instrumented2/instrumented2.go +++ b/pkg/collector/corechecks/servicediscovery/apm/testutil/instrumented2/instrumented2.go @@ -9,7 +9,9 @@ package main import ( "fmt" - "time" + "os" + "os/signal" + "syscall" "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer" ) @@ -20,5 +22,21 @@ func main() { fmt.Println(err) } - time.Sleep(time.Second * 20) + // Create a channel to listen for OS signals + signalChan := make(chan os.Signal, 1) + done := make(chan bool, 1) + + // Notify on SIGINT and SIGTERM + signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT) + + go func() { + <-signalChan // Wait for a termination signal + done <- true + }() + + // Print a message and wait for a signal + println("Running... Press Ctrl+C to exit.") + + <-done // Block until a signal is received + println("Gracefully shutting down.") }