Skip to content

Commit 8b3833f

Browse files
authored
test: deduplicate Flush() timeout settings (#702)
1 parent 24d7c09 commit 8b3833f

File tree

7 files changed

+37
-23
lines changed

7 files changed

+37
-23
lines changed

fasthttp/sentryfasthttp_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/getsentry/sentry-go"
1212
sentryfasthttp "github.com/getsentry/sentry-go/fasthttp"
13+
"github.com/getsentry/sentry-go/internal/testutils"
1314
"github.com/google/go-cmp/cmp"
1415
"github.com/google/go-cmp/cmp/cmpopts"
1516
"github.com/valyala/fasthttp"
@@ -194,7 +195,7 @@ func TestIntegration(t *testing.T) {
194195
}
195196
}
196197

197-
if ok := sentry.Flush(time.Second); !ok {
198+
if ok := sentry.Flush(testutils.FlushTimeout()); !ok {
198199
t.Fatal("sentry.Flush timed out")
199200
}
200201
close(eventsCh)

gin/sentrygin_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/getsentry/sentry-go"
1414
sentrygin "github.com/getsentry/sentry-go/gin"
15+
"github.com/getsentry/sentry-go/internal/testutils"
1516
"github.com/gin-gonic/gin"
1617
"github.com/google/go-cmp/cmp"
1718
"github.com/google/go-cmp/cmp/cmpopts"
@@ -317,7 +318,7 @@ func TestIntegration(t *testing.T) {
317318
res.Body.Close()
318319
}
319320

320-
if ok := sentry.Flush(time.Second); !ok {
321+
if ok := sentry.Flush(testutils.FlushTimeout()); !ok {
321322
t.Fatal("sentry.Flush timed out")
322323
}
323324
close(eventsCh)

http/sentryhttp_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/getsentry/sentry-go"
1313
sentryhttp "github.com/getsentry/sentry-go/http"
14+
"github.com/getsentry/sentry-go/internal/testutils"
1415
"github.com/google/go-cmp/cmp"
1516
"github.com/google/go-cmp/cmp/cmpopts"
1617
)
@@ -197,7 +198,7 @@ func TestIntegration(t *testing.T) {
197198
res.Body.Close()
198199
}
199200

200-
if ok := sentry.Flush(time.Second); !ok {
201+
if ok := sentry.Flush(testutils.FlushTimeout()); !ok {
201202
t.Fatal("sentry.Flush timed out")
202203
}
203204
close(eventsCh)

internal/testutils/consts.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package testutils
2+
3+
import (
4+
"os"
5+
"time"
6+
)
7+
8+
func IsCI() bool {
9+
return os.Getenv("CI") != ""
10+
}
11+
12+
func FlushTimeout() time.Duration {
13+
if IsCI() {
14+
// CI is very overloaded so we need to allow for a long wait time.
15+
return 5 * time.Second
16+
}
17+
18+
return time.Second
19+
}

logrus/logrusentry_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/sirupsen/logrus"
1616

1717
"github.com/getsentry/sentry-go"
18+
"github.com/getsentry/sentry-go/internal/testutils"
1819
)
1920

2021
func TestNew(t *testing.T) {
@@ -36,7 +37,7 @@ func TestNew(t *testing.T) {
3637
if id := h.hub.CaptureEvent(&sentry.Event{}); id == nil {
3738
t.Error("CaptureEvent failed")
3839
}
39-
if !h.Flush(5 * time.Second) {
40+
if !h.Flush(testutils.FlushTimeout()) {
4041
t.Error("flush failed")
4142
}
4243
})
@@ -59,7 +60,7 @@ func TestFire(t *testing.T) {
5960
t.Fatal(err)
6061
}
6162

62-
if !hook.Flush(5 * time.Second) {
63+
if !hook.Flush(testutils.FlushTimeout()) {
6364
t.Error("flush failed")
6465
}
6566
}

profiler_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package sentry
22

33
import (
44
"fmt"
5-
"os"
65
"runtime"
76
"strconv"
87
"strings"
98
"sync/atomic"
109
"testing"
1110
"time"
1211

12+
"github.com/getsentry/sentry-go/internal/testutils"
1313
"github.com/stretchr/testify/require"
1414
)
1515

@@ -46,7 +46,7 @@ func TestProfilerCollection(t *testing.T) {
4646

4747
start := time.Now()
4848
stopFn := startProfiling(start)
49-
if isCI() {
49+
if testutils.IsCI() {
5050
doWorkFor(5 * time.Second)
5151
} else {
5252
doWorkFor(35 * time.Millisecond)
@@ -238,7 +238,7 @@ func validateProfile(t *testing.T, trace *profileTrace, duration time.Duration)
238238
}
239239

240240
func TestProfilerSamplingRate(t *testing.T) {
241-
if isCI() {
241+
if testutils.IsCI() {
242242
t.Skip("Skipping on CI because the machines are too overloaded to provide consistent ticker resolution.")
243243
}
244244
if testing.Short() {
@@ -303,13 +303,9 @@ func testTick(t *testing.T, count, i int, prevTick time.Time) time.Time {
303303
return time.Now()
304304
}
305305

306-
func isCI() bool {
307-
return os.Getenv("CI") != ""
308-
}
309-
310306
// This test measures the accuracy of time.NewTicker() on the current system.
311307
func TestProfilerTimeTicker(t *testing.T) {
312-
if isCI() {
308+
if testutils.IsCI() {
313309
t.Skip("Skipping on CI because the machines are too overloaded to provide consistent ticker resolution.")
314310
}
315311

@@ -426,7 +422,7 @@ func TestProfilerOverhead(t *testing.T) {
426422
if testing.Short() {
427423
t.Skip("Skipping overhead benchmark in short mode.")
428424
}
429-
if isCI() {
425+
if testutils.IsCI() {
430426
t.Skip("Skipping on CI because the machines are too overloaded to run the test properly - they show between 3 and 30 %% overhead....")
431427
}
432428

transport_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515
"time"
1616

17+
"github.com/getsentry/sentry-go/internal/testutils"
1718
"github.com/google/go-cmp/cmp"
1819
)
1920

@@ -432,13 +433,7 @@ func TestHTTPTransport(t *testing.T) {
432433

433434
transportMustFlush := func(t *testing.T, id string) {
434435
t.Helper()
435-
436-
timeout := 100 * time.Millisecond
437-
if isCI() {
438-
// CI is very overloaded so we need to allow for a long wait time.
439-
timeout = 5 * time.Second
440-
}
441-
ok := transport.Flush(timeout)
436+
ok := transport.Flush(testutils.FlushTimeout())
442437
if !ok {
443438
t.Fatalf("[CLIENT] {%.4s} Flush() timed out", id)
444439
}
@@ -552,7 +547,7 @@ func testKeepAlive(t *testing.T, tr Transport) {
552547
checkLastConnReuse := func(reused bool) {
553548
t.Helper()
554549
reqCount++
555-
if !tr.Flush(2 * time.Second) {
550+
if !tr.Flush(testutils.FlushTimeout()) {
556551
t.Fatal("Flush timed out")
557552
}
558553
if len(rt.reusedConn) != reqCount {
@@ -666,7 +661,7 @@ func testRateLimiting(t *testing.T, tr Transport) {
666661
}()
667662
wg.Wait()
668663

669-
if !tr.Flush(time.Second) {
664+
if !tr.Flush(testutils.FlushTimeout()) {
670665
t.Fatal("Flush timed out")
671666
}
672667

0 commit comments

Comments
 (0)