forked from zenthangplus/goccm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goccm_test.go
72 lines (61 loc) · 1.3 KB
/
goccm_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
71
72
package goccm
import (
"fmt"
"log"
"testing"
"time"
)
func TestExample(t *testing.T) {
c := New(3)
for i := 1; i <= 10; i++ {
c.Wait()
go func(i int) {
fmt.Printf("Job %d is running\n", i)
time.Sleep(20 * time.Millisecond)
c.Done()
}(i)
}
c.WaitAllDone()
}
// TestManuallyClose will close after 5 jobs, others should not run
func TestManuallyClose(t *testing.T) {
executedJobs := make(chan int, 1000)
c := New(3)
for i := 1; i <= 1000; i++ {
jobId := i
c.Wait()
go func() {
executedJobs <- jobId
fmt.Printf("Executed job id %d\n", jobId)
time.Sleep(20 * time.Millisecond)
c.Done()
}()
if i == 5 {
log.Printf("Closing manager")
c.Close()
break
}
}
c.WaitAllDone()
}
func TestConcurrency(t *testing.T) {
var maxRunningJobs = 3
testMaxRunningJobs := make(chan int32, 100)
c := New(maxRunningJobs)
for i := 1; i <= 10; i++ {
c.Wait()
go func(i int) {
fmt.Printf("Current running jobs %d\n", c.RunningCount())
testMaxRunningJobs <- c.RunningCount()
time.Sleep(20 * time.Millisecond)
c.Done()
}(i)
}
c.WaitAllDone()
for i := 1; i <= 10; i++ {
observed := <-testMaxRunningJobs
if observed > int32(maxRunningJobs) {
t.Errorf("The number of concurrency jobs has exceeded %d. Real result %d.", maxRunningJobs, int(observed))
}
}
}