2021-12-24 17:22:24 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2022-06-10 05:01:14 +00:00
|
|
|
"sync"
|
2021-12-24 17:22:24 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReonce_DoAndReset(t *testing.T) {
|
|
|
|
var cnt = 0
|
|
|
|
var reonce Reonce
|
2022-06-10 05:01:14 +00:00
|
|
|
var wgAll, wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
wgAll.Add(2)
|
2021-12-24 17:22:24 +00:00
|
|
|
go reonce.Do(func() {
|
2022-02-15 05:55:19 +00:00
|
|
|
t.Log("once #1")
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
cnt++
|
2022-06-10 05:01:14 +00:00
|
|
|
wg.Done()
|
|
|
|
wgAll.Done()
|
2021-12-24 17:22:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// make sure it's locked
|
2022-06-10 05:01:14 +00:00
|
|
|
wg.Wait()
|
2021-12-24 17:22:24 +00:00
|
|
|
t.Logf("reset")
|
|
|
|
reonce.Reset()
|
|
|
|
|
|
|
|
go reonce.Do(func() {
|
2022-02-15 05:55:19 +00:00
|
|
|
t.Log("once #2")
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
cnt++
|
2022-06-10 05:01:14 +00:00
|
|
|
wgAll.Done()
|
2021-12-24 17:22:24 +00:00
|
|
|
})
|
|
|
|
|
2022-06-10 05:01:14 +00:00
|
|
|
wgAll.Wait()
|
2021-12-24 17:22:24 +00:00
|
|
|
assert.Equal(t, 2, cnt)
|
|
|
|
}
|