counter: fix mutex copy issue

This commit is contained in:
vinchent 2024-09-21 19:47:27 +02:00
parent 572c8033f6
commit 6ded953ec6

View File

@ -5,7 +5,9 @@ import (
"testing" "testing"
) )
func assertCounter(t testing.TB, got Counter, want int) { // assertCounter
// XXX: a copy of Counter is created, but we shouldn't copy a mutex. Use pointer instead.
func assertCounter(t testing.TB, got *Counter, want int) {
t.Helper() t.Helper()
if got.Value() != want { if got.Value() != want {
t.Errorf("got %d, want %d", got.Value(), want) t.Errorf("got %d, want %d", got.Value(), want)
@ -20,7 +22,7 @@ func TestCounter(t *testing.T) {
counter.Inc() counter.Inc()
counter.Inc() counter.Inc()
assertCounter(t, counter, 3) assertCounter(t, &counter, 3)
}) })
t.Run("it runs safely concurrently", func(t *testing.T) { t.Run("it runs safely concurrently", func(t *testing.T) {
@ -37,6 +39,6 @@ func TestCounter(t *testing.T) {
}() }()
} }
wg.Wait() wg.Wait()
assertCounter(t, counter, wantedCount) assertCounter(t, &counter, wantedCount)
}) })
} }