forked from evalphobia/logrus_sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_test.go
70 lines (59 loc) · 1.42 KB
/
async_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
package logrus_sentry
import (
"net/http"
"sync"
"testing"
"time"
"github.com/sirupsen/logrus"
)
func TestParallelLogging(t *testing.T) {
WithTestDSN(t, func(dsn string, pch <-chan *resultPacket) {
logger := getTestLogger()
hook, err := NewAsyncSentryHook(dsn, []logrus.Level{
logrus.ErrorLevel,
})
if err != nil {
t.Fatal(err.Error())
}
logger.Hooks.Add(hook)
wg := &sync.WaitGroup{}
// start draining messages
var logsReceived int
const logCount = 10
go func() {
for i := 0; i < logCount; i++ {
timeoutCh := time.After(hook.Timeout * 2)
var packet *resultPacket
select {
case packet = <-pch:
case <-timeoutCh:
t.Fatalf("Waited %s without a response", hook.Timeout*2)
}
if packet.Logger != logger_name {
t.Errorf("logger should have been %s, was %s", logger_name, packet.Logger)
}
if packet.ServerName != server_name {
t.Errorf("server_name should have been %s, was %s", server_name, packet.ServerName)
}
logsReceived++
wg.Done()
}
}()
req, _ := http.NewRequest("GET", "url", nil)
log := logger.WithFields(logrus.Fields{
"server_name": server_name,
"logger": logger_name,
"http_request": req,
})
for i := 0; i < logCount; i++ {
wg.Add(1)
go func() {
log.Error(message)
}()
}
wg.Wait()
if logCount != logsReceived {
t.Errorf("Sent %d logs, received %d", logCount, logsReceived)
}
})
}