seaweedfs/weed/util/limited_async_pool_test.go

64 lines
1.3 KiB
Go
Raw Normal View History

2022-09-26 04:45:55 +08:00
package util
import (
"fmt"
2022-09-26 04:49:28 +08:00
"github.com/stretchr/testify/assert"
"sort"
2022-09-26 04:45:55 +08:00
"testing"
"time"
)
func TestAsyncPool(t *testing.T) {
p := NewLimitedAsyncExecutor(3)
2022-09-26 08:42:23 +08:00
p.Execute(FirstFunc)
p.Execute(SecondFunc)
p.Execute(ThirdFunc)
p.Execute(FourthFunc)
p.Execute(FifthFunc)
2022-09-26 04:45:55 +08:00
2022-09-26 04:49:28 +08:00
var sorted_results []int
2022-09-26 08:42:23 +08:00
for i := 0; i < 5; i++ {
f := p.NextFuture()
x := f.Await().(int)
2022-09-26 04:45:55 +08:00
println(x)
2022-09-26 04:49:28 +08:00
sorted_results = append(sorted_results, x)
2022-09-26 04:45:55 +08:00
}
2022-09-26 04:49:28 +08:00
assert.True(t, sort.IntsAreSorted(sorted_results), "results should be sorted")
2022-09-26 04:45:55 +08:00
}
func FirstFunc() any {
fmt.Println("-- Executing first function --")
2022-09-26 04:49:28 +08:00
time.Sleep(70 * time.Millisecond)
2022-09-26 04:45:55 +08:00
fmt.Println("-- First Function finished --")
return 1
}
func SecondFunc() any {
fmt.Println("-- Executing second function --")
2022-09-26 04:49:28 +08:00
time.Sleep(50 * time.Millisecond)
2022-09-26 04:45:55 +08:00
fmt.Println("-- Second Function finished --")
return 2
}
func ThirdFunc() any {
fmt.Println("-- Executing third function --")
2022-09-26 04:49:28 +08:00
time.Sleep(20 * time.Millisecond)
2022-09-26 04:45:55 +08:00
fmt.Println("-- Third Function finished --")
return 3
}
func FourthFunc() any {
fmt.Println("-- Executing fourth function --")
2022-09-26 04:49:28 +08:00
time.Sleep(100 * time.Millisecond)
2022-09-26 04:45:55 +08:00
fmt.Println("-- Fourth Function finished --")
return 4
}
func FifthFunc() any {
fmt.Println("-- Executing fifth function --")
2022-09-26 04:49:28 +08:00
time.Sleep(40 * time.Millisecond)
2022-09-26 04:45:55 +08:00
fmt.Println("-- Fourth fifth finished --")
return 5
}