-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththreads_test.go
70 lines (60 loc) · 1.43 KB
/
threads_test.go
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
64
65
66
67
68
69
70
package threads
import (
"fmt"
"math/rand"
"testing"
"time"
future "github.com/wushilin/future"
)
func TestFuture(t *testing.T) {
tp := NewPool(10, 1000000)
tasks := make([]func() int, 1000)
for i := 0; i < len(tasks); i++ {
tasks[i] = sleeper
}
fmt.Printf("About to start\n")
tp.Start()
fg := SubmitTasks(tp, tasks)
go func() {
for {
ok, result := fg.WaitTimeOut(time.Second)
if !ok {
fmt.Println("fg.WaitTimeout didn't succeed")
} else {
fmt.Printf("fg.WaitTimeout did work! %v\n", result)
return
}
}
}()
fmt.Printf("FutureGroup is %v\n", fg)
for {
fmt.Printf("Count: %d\n", fg.Count())
fmt.Printf("ReadyCount: %d\n", fg.ReadyCount())
ready, result := fg.WaitTimeOut(100 * time.Millisecond)
fmt.Println(ready, result)
fmt.Printf("Active count: %d\n", fg.ThreadPool().ActiveCount())
fmt.Printf("Pending jobs: %d\n", fg.ThreadPool().PendingCount())
fmt.Printf("Completed jobs: %d\n", fg.ThreadPool().CompletedCount())
if ready {
break
}
}
fmt.Println(fg.IsAllReady())
fmt.Println("About to wait")
tp.Shutdown()
tp.Wait()
fmt.Println("Wait done")
futInstant := future.InstantFutureOf(5)
future.Chain(futInstant, mapadd).Then(printer[int])
}
func mapadd(i int) int {
return i + 1
}
var r = rand.New(rand.NewSource(99))
func sleeper() int {
time.Sleep(time.Duration((r.Int31() % 100)) * time.Millisecond)
return 1
}
func printer[T any](i T) {
fmt.Printf("You got %v\n", i)
}