-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
113 lines (98 loc) · 2.1 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package bt
import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"testing"
)
func setupServer() {
var err error
addr := net.TCPAddr{
IP: []byte{127, 0, 0, 1},
}
listener, err := net.ListenTCP("tcp4", &addr)
if err != nil {
panic(err)
}
port := listener.Addr().(*net.TCPAddr).Port
Options.Endpoint = fmt.Sprintf("http://127.0.0.1:%d", port)
Options.Token = "fake token"
Options.CaptureAllGoroutines = true
//Options.DebugBacktrace = true
Options.ContextLineCount = 2
go func() {
handler := myHandler{
listener: listener,
}
err = http.Serve(listener, handler)
}()
}
func TestMain(m *testing.M) {
setupServer()
os.Exit(m.Run())
}
func TestEverything(t *testing.T) {
causeErrorReport()
}
func TestPanic(t *testing.T) {
count := 0
for i := 0; i < 5; i++ {
func() {
defer func() {
_ = recover()
count++
}()
func() {
defer ReportPanic(nil)
// fire off a panic. this should happen 5 times
panic("it broke")
}()
}()
}
if count != 5 {
// really this doesn't do much, since it won't be hit if the code above deadlocks
t.Fatal("Expected 5 panics")
}
}
type myHandler struct {
listener *net.TCPListener
}
func (h myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer h.listener.Close()
var err error
body, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
report := map[string]interface{}{}
err = json.Unmarshal(body, &report)
if err != nil {
panic(err)
}
if report["lang"] != "go" {
panic("bad lang")
}
attributes := report["attributes"].(map[string]interface{})
if attributes["error.message"] != "it broke" {
panic("bad error message")
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK\n")
}
func doSomething(ch chan int) {
<-ch
}
func causeErrorReport() {
go doSomething(make(chan int))
Report(errors.New("it broke"), nil)
finishSendingReports(false)
for _, v := range []string{"backtrace.version", "backtrace.agent", "hostname", "uname.sysname", "cpu.arch", "process.id", "application.session", "application"} {
if _, ok := Options.Attributes[v]; !ok {
panic(v + " - attribute not set")
}
}
}