Codebase list golang-github-go-kit-kit / 83d4711
log: notes and tests re: concurrent access Addresses #24 Peter Bourgon 9 years ago
4 changed file(s) with 43 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
0 package log_test
1
2 import (
3 "strconv"
4 "sync"
5 "testing"
6
7 "github.com/peterbourgon/gokit/log"
8 )
9
10 // These test are designed to be run with the race detector.
11
12 func testConcurrency(t *testing.T, logger log.Logger) {
13 for _, n := range []int{10, 100, 500} {
14 wg := sync.WaitGroup{}
15 wg.Add(n)
16 for i := 0; i < n; i++ {
17 go func() { spam(logger); wg.Done() }()
18 }
19 wg.Wait()
20 }
21 }
22
23 func spam(logger log.Logger) {
24 for i := 0; i < 100; i++ {
25 logger.Log("key", strconv.FormatInt(int64(i), 10))
26 }
27 }
2626 func BenchmarkJSONLoggerContextual(b *testing.B) {
2727 benchmarkRunner(b, log.NewJSONLogger(ioutil.Discard), withMessage)
2828 }
29
30 func TestJSONLoggerConcurrency(t *testing.T) {
31 testConcurrency(t, log.NewJSONLogger(ioutil.Discard))
32 }
77 //
88 // Log creates a log event from keyvals, a variadic sequence of alternating
99 // keys and values.
10 //
11 // Logger implementations must be safe for concurrent use by multiple
12 // goroutines.
1013 type Logger interface {
1114 Log(keyvals ...interface{}) error
1215 }
3636 }
3737 }
3838
39 type mymap map[int]int
40
41 func (m mymap) String() string { return "special_behavior" }
42
4339 func BenchmarkPrefixLoggerSimple(b *testing.B) {
4440 benchmarkRunner(b, log.NewPrefixLogger(ioutil.Discard), baseMessage)
4541 }
4743 func BenchmarkPrefixLoggerContextual(b *testing.B) {
4844 benchmarkRunner(b, log.NewPrefixLogger(ioutil.Discard), withMessage)
4945 }
46
47 func TestPrefixLoggerConcurrency(t *testing.T) {
48 testConcurrency(t, log.NewPrefixLogger(ioutil.Discard))
49 }
50
51 type mymap map[int]int
52
53 func (m mymap) String() string { return "special_behavior" }