-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintegration_test.go
78 lines (68 loc) · 1.74 KB
/
integration_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
package periskop
import (
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"testing"
)
func errFunc() error {
var dat map[string]interface{}
// will return "unexpected end of JSON input"
return json.Unmarshal([]byte(`{"id":`), &dat)
}
func parseJSON(exportedErrors string) payload {
p := payload{}
json.Unmarshal([]byte(exportedErrors), &p)
return p
}
func TestHandler(t *testing.T) {
body := "some body"
c := NewErrorCollector()
c.ReportError(errFunc())
c.ReportWithHTTPContext(errFunc(), &HTTPContext{
RequestMethod: "GET",
RequestURL: "http://example.com",
RequestHeaders: map[string]string{"Cache-Control": "no-cache"},
RequestBody: &body,
})
e := NewErrorExporter(&c)
h := NewHandler(e)
req, err := http.NewRequest("GET", "/-/exceptions", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
p := parseJSON(rr.Body.String())
if p.AggregatedErrors[0].TotalCount != 2 {
t.Errorf("wrong number of exceptions collected: %s", rr.Body.String())
}
}
func TestConcurrency(t *testing.T) {
const maxGoRoutines = 10
const maxIterations = 20
c := NewErrorCollector()
e := NewErrorExporter(&c)
var wg sync.WaitGroup
wg.Add(maxGoRoutines)
for i := 0; i < maxGoRoutines; i++ {
go func() {
defer wg.Done()
for i := 0; i < maxIterations; i++ {
c.ReportError(errFunc())
}
}()
e.Export()
}
wg.Wait()
s, _ := e.Export()
p := parseJSON(s)
if p.AggregatedErrors[0].TotalCount != maxGoRoutines*maxIterations {
t.Errorf("num total errors expected %d, got %d", maxGoRoutines*maxIterations,
p.AggregatedErrors[0].TotalCount)
}
}