seaweedfs/weed/util/limited_async_pool_test.go

64 lines
1.3 KiB
Go
Raw Permalink 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)
2022-09-26 00:42:23 +00:00
p.Execute(FirstFunc)
p.Execute(SecondFunc)
p.Execute(ThirdFunc)
p.Execute(FourthFunc)
p.Execute(FifthFunc)
2022-09-25 20:45:55 +00:00
2022-09-25 20:49:28 +00:00
var sorted_results []int
2022-09-26 00:42:23 +00:00
for i := 0; i < 5; i++ {
f := p.NextFuture()
x := f.Await().(int)
2022-09-25 20:45:55 +00:00
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
}