Codebase list golang-github-go-kit-kit / 51ae681
Improve log.testConcurrency to check errors and control total event count. Chris Hines 7 years ago
4 changed file(s) with 32 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
00 package log_test
11
22 import (
3 "strconv"
4 "sync"
3 "math"
54 "testing"
65
76 "github.com/go-kit/kit/log"
98
109 // These test are designed to be run with the race detector.
1110
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() }()
11 func testConcurrency(t *testing.T, logger log.Logger, total int) {
12 n := int(math.Sqrt(float64(total)))
13 share := total / n
14
15 errC := make(chan error, n)
16
17 for i := 0; i < n; i++ {
18 go func() {
19 errC <- spam(logger, share)
20 }()
21 }
22
23 for i := 0; i < n; i++ {
24 err := <-errC
25 if err != nil {
26 t.Fatalf("concurrent logging error: %v", err)
1827 }
19 wg.Wait()
2028 }
2129 }
2230
23 func spam(logger log.Logger) {
24 for i := 0; i < 100; i++ {
25 logger.Log("key", strconv.FormatInt(int64(i), 10))
31 func spam(logger log.Logger, count int) error {
32 for i := 0; i < count; i++ {
33 err := logger.Log("key", i)
34 if err != nil {
35 return err
36 }
2637 }
38 return nil
2739 }
152152 }
153153
154154 func TestJSONLoggerConcurrency(t *testing.T) {
155 testConcurrency(t, log.NewJSONLogger(ioutil.Discard))
155 t.Parallel()
156 testConcurrency(t, log.NewJSONLogger(ioutil.Discard), 10000)
156157 }
4747 }
4848
4949 func TestLogfmtLoggerConcurrency(t *testing.T) {
50 testConcurrency(t, log.NewLogfmtLogger(ioutil.Discard))
50 t.Parallel()
51 testConcurrency(t, log.NewLogfmtLogger(ioutil.Discard), 10000)
5152 }
5253
5354 type mymap map[int]int
5151 }
5252
5353 func TestSwapLoggerConcurrency(t *testing.T) {
54 testConcurrency(t, &log.SwapLogger{})
54 t.Parallel()
55 testConcurrency(t, &log.SwapLogger{}, 10000)
5556 }
5657
5758 func TestSyncLoggerConcurrency(t *testing.T) {
5960 w = &bytes.Buffer{}
6061 logger := log.NewLogfmtLogger(w)
6162 logger = log.NewSyncLogger(logger)
62 testConcurrency(t, logger)
63 testConcurrency(t, logger, 10000)
6364 }
6465
6566 func TestSyncWriterConcurrency(t *testing.T) {
6667 var w io.Writer
6768 w = &bytes.Buffer{}
6869 w = log.NewSyncWriter(w)
69 testConcurrency(t, log.NewLogfmtLogger(w))
70 testConcurrency(t, log.NewLogfmtLogger(w), 10000)
7071 }