Skip to content

Commit

Permalink
Configure bugsnag at the start of testapp, rewrite net-http tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DariaKunoichi committed Aug 14, 2024
1 parent 8507abc commit 57225eb
Show file tree
Hide file tree
Showing 30 changed files with 243 additions and 461 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
maze_output
vendor

features/fixtures/testbuild

# ignore the gemfile to prevent testing against stale versions
Gemfile.lock
20 changes: 12 additions & 8 deletions features/configuration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,25 @@ Feature: Configure integration with environment variables
When I start the service "app"
And I run "AutoconfigPanicScenario"
And I wait to receive an error
And the in-project frames of the stacktrace are:
| file | method | lineNumber |
| cases.go | explicitPanic | 22 |
| main.go | main | 11 |
And the "file" of stack frame 0 equals "features/fixtures/app/autoconfig_scenario.go"
And the "method" of stack frame 0 equals "AutoconfigPanicScenario.func1"
And the "lineNumber" of stack frame 0 equals 11
And the "file" of stack frame 1 equals "features/fixtures/app/main.go"
And the "method" of stack frame 1 equals "main"
And the "lineNumber" of stack frame 1 equals 65

Scenario: Configuring source root
Given I set environment variable "BUGSNAG_SOURCE_ROOT" to "/app/src/features/fixtures/app/"
And I set environment variable "BUGSNAG_AUTO_CAPTURE_SESSIONS" to "0"
When I start the service "app"
And I run "AutoconfigPanicScenario"
And I wait to receive an error
And the in-project frames of the stacktrace are:
| file | method | lineNumber |
| cases.go | explicitPanic | 22 |
| main.go | main | 11 |
And the "file" of stack frame 0 equals "autoconfig_scenario.go"
And the "method" of stack frame 0 equals "AutoconfigPanicScenario.func1"
And the "lineNumber" of stack frame 0 equals 11
And the "file" of stack frame 1 equals "main.go"
And the "method" of stack frame 1 equals "main"
And the "lineNumber" of stack frame 1 equals 65

Scenario: Delivering events filtering through notify release stages
Given I set environment variable "BUGSNAG_NOTIFY_RELEASE_STAGES" to "prod,beta"
Expand Down
3 changes: 3 additions & 0 deletions features/fixtures/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ WORKDIR /app/src/features/fixtures/app
RUN go mod init && go mod tidy && \
echo "replace github.com/bugsnag/bugsnag-go/v2 => /app/src/github.com/bugsnag/bugsnag-go/v2" >> go.mod && \
go mod tidy

RUN chmod +x run.sh
CMD ["/app/src/features/fixtures/app/run.sh"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@ import (
bugsnag "github.com/bugsnag/bugsnag-go/v2"
)

func AutoconfigPanicScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func AutoconfigPanicScenario(command Command) func() {
scenarioFunc := func() {
panic("PANIQ!")
}
return config, scenarioFunc
return scenarioFunc
}

func AutoconfigHandledScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func AutoconfigHandledScenario(command Command) func() {
scenarioFunc := func() {
bugsnag.Notify(fmt.Errorf("gone awry!"))
}
return config, scenarioFunc
return scenarioFunc
}

func AutoconfigMetadataScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func AutoconfigMetadataScenario(command Command) func() {
scenarioFunc := func() {
bugsnag.OnBeforeNotify(func(event *bugsnag.Event, config *bugsnag.Configuration) error {
event.MetaData.Add("fruit", "Tomato", "beefsteak")
Expand All @@ -35,5 +29,5 @@ func AutoconfigMetadataScenario(command Command) (bugsnag.Configuration, func())
})
bugsnag.Notify(fmt.Errorf("gone awry!"))
}
return config, scenarioFunc
}
return scenarioFunc
}
15 changes: 0 additions & 15 deletions features/fixtures/app/configure.go

This file was deleted.

42 changes: 16 additions & 26 deletions features/fixtures/app/handled_scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/bugsnag/bugsnag-go/v2"
)

func HandledErrorScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func HandledErrorScenario(command Command) func() {
scenarioFunc := func() {
if _, err := os.Open("nonexistent_file.txt"); err != nil {
if errClass := os.Getenv("ERROR_CLASS"); errClass != "" {
Expand All @@ -21,47 +19,42 @@ func HandledErrorScenario(command Command) (bugsnag.Configuration, func()) {
}
}
}
return config, scenarioFunc
return scenarioFunc
}

func MultipleHandledErrorsScenario(command Command) (bugsnag.Configuration, func()) {
func MultipleHandledErrorsScenario(command Command) func() {
//Make the order of the below predictable
config := ConfigureBugsnag(command)
config.Synchronous = true
bugsnag.Configure(bugsnag.Configuration{Synchronous: true})

scenarioFunc := func() {
ctx := bugsnag.StartSession(context.Background())
bugsnag.Notify(fmt.Errorf("oops"), ctx)
bugsnag.Notify(fmt.Errorf("oops"), ctx)
}
return config, scenarioFunc
return scenarioFunc
}

func NestedHandledErrorScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func NestedHandledErrorScenario(command Command) func() {
scenarioFunc := func() {
if err := Login("token " + os.Getenv("API_KEY")); err != nil {
bugsnag.Notify(NewCustomErr("terminate process", err))
} else {
i := len(os.Getenv("API_KEY"))
// Some nonsense to avoid inlining checkValue
if val, err := CheckValue(i); err != nil {
fmt.Printf("err: %v, val: %d", err, val)
fmt.Printf("err: %v, val: %d\n", err, val)
}
if val, err := CheckValue(i - 46); err != nil {
fmt.Printf("err: %v, val: %d", err, val)
fmt.Printf("err: %v, val: %d\n", err, val)
}

log.Fatalf("This test is broken - no error was generated.")
}
}
return config, scenarioFunc
return scenarioFunc
}

func HandledCallbackErrorScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func HandledCallbackErrorScenario(command Command) func() {
scenarioFunc := func() {
bugsnag.Notify(fmt.Errorf("inadequent Prep Error"), func(event *bugsnag.Event) {
event.Context = "nonfatal.go:14"
Expand All @@ -71,24 +64,21 @@ func HandledCallbackErrorScenario(command Command) (bugsnag.Configuration, func(
event.Stacktrace[1].LineNumber = 0
})
}
return config, scenarioFunc
return scenarioFunc
}

func HandledToUnhandledScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func HandledToUnhandledScenario(command Command) func() {
scenarioFunc := func() {
bugsnag.Notify(fmt.Errorf("unknown event"), func(event *bugsnag.Event) {
event.Unhandled = true
event.Severity = bugsnag.SeverityError
})
}
return config, scenarioFunc
return scenarioFunc
}

func OnBeforeNotifyScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)
config.Synchronous = true
func OnBeforeNotifyScenario(command Command) func() {
bugsnag.Configure(bugsnag.Configuration{Synchronous: true})

scenarioFunc := func() {
bugsnag.OnBeforeNotify(
Expand All @@ -106,5 +96,5 @@ func OnBeforeNotifyScenario(command Command) (bugsnag.Configuration, func()) {
bugsnag.Notify(fmt.Errorf("don't ignore this error"))
bugsnag.Notify(fmt.Errorf("change error message"))
}
return config, scenarioFunc
return scenarioFunc
}
104 changes: 51 additions & 53 deletions features/fixtures/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,68 @@ package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/bugsnag/bugsnag-go/v2"
)

var scenariosMap = map[string] func(Command)(bugsnag.Configuration, func()){
"UnhandledScenario": UnhandledCrashScenario,
"HandledScenario": HandledErrorScenario,
"MultipleUnhandledScenario": MultipleUnhandledErrorsScenario,
"MultipleHandledScenario": MultipleHandledErrorsScenario,
"NestedErrorScenario": NestedHandledErrorScenario,
"MetadataScenario": MetadataScenario,
"FilteredMetadataScenario": FilteredMetadataScenario,
var scenariosMap = map[string]func(Command) func(){
"UnhandledScenario": UnhandledCrashScenario,
"HandledScenario": HandledErrorScenario,
"MultipleUnhandledScenario": MultipleUnhandledErrorsScenario,
"MultipleHandledScenario": MultipleHandledErrorsScenario,
"NestedErrorScenario": NestedHandledErrorScenario,
"MetadataScenario": MetadataScenario,
"FilteredMetadataScenario": FilteredMetadataScenario,
"HandledCallbackErrorScenario": HandledCallbackErrorScenario,
"SendSessionScenario": SendSessionScenario,
"HandledToUnhandledScenario": HandledToUnhandledScenario,
"SetUserScenario": SetUserScenario,
"RecoverAfterPanicScenario": RecoverAfterPanicScenario,
"AutonotifyPanicScenario": AutonotifyPanicScenario,
"SessionAndErrorScenario": SessionAndErrorScenario,
"OnBeforeNotifyScenario": OnBeforeNotifyScenario,
"AutoconfigPanicScenario": AutoconfigPanicScenario,
"AutoconfigHandledScenario": AutoconfigHandledScenario,
"AutoconfigMetadataScenario": AutoconfigMetadataScenario,
"SendSessionScenario": SendSessionScenario,
"HandledToUnhandledScenario": HandledToUnhandledScenario,
"SetUserScenario": SetUserScenario,
"RecoverAfterPanicScenario": RecoverAfterPanicScenario,
"AutonotifyPanicScenario": AutonotifyPanicScenario,
"SessionAndErrorScenario": SessionAndErrorScenario,
"OnBeforeNotifyScenario": OnBeforeNotifyScenario,
"AutoconfigPanicScenario": AutoconfigPanicScenario,
"AutoconfigHandledScenario": AutoconfigHandledScenario,
"AutoconfigMetadataScenario": AutoconfigMetadataScenario,
"HttpServerScenario": HttpServerScenario,
}

func main() {
// Listening to the OS Signals
signalsChan := make(chan os.Signal, 1)
signal.Notify(signalsChan, syscall.SIGINT, syscall.SIGTERM)
ticker := time.NewTicker(1 * time.Second)
addr := os.Getenv("DEFAULT_MAZE_ADDRESS")
if addr == "" {
addr = DEFAULT_MAZE_ADDRESS
}

// Increase publish rate for testing
bugsnag.DefaultSessionPublishInterval = time.Millisecond * 50
endpoints := bugsnag.Endpoints{
Notify: fmt.Sprintf("%+v/notify", addr),
Sessions: fmt.Sprintf("%+v/sessions", addr),
}
// HAS TO RUN FIRST BECAUSE OF PANIC WRAP
// https://github.com/bugsnag/panicwrap/blob/master/panicwrap.go#L177-L203
bugsnag.Configure(bugsnag.Configuration{
APIKey: "166f5ad3590596f9aa8d601ea89af845",
Endpoints: endpoints,
})
// Increase publish rate for testing
bugsnag.DefaultSessionPublishInterval = time.Millisecond * 50

addr := os.Getenv("DEFAULT_MAZE_ADDRESS")
if (addr == "") {
addr = DEFAULT_MAZE_ADDRESS
}

for {
select {
case <-ticker.C:
command := GetCommand(addr)
fmt.Printf("[Bugsnag] Received command: %+v\n", command)

if command.Action == "run-scenario" {
prepareScenarioFunc, ok := scenariosMap[command.ScenarioName]
if ok {
config, scenarioFunc := prepareScenarioFunc(command)
bugsnag.Configure(config)
time.Sleep(200 * time.Millisecond)
scenarioFunc()
time.Sleep(200 * time.Millisecond)
}
}
case <-signalsChan:
fmt.Println("[Bugsnag] Context is done, closing")
ticker.Stop()
return
// Listening to the OS Signals
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-ticker.C:
command := GetCommand(addr)
fmt.Printf("[Bugsnag] Received command: %+v\n", command)
if command.Action != "run-scenario" {
continue
}
prepareScenarioFunc, ok := scenariosMap[command.ScenarioName]
if ok {
scenarioFunc := prepareScenarioFunc(command)
scenarioFunc()
time.Sleep(200 * time.Millisecond)
}
}
}
}


12 changes: 4 additions & 8 deletions features/fixtures/app/metadata_scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"github.com/bugsnag/bugsnag-go/v2"
)

func MetadataScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func MetadataScenario(command Command) func() {
scenarioFunc := func() {
customerData := map[string]string{"Name": "Joe Bloggs", "Age": "21"}
bugsnag.Notify(fmt.Errorf("oops"), bugsnag.MetaData{
Expand All @@ -18,12 +16,10 @@ func MetadataScenario(command Command) (bugsnag.Configuration, func()) {
},
})
}
return config, scenarioFunc
return scenarioFunc
}

func FilteredMetadataScenario(command Command) (bugsnag.Configuration, func()) {
config := ConfigureBugsnag(command)

func FilteredMetadataScenario(command Command) func() {
scenarioFunc := func() {
bugsnag.Notify(fmt.Errorf("oops"), bugsnag.MetaData{
"Account": {
Expand All @@ -32,5 +28,5 @@ func FilteredMetadataScenario(command Command) (bugsnag.Configuration, func()) {
},
})
}
return config, scenarioFunc
return scenarioFunc
}
Loading

0 comments on commit 57225eb

Please sign in to comment.