From 3d13fb3eae0db62c276a13fa7a994abbab06bff7 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Tue, 15 Aug 2023 13:34:06 +0200 Subject: [PATCH] test: deduplicate Flush() timeout settings --- fasthttp/sentryfasthttp_test.go | 3 ++- gin/sentrygin_test.go | 3 ++- http/sentryhttp_test.go | 3 ++- internal/testutils/consts.go | 19 +++++++++++++++++++ logrus/logrusentry_test.go | 5 +++-- profiler_test.go | 14 +++++--------- transport_test.go | 13 ++++--------- 7 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 internal/testutils/consts.go diff --git a/fasthttp/sentryfasthttp_test.go b/fasthttp/sentryfasthttp_test.go index e77871d93..25187acb9 100644 --- a/fasthttp/sentryfasthttp_test.go +++ b/fasthttp/sentryfasthttp_test.go @@ -10,6 +10,7 @@ import ( "github.com/getsentry/sentry-go" sentryfasthttp "github.com/getsentry/sentry-go/fasthttp" + "github.com/getsentry/sentry-go/internal/testutils" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/valyala/fasthttp" @@ -194,7 +195,7 @@ func TestIntegration(t *testing.T) { } } - if ok := sentry.Flush(time.Second); !ok { + if ok := sentry.Flush(testutils.FlushTimeout()); !ok { t.Fatal("sentry.Flush timed out") } close(eventsCh) diff --git a/gin/sentrygin_test.go b/gin/sentrygin_test.go index 97d733214..82a4ed071 100644 --- a/gin/sentrygin_test.go +++ b/gin/sentrygin_test.go @@ -12,6 +12,7 @@ import ( "github.com/getsentry/sentry-go" sentrygin "github.com/getsentry/sentry-go/gin" + "github.com/getsentry/sentry-go/internal/testutils" "github.com/gin-gonic/gin" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -317,7 +318,7 @@ func TestIntegration(t *testing.T) { res.Body.Close() } - if ok := sentry.Flush(time.Second); !ok { + if ok := sentry.Flush(testutils.FlushTimeout()); !ok { t.Fatal("sentry.Flush timed out") } close(eventsCh) diff --git a/http/sentryhttp_test.go b/http/sentryhttp_test.go index dc6484f4f..45c3eb039 100644 --- a/http/sentryhttp_test.go +++ b/http/sentryhttp_test.go @@ -11,6 +11,7 @@ import ( "github.com/getsentry/sentry-go" sentryhttp "github.com/getsentry/sentry-go/http" + "github.com/getsentry/sentry-go/internal/testutils" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" ) @@ -197,7 +198,7 @@ func TestIntegration(t *testing.T) { res.Body.Close() } - if ok := sentry.Flush(time.Second); !ok { + if ok := sentry.Flush(testutils.FlushTimeout()); !ok { t.Fatal("sentry.Flush timed out") } close(eventsCh) diff --git a/internal/testutils/consts.go b/internal/testutils/consts.go new file mode 100644 index 000000000..276e23bdf --- /dev/null +++ b/internal/testutils/consts.go @@ -0,0 +1,19 @@ +package testutils + +import ( + "os" + "time" +) + +func IsCI() bool { + return os.Getenv("CI") != "" +} + +func FlushTimeout() time.Duration { + if IsCI() { + // CI is very overloaded so we need to allow for a long wait time. + return 5 * time.Second + } + + return time.Second +} diff --git a/logrus/logrusentry_test.go b/logrus/logrusentry_test.go index b206d5b85..f36250ac9 100644 --- a/logrus/logrusentry_test.go +++ b/logrus/logrusentry_test.go @@ -15,6 +15,7 @@ import ( "github.com/sirupsen/logrus" "github.com/getsentry/sentry-go" + "github.com/getsentry/sentry-go/internal/testutils" ) func TestNew(t *testing.T) { @@ -36,7 +37,7 @@ func TestNew(t *testing.T) { if id := h.hub.CaptureEvent(&sentry.Event{}); id == nil { t.Error("CaptureEvent failed") } - if !h.Flush(5 * time.Second) { + if !h.Flush(testutils.FlushTimeout()) { t.Error("flush failed") } }) @@ -59,7 +60,7 @@ func TestFire(t *testing.T) { t.Fatal(err) } - if !hook.Flush(5 * time.Second) { + if !hook.Flush(testutils.FlushTimeout()) { t.Error("flush failed") } } diff --git a/profiler_test.go b/profiler_test.go index 0287b302c..4abb5441b 100644 --- a/profiler_test.go +++ b/profiler_test.go @@ -2,7 +2,6 @@ package sentry import ( "fmt" - "os" "runtime" "strconv" "strings" @@ -10,6 +9,7 @@ import ( "testing" "time" + "github.com/getsentry/sentry-go/internal/testutils" "github.com/stretchr/testify/require" ) @@ -46,7 +46,7 @@ func TestProfilerCollection(t *testing.T) { start := time.Now() stopFn := startProfiling(start) - if isCI() { + if testutils.IsCI() { doWorkFor(5 * time.Second) } else { doWorkFor(35 * time.Millisecond) @@ -238,7 +238,7 @@ func validateProfile(t *testing.T, trace *profileTrace, duration time.Duration) } func TestProfilerSamplingRate(t *testing.T) { - if isCI() { + if testutils.IsCI() { t.Skip("Skipping on CI because the machines are too overloaded to provide consistent ticker resolution.") } if testing.Short() { @@ -303,13 +303,9 @@ func testTick(t *testing.T, count, i int, prevTick time.Time) time.Time { return time.Now() } -func isCI() bool { - return os.Getenv("CI") != "" -} - // This test measures the accuracy of time.NewTicker() on the current system. func TestProfilerTimeTicker(t *testing.T) { - if isCI() { + if testutils.IsCI() { t.Skip("Skipping on CI because the machines are too overloaded to provide consistent ticker resolution.") } @@ -426,7 +422,7 @@ func TestProfilerOverhead(t *testing.T) { if testing.Short() { t.Skip("Skipping overhead benchmark in short mode.") } - if isCI() { + if testutils.IsCI() { t.Skip("Skipping on CI because the machines are too overloaded to run the test properly - they show between 3 and 30 %% overhead....") } diff --git a/transport_test.go b/transport_test.go index de0ab9a05..fa8e8a328 100644 --- a/transport_test.go +++ b/transport_test.go @@ -14,6 +14,7 @@ import ( "testing" "time" + "github.com/getsentry/sentry-go/internal/testutils" "github.com/google/go-cmp/cmp" ) @@ -432,13 +433,7 @@ func TestHTTPTransport(t *testing.T) { transportMustFlush := func(t *testing.T, id string) { t.Helper() - - timeout := 100 * time.Millisecond - if isCI() { - // CI is very overloaded so we need to allow for a long wait time. - timeout = 5 * time.Second - } - ok := transport.Flush(timeout) + ok := transport.Flush(testutils.FlushTimeout()) if !ok { t.Fatalf("[CLIENT] {%.4s} Flush() timed out", id) } @@ -552,7 +547,7 @@ func testKeepAlive(t *testing.T, tr Transport) { checkLastConnReuse := func(reused bool) { t.Helper() reqCount++ - if !tr.Flush(2 * time.Second) { + if !tr.Flush(testutils.FlushTimeout()) { t.Fatal("Flush timed out") } if len(rt.reusedConn) != reqCount { @@ -666,7 +661,7 @@ func testRateLimiting(t *testing.T, tr Transport) { }() wg.Wait() - if !tr.Flush(time.Second) { + if !tr.Flush(testutils.FlushTimeout()) { t.Fatal("Flush timed out") }