seaweedfs/weed/util/limited_async_pool_test.go

64 lines
1.4 KiB
Go
Raw Normal View History

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