mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-02 11:59:00 +08:00
b5b5afb63f
#34680 Signed-off-by: lixinguo <xinguo.li@zilliz.com> Co-authored-by: lixinguo <xinguo.li@zilliz.com>
58 lines
995 B
Go
58 lines
995 B
Go
package syncutil
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAsyncTaskNotifier(t *testing.T) {
|
|
n := NewAsyncTaskNotifier[error]()
|
|
assert.NotNil(t, n.Context())
|
|
|
|
select {
|
|
case <-n.FinishChan():
|
|
t.Errorf("should not done")
|
|
return
|
|
case <-n.Context().Done():
|
|
t.Error("should not cancel")
|
|
return
|
|
default:
|
|
}
|
|
|
|
finishErr := errors.New("test")
|
|
|
|
ch := make(chan struct{})
|
|
go func() {
|
|
defer close(ch)
|
|
done := false
|
|
cancel := false
|
|
cancelCh := n.Context().Done()
|
|
doneCh := n.FinishChan()
|
|
for i := 0; ; i += 1 {
|
|
select {
|
|
case <-doneCh:
|
|
done = true
|
|
doneCh = nil
|
|
case <-cancelCh:
|
|
cancel = true
|
|
cancelCh = nil
|
|
n.Finish(finishErr)
|
|
}
|
|
if cancel && done {
|
|
return
|
|
}
|
|
if i == 0 {
|
|
assert.True(t, cancel && !done)
|
|
} else if i == 1 {
|
|
assert.True(t, cancel && done)
|
|
}
|
|
}
|
|
}()
|
|
n.Cancel()
|
|
n.BlockUntilFinish()
|
|
assert.ErrorIs(t, n.BlockAndGetResult(), finishErr)
|
|
<-ch
|
|
}
|