代码拉取完成,页面将自动刷新
package async_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/ghosind/go-assert"
"github.com/ghosind/go-async"
)
func TestUntil(t *testing.T) {
a := assert.New(t)
count := 0
out, err := async.Until(func(c int) bool {
return c < 5
}, func() int {
count++
return count
})
a.NilNow(err)
a.EqualNow(out, []any{5})
}
func TestUntilInvalidParameters(t *testing.T) {
a := assert.New(t)
a.PanicOfNow(func() {
async.Until(nil, func() {})
}, async.ErrNotFunction)
a.PanicOfNow(func() {
async.Until(func() {}, nil)
}, async.ErrNotFunction)
a.PanicOfNow(func() {
async.Until(1, "hello")
}, async.ErrNotFunction)
a.PanicOfNow(func() {
async.Until(func() {}, func() {})
}, async.ErrInvalidTestFunc)
a.NotPanicNow(func() {
async.Until(func() bool { return false }, func() {})
})
a.NotPanicNow(func() {
async.Until(func(err error) bool { return false }, func() error { return nil })
})
a.NotPanicNow(func() {
async.Until(func(ctx context.Context, err error) bool { return false }, func() error { return nil })
})
a.NotPanicNow(func() {
async.Until(func(ctx context.Context) bool { return false }, func() error { return nil })
})
a.PanicOfNow(func() {
async.Until(func(ctx context.Context, i int) bool { return false }, func() error { return nil })
}, async.ErrInvalidTestFunc)
a.PanicOfNow(func() {
async.Until(func(ctx context.Context, i int) bool { return false }, func() {})
}, async.ErrInvalidTestFunc)
}
func TestUntilWithFunctionError(t *testing.T) {
a := assert.New(t)
count := 0
unexpectedErr := errors.New("unexpected error")
out, err := async.Until(func(c int, err error) bool {
return c < 5
}, func() (int, error) {
count++
return count, unexpectedErr
})
a.NilNow(err)
a.EqualNow(out, []any{5, unexpectedErr})
}
func TestUntilWithTestFunctionError(t *testing.T) {
a := assert.New(t)
expectedErr := errors.New("expected error")
out, err := async.Until(func(n int) bool {
panic(expectedErr)
}, func() int {
return 0
})
a.NotNilNow(err)
a.IsErrorNow(err, expectedErr)
a.EqualNow(out, []any{0})
}
func TestUntilWithContext(t *testing.T) {
a := assert.New(t)
start := time.Now()
ctx, canFunc := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer canFunc()
out, err := async.UntilWithContext(ctx, func(ctx context.Context) bool {
select {
case <-ctx.Done():
return false
default:
return true
}
}, func() {
})
a.NilNow(err)
a.EqualNow(out, []any{})
dur := time.Since(start)
a.GteNow(dur, 100*time.Millisecond)
a.LteNow(dur, 150*time.Millisecond)
}
func ExampleUntil() {
i := 0
out, err := async.Until(func(n int) bool {
return n < 3
}, func() int {
i++
return i
})
fmt.Println(i)
fmt.Println(out)
fmt.Println(err)
// Output:
// 3
// [3]
// <nil>
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。