-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for asynchronous logging (#27)
* Add Async-mode constructors * Move sending of raven packet to its own func * Set default timeout to 1s in async mode * Add async plumbing * Add async tests * Add Flush() function
- Loading branch information
1 parent
f1b8721
commit d5bbafe
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters