mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12:28 +08:00
test: add unit test for promise Race and Any function
This commit is contained in:
@@ -237,7 +237,7 @@ func Race[T any](promises []*Promise[T]) *Promise[T] {
|
|||||||
|
|
||||||
// Any resolves as soon as any of the input's Promises resolve, with the value of the resolved Promise.
|
// Any resolves as soon as any of the input's Promises resolve, with the value of the resolved Promise.
|
||||||
// Any rejects if all of the given Promises are rejected with a combination of all errors.
|
// Any rejects if all of the given Promises are rejected with a combination of all errors.
|
||||||
func Any[T any](promises ...*Promise[T]) *Promise[T] {
|
func Any[T any](promises []*Promise[T]) *Promise[T] {
|
||||||
if len(promises) == 0 {
|
if len(promises) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package async
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
@@ -117,7 +118,7 @@ func TestPromise_Catch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAll(t *testing.T) {
|
func TestAll(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestPromise_Catch")
|
assert := internal.NewAssert(t, "TestPromise_All")
|
||||||
|
|
||||||
t.Run("AllPromisesFullfilled", func(_ *testing.T) {
|
t.Run("AllPromisesFullfilled", func(_ *testing.T) {
|
||||||
p1 := New(func(resolve func(string), reject func(error)) {
|
p1 := New(func(resolve func(string), reject func(error)) {
|
||||||
@@ -159,7 +160,7 @@ func TestAll(t *testing.T) {
|
|||||||
_, err := p.Await()
|
_, err := p.Await()
|
||||||
|
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
// assert.Equal("error1", err.Error())
|
assert.Equal("error1", err.Error())
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("PromisesOnlyRejected", func(_ *testing.T) {
|
t.Run("PromisesOnlyRejected", func(_ *testing.T) {
|
||||||
@@ -183,3 +184,119 @@ func TestAll(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAny(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPromise_Any")
|
||||||
|
|
||||||
|
t.Run("AnyFullfilled", func(_ *testing.T) {
|
||||||
|
p1 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
time.Sleep(time.Millisecond * 250)
|
||||||
|
resolve("fast")
|
||||||
|
})
|
||||||
|
p2 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
time.Sleep(time.Millisecond * 500)
|
||||||
|
resolve("slow")
|
||||||
|
})
|
||||||
|
p3 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
reject(errors.New("error"))
|
||||||
|
})
|
||||||
|
|
||||||
|
p := Any([]*Promise[string]{p1, p2, p3})
|
||||||
|
|
||||||
|
val, err := p.Await()
|
||||||
|
assert.Equal("fast", val)
|
||||||
|
assert.IsNil(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("EmptyPromises", func(_ *testing.T) {
|
||||||
|
var empty = []*Promise[any]{}
|
||||||
|
p := Any(empty)
|
||||||
|
assert.IsNil(p)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("OnlyRejected", func(_ *testing.T) {
|
||||||
|
p1 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
reject(errors.New("error1"))
|
||||||
|
})
|
||||||
|
p2 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
reject(errors.New("error2"))
|
||||||
|
})
|
||||||
|
|
||||||
|
p := Any([]*Promise[string]{p1, p2})
|
||||||
|
|
||||||
|
_, err := p.Await()
|
||||||
|
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
// assert.Equal("error1", err.Error())
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRace(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPromise_Race")
|
||||||
|
|
||||||
|
t.Run("PromisesFullfilled", func(_ *testing.T) {
|
||||||
|
p1 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
resolve("a")
|
||||||
|
})
|
||||||
|
p2 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
time.Sleep(time.Millisecond * 300)
|
||||||
|
resolve("b")
|
||||||
|
})
|
||||||
|
|
||||||
|
p := Race([]*Promise[string]{p1, p2})
|
||||||
|
|
||||||
|
val, err := p.Await()
|
||||||
|
assert.Equal("a", val)
|
||||||
|
assert.IsNil(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("EmptyPromises", func(_ *testing.T) {
|
||||||
|
var empty = []*Promise[any]{}
|
||||||
|
p := Race(empty)
|
||||||
|
assert.IsNil(p)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("PromisesContainRejected", func(_ *testing.T) {
|
||||||
|
p1 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
resolve("a")
|
||||||
|
})
|
||||||
|
p2 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
reject(errors.New("error1"))
|
||||||
|
})
|
||||||
|
p3 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
reject(errors.New("error2"))
|
||||||
|
})
|
||||||
|
|
||||||
|
p := Race([]*Promise[string]{p1, p2, p3})
|
||||||
|
|
||||||
|
val, err := p.Await()
|
||||||
|
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
// assert.Equal("error1", err.Error())
|
||||||
|
assert.Equal("", val)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("PromisesOnlyRejected", func(_ *testing.T) {
|
||||||
|
p1 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
reject(errors.New("error1"))
|
||||||
|
|
||||||
|
})
|
||||||
|
p2 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
reject(errors.New("error2"))
|
||||||
|
})
|
||||||
|
p3 := New(func(resolve func(string), reject func(error)) {
|
||||||
|
reject(errors.New("error3"))
|
||||||
|
})
|
||||||
|
|
||||||
|
p := Race([]*Promise[string]{p1, p2, p3})
|
||||||
|
|
||||||
|
_, err := p.Await()
|
||||||
|
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
// assert.Equal("error1", err.Error())
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user