mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-02 20:09:57 +08:00
465fd474de
issue: #30647 - ContextCond is a broadcast-only condition variable which can be canceled by context. - VersionedNotifier is a version-based notifier-listener implementation, which promise no change can be ignored. Signed-off-by: chyezh <chyezh@outlook.com>
40 lines
730 B
Go
40 lines
730 B
Go
package syncutil
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestContextCond(t *testing.T) {
|
|
cv := NewContextCond(&sync.Mutex{})
|
|
cv.L.Lock()
|
|
go func() {
|
|
time.Sleep(1 * time.Second)
|
|
cv.LockAndBroadcast()
|
|
cv.L.Unlock()
|
|
}()
|
|
// Acquire lock before wait.
|
|
assert.NoError(t, cv.Wait(context.Background()))
|
|
cv.L.Unlock()
|
|
|
|
cv.L.Lock()
|
|
go func() {
|
|
time.Sleep(1 * time.Second)
|
|
cv.LockAndBroadcast()
|
|
cv.L.Unlock()
|
|
}()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
|
defer cancel()
|
|
|
|
// Acquire no lock if wait returns error.
|
|
assert.Error(t, cv.Wait(ctx))
|
|
|
|
cv.L.Lock()
|
|
assert.NoError(t, cv.Wait(context.Background()))
|
|
cv.L.Unlock()
|
|
}
|