Skip to content

Commit e518a28

Browse files
committedApr 6, 2016
Update assertCanBeReused to check using multiple encoders/goroutines
1 parent 7720503 commit e518a28

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed
 

‎field_test.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package zap
2222

2323
import (
2424
"errors"
25+
"sync"
2526
"testing"
2627
"time"
2728

@@ -48,11 +49,24 @@ func assertFieldJSON(t testing.TB, expected string, field Field) {
4849
}
4950

5051
func assertCanBeReused(t testing.TB, field Field) {
51-
enc := newJSONEncoder()
52-
defer enc.Free()
52+
var wg sync.WaitGroup
53+
54+
for i := 0; i < 100; i++ {
55+
enc := newJSONEncoder()
56+
defer enc.Free()
57+
58+
// Ensure using the field in multiple encoders in separate goroutines
59+
// does not cause any races or panics.
60+
wg.Add(1)
61+
go func() {
62+
defer wg.Done()
63+
assert.NotPanics(t, func() {
64+
field.addTo(enc)
65+
}, "Reusing a field should not cause issues")
66+
}()
67+
}
5368

54-
field.addTo(enc)
55-
assert.NotPanics(t, func() { field.addTo(enc) }, "Expected reusing field to panic.")
69+
wg.Wait()
5670
}
5771

5872
func TestBoolField(t *testing.T) {

0 commit comments

Comments
 (0)
Please sign in to comment.