From a37a71b02573544589312b5aa723a250c6f7fe67 Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Wed, 20 Nov 2024 17:10:12 +0000 Subject: [PATCH] test_: sentry init, report and close --- api/geth_backend.go | 1 - internal/sentry/sentry.go | 7 ++-- internal/sentry/sentry_test.go | 62 ++++++++++++++++++++++++++++++---- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/api/geth_backend.go b/api/geth_backend.go index 954f75011..13cea36e0 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -2860,7 +2860,6 @@ func (b *GethStatusBackend) EnableSentry() error { } func (b *GethStatusBackend) DisableSentry() error { - sentry.Flush() return sentry.Close() } diff --git a/internal/sentry/sentry.go b/internal/sentry/sentry.go index 920c94752..d4ec7c7ff 100644 --- a/internal/sentry/sentry.go +++ b/internal/sentry/sentry.go @@ -21,16 +21,13 @@ func MustInit(options ...Option) { } func Close() error { + sentry.Flush(time.Second * 2) // Set DSN to empty string to disable sending events return sentry.Init(sentry.ClientOptions{ Dsn: "", }) } -func Flush() { - sentry.Flush(time.Second * 2) -} - func Recover() { err := recover() if err == nil { @@ -42,7 +39,7 @@ func Recover() { func RecoverError(err interface{}) { sentry.CurrentHub().Recover(err) - sentry.Flush(time.Second * 5) + sentry.Flush(time.Second * 2) } func defaultConfig() *sentry.ClientOptions { diff --git a/internal/sentry/sentry_test.go b/internal/sentry/sentry_test.go index 7afcd8603..0d50ad567 100644 --- a/internal/sentry/sentry_test.go +++ b/internal/sentry/sentry_test.go @@ -1,10 +1,14 @@ package sentry import ( + "errors" + "fmt" + "sync" "testing" + "github.com/brianvoe/gofakeit/v6" "github.com/getsentry/sentry-go" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestBeforeSend(t *testing.T) { @@ -29,11 +33,57 @@ func TestBeforeSend(t *testing.T) { result := beforeSend(event, nil) // Verify that the stacktrace frames are correctly trimmed - assert.NotNil(t, result) - assert.Len(t, result.Exception[0].Stacktrace.Frames, 1) - assert.Equal(t, "OtherFunction", result.Exception[0].Stacktrace.Frames[0].Function) + require.NotNil(t, result) + require.Len(t, result.Exception[0].Stacktrace.Frames, 1) + require.Equal(t, "OtherFunction", result.Exception[0].Stacktrace.Frames[0].Function) // Verify that Modules and ServerName are empty - assert.Empty(t, result.Modules) - assert.Empty(t, result.ServerName) + require.Empty(t, result.Modules) + require.Empty(t, result.ServerName) +} + +func TestSentry(t *testing.T) { + dsn := fmt.Sprintf("https://%s@sentry.example.com/%d", + gofakeit.LetterN(32), + gofakeit.Number(0, 1000), + ) + context := gofakeit.LetterN(5) + version := gofakeit.LetterN(5) + + var producedEvent *sentry.Event + interceptor := func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event { + producedEvent = event + return nil + } + + err := Init( + WithDSN(dsn), + WithContext(context, version), + func(o *sentry.ClientOptions) { + o.BeforeSend = interceptor + }, + ) + require.NoError(t, err) + + wg := sync.WaitGroup{} + wg.Add(1) + + go func() { + message := gofakeit.LetterN(5) + err := errors.New(message) + defer func() { + recoveredError := recover().(error) + require.NotNil(t, recoveredError) + require.ErrorIs(t, err, recoveredError) + wg.Done() + }() + defer Recover() + panic(err) + }() + + wg.Wait() + require.NotNil(t, producedEvent) + + err = Close() + require.NoError(t, err) }