2023-12-02 02:13:53 +00:00
|
|
|
package util_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/CPunch/gopenfusion/util"
|
|
|
|
"github.com/matryer/is"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWaitWithTimeout(t *testing.T) {
|
|
|
|
is := is.New(t)
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
go func() {
|
2023-12-02 02:23:27 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2023-12-02 02:13:53 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
2023-12-02 02:23:27 +00:00
|
|
|
is.True(!util.WaitWithTimeout(wg, 500*time.Millisecond)) // timeout should occur
|
|
|
|
is.True(util.WaitWithTimeout(wg, 750*time.Millisecond)) // timeout shouldn't occur
|
2023-12-02 02:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSelectWithTimeout(t *testing.T) {
|
|
|
|
is := is.New(t)
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
2023-12-02 02:23:27 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2023-12-02 02:13:53 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
2023-12-02 02:23:27 +00:00
|
|
|
is.True(!util.SelectWithTimeout(ch, 500*time.Millisecond)) // timeout should occur
|
|
|
|
is.True(util.SelectWithTimeout(ch, 750*time.Millisecond)) // timeout shouldn't occur
|
2023-12-02 02:13:53 +00:00
|
|
|
}
|