-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve log.testConcurrency to check errors and control total event c…
…ount.
- Loading branch information
1 parent
6aa1e99
commit 02735ee
Showing
4 changed files
with
32 additions
and
17 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 |
---|---|---|
@@ -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 | ||
} |
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
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
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