aboutsummaryrefslogtreecommitdiff
path: root/weed/util/limited_async_pool_test.go
diff options
context:
space:
mode:
authorchrislu <chris.lu@gmail.com>2022-09-25 13:50:02 -0700
committerchrislu <chris.lu@gmail.com>2022-09-25 13:50:02 -0700
commit1484cb224f4e31851a0e45ed29c6f0d072f0856f (patch)
tree9d136d089dfba2c58246ae0517d18e5af6634630 /weed/util/limited_async_pool_test.go
parent600d2f92a44b8fb76653ef0547bce5a406e82eb8 (diff)
parentcc570a4477a791312355dfd325a49bd276522997 (diff)
downloadseaweedfs-1484cb224f4e31851a0e45ed29c6f0d072f0856f.tar.xz
seaweedfs-1484cb224f4e31851a0e45ed29c6f0d072f0856f.zip
Merge branch 'master' into message_send
Diffstat (limited to 'weed/util/limited_async_pool_test.go')
-rw-r--r--weed/util/limited_async_pool_test.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/weed/util/limited_async_pool_test.go b/weed/util/limited_async_pool_test.go
new file mode 100644
index 000000000..935b158da
--- /dev/null
+++ b/weed/util/limited_async_pool_test.go
@@ -0,0 +1,63 @@
+package util
+
+import (
+ "fmt"
+ "github.com/stretchr/testify/assert"
+ "sort"
+ "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))
+
+ var sorted_results []int
+ for _, r := range results {
+ x := r.Await().(int)
+ println(x)
+ sorted_results = append(sorted_results, x)
+ }
+ assert.True(t, sort.IntsAreSorted(sorted_results), "results should be sorted")
+}
+
+func FirstFunc() any {
+ fmt.Println("-- Executing first function --")
+ time.Sleep(70 * time.Millisecond)
+ fmt.Println("-- First Function finished --")
+ return 1
+}
+
+func SecondFunc() any {
+ fmt.Println("-- Executing second function --")
+ time.Sleep(50 * time.Millisecond)
+ fmt.Println("-- Second Function finished --")
+ return 2
+}
+
+func ThirdFunc() any {
+ fmt.Println("-- Executing third function --")
+ time.Sleep(20 * time.Millisecond)
+ fmt.Println("-- Third Function finished --")
+ return 3
+}
+
+func FourthFunc() any {
+ fmt.Println("-- Executing fourth function --")
+ time.Sleep(100 * time.Millisecond)
+ fmt.Println("-- Fourth Function finished --")
+ return 4
+}
+
+func FifthFunc() any {
+ fmt.Println("-- Executing fifth function --")
+ time.Sleep(40 * time.Millisecond)
+ fmt.Println("-- Fourth fifth finished --")
+ return 5
+}