From 02735ee64db14784f7ca1a740461e65db502559b Mon Sep 17 00:00:00 2001 From: Chris Hines Date: Sun, 22 May 2016 22:02:25 -0400 Subject: [PATCH] Improve log.testConcurrency to check errors and control total event count. --- log/concurrency_test.go | 36 ++++++++++++++++++++++++------------ log/json_logger_test.go | 3 ++- log/logfmt_logger_test.go | 3 ++- log/sync_test.go | 7 ++++--- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/log/concurrency_test.go b/log/concurrency_test.go index e68d16a3e..95a749e77 100644 --- a/log/concurrency_test.go +++ b/log/concurrency_test.go @@ -1,8 +1,7 @@ package log_test import ( - "strconv" - "sync" + "math" "testing" "github.com/go-kit/kit/log" @@ -10,19 +9,32 @@ import ( // 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 } diff --git a/log/json_logger_test.go b/log/json_logger_test.go index 78697b6cf..42df70c1c 100644 --- a/log/json_logger_test.go +++ b/log/json_logger_test.go @@ -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) } diff --git a/log/logfmt_logger_test.go b/log/logfmt_logger_test.go index 9e7361ff3..91bbca15c 100644 --- a/log/logfmt_logger_test.go +++ b/log/logfmt_logger_test.go @@ -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 diff --git a/log/sync_test.go b/log/sync_test.go index 07960c9b6..3d915ec7b 100644 --- a/log/sync_test.go +++ b/log/sync_test.go @@ -52,7 +52,8 @@ 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) { @@ -60,12 +61,12 @@ func TestSyncLoggerConcurrency(t *testing.T) { 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) }