sync: add tests

This commit is contained in:
Nia Waldvogel
2021-12-18 14:13:01 -05:00
committed by Ron Evans
parent 8f2082df69
commit 38305399a3
5 changed files with 386 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
package sync_test
import (
"sync"
"testing"
)
// TestOnceUncontended tests Once on a single goroutine.
func TestOnceUncontended(t *testing.T) {
var once sync.Once
{
var ran bool
once.Do(func() {
ran = true
})
if !ran {
t.Error("first call to Do did not run")
}
}
{
var ran bool
once.Do(func() {
ran = true
})
if ran {
t.Error("second call to Do ran")
}
}
}
// TestOnceConcurrent tests multiple concurrent invocations of sync.Once.
func TestOnceConcurrent(t *testing.T) {
var once sync.Once
var mu sync.Mutex
mu.Lock()
var ran bool
var ranTwice bool
once.Do(func() {
ran = true
// Start a goroutine and (approximately) wait for it to enter the call to Do.
var startWait sync.Mutex
startWait.Lock()
go func() {
startWait.Unlock()
once.Do(func() {
ranTwice = true
})
mu.Unlock()
}()
startWait.Lock()
})
if !ran {
t.Error("first call to Do did not run")
}
// Wait for the goroutine to finish.
mu.Lock()
if ranTwice {
t.Error("second concurrent call to Once also ran")
}
}