aboutsummaryrefslogtreecommitdiff
path: root/weed/util/limited_async_pool_test.go
blob: 090ce5375a34b14e494a7180ed27ab16793b2f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package util

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"sort"
	"testing"
	"time"
)

func TestAsyncPool(t *testing.T) {
	p := NewLimitedAsyncExecutor(3)

	p.Execute(FirstFunc)
	p.Execute(SecondFunc)
	p.Execute(ThirdFunc)
	p.Execute(FourthFunc)
	p.Execute(FifthFunc)

	var sorted_results []int
	for i := 0; i < 5; i++ {
		f := p.NextFuture()
		x := f.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
}