test_: sentry init, report and close

This commit is contained in:
Igor Sirotin 2024-11-20 17:10:12 +00:00
parent 190671df9e
commit a37a71b025
No known key found for this signature in database
GPG Key ID: 425E227CAAB81F95
3 changed files with 58 additions and 12 deletions

View File

@ -2860,7 +2860,6 @@ func (b *GethStatusBackend) EnableSentry() error {
}
func (b *GethStatusBackend) DisableSentry() error {
sentry.Flush()
return sentry.Close()
}

View File

@ -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 {

View File

@ -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)
}