Skip to content

Commit

Permalink
Improve log.testConcurrency to check errors and control total event c…
Browse files Browse the repository at this point in the history
…ount.
  • Loading branch information
ChrisHines committed Jul 15, 2016
1 parent 6aa1e99 commit 02735ee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
36 changes: 24 additions & 12 deletions log/concurrency_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package log_test

import (
"strconv"
"sync"
"math"
"testing"

"github.com/go-kit/kit/log"
)

// These test are designed to be run with the race detector.

func testConcurrency(t *testing.T, logger log.Logger) {
for _, n := range []int{10, 100, 500} {
wg := sync.WaitGroup{}
wg.Add(n)
for i := 0; i < n; i++ {
go func() { spam(logger); wg.Done() }()
func testConcurrency(t *testing.T, logger log.Logger, total int) {
n := int(math.Sqrt(float64(total)))
share := total / n

errC := make(chan error, n)

for i := 0; i < n; i++ {
go func() {
errC <- spam(logger, share)
}()
}

for i := 0; i < n; i++ {
err := <-errC
if err != nil {
t.Fatalf("concurrent logging error: %v", err)
}
wg.Wait()
}
}

func spam(logger log.Logger) {
for i := 0; i < 100; i++ {
logger.Log("key", strconv.FormatInt(int64(i), 10))
func spam(logger log.Logger, count int) error {
for i := 0; i < count; i++ {
err := logger.Log("key", i)
if err != nil {
return err
}
}
return nil
}
3 changes: 2 additions & 1 deletion log/json_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,6 @@ func BenchmarkJSONLoggerContextual(b *testing.B) {
}

func TestJSONLoggerConcurrency(t *testing.T) {
testConcurrency(t, log.NewJSONLogger(ioutil.Discard))
t.Parallel()
testConcurrency(t, log.NewJSONLogger(ioutil.Discard), 10000)
}
3 changes: 2 additions & 1 deletion log/logfmt_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func BenchmarkLogfmtLoggerContextual(b *testing.B) {
}

func TestLogfmtLoggerConcurrency(t *testing.T) {
testConcurrency(t, log.NewLogfmtLogger(ioutil.Discard))
t.Parallel()
testConcurrency(t, log.NewLogfmtLogger(ioutil.Discard), 10000)
}

type mymap map[int]int
Expand Down
7 changes: 4 additions & 3 deletions log/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,21 @@ func TestSwapLogger(t *testing.T) {
}

func TestSwapLoggerConcurrency(t *testing.T) {
testConcurrency(t, &log.SwapLogger{})
t.Parallel()
testConcurrency(t, &log.SwapLogger{}, 10000)
}

func TestSyncLoggerConcurrency(t *testing.T) {
var w io.Writer
w = &bytes.Buffer{}
logger := log.NewLogfmtLogger(w)
logger = log.NewSyncLogger(logger)
testConcurrency(t, logger)
testConcurrency(t, logger, 10000)
}

func TestSyncWriterConcurrency(t *testing.T) {
var w io.Writer
w = &bytes.Buffer{}
w = log.NewSyncWriter(w)
testConcurrency(t, log.NewLogfmtLogger(w))
testConcurrency(t, log.NewLogfmtLogger(w), 10000)
}

0 comments on commit 02735ee

Please sign in to comment.