mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [promise] buffer the settle channels of All, Race and Any (#428)
* fix(package): [promise] buffer the settle channels of All, Race and Any * test(package): [promise] check that All, Race and Any leave no goroutines
This commit is contained in:
1 parent
e393f0e14f
commit
285e69edd7
2 files changed
+43
-4
No files matched your search
@@ -2,6 +2,7 @@ package promise
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -310,3 +311,39 @@ func TestRace(t *testing.T) {
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestRaceAnyAllDoNotLeakGoroutines(t *testing.T) {
|
||||
// not parallel: it counts the goroutines of the whole test binary
|
||||
assert := internal.NewAssert(t, "TestRaceAnyAllDoNotLeakGoroutines")
|
||||
|
||||
newResolved := func(n int) []*Promise[int] {
|
||||
ps := make([]*Promise[int], 0, 3)
|
||||
for i := 0; i < 3; i++ {
|
||||
ps = append(ps, New(func(resolve func(int), reject func(error)) { resolve(n) }))
|
||||
}
|
||||
return ps
|
||||
}
|
||||
newRejected := func() []*Promise[int] {
|
||||
ps := make([]*Promise[int], 0, 3)
|
||||
for i := 0; i < 3; i++ {
|
||||
ps = append(ps, New(func(resolve func(int), reject func(error)) { reject(errors.New("e")) }))
|
||||
}
|
||||
return ps
|
||||
}
|
||||
|
||||
before := runtime.NumGoroutine()
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
_, _ = Race(newResolved(i)).Await()
|
||||
_, _ = Race(newRejected()).Await()
|
||||
_, _ = Any(newResolved(i)).Await()
|
||||
_, _ = All(newRejected()).Await()
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for runtime.NumGoroutine() > before && time.Now().Before(deadline) {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
|
||||
assert.Equal(true, runtime.NumGoroutine() <= before)
|
||||
}
|
||||
Reference in new issue
Block a user