-
Notifications
You must be signed in to change notification settings - Fork 8
/
Top_test.go
111 lines (100 loc) · 2.53 KB
/
Top_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import "testing"
import "time"
import "fmt"
/*func TestTopList(t *testing.T) {
top := new(Top)
top.StartCollectInfo()
for i := 0; i < 10; i++ {
processList, err := top.GetProcessList()
time.Sleep(500 * time.Millisecond)
if processList == nil {
t.Errorf("GetProcessList return null ")
return
} else if err != nil {
t.Errorf("")
return
}
}
}
func TestTopCpu(t *testing.T) {
top := new(Top)
top.StartCollectInfo()
for i := 0; i < 5; i++ {
processList, err := top.GetProcessList()
time.Sleep(500 * time.Millisecond)
if processList == nil {
t.Errorf("GetProcessList return null ")
return
} else if err != nil {
t.Errorf("")
return
} else {
for _, element := range processList {
if element.Cpu != 0 {
fmt.Printf("Name: %v Usage: %v \n", element.Name, int32(element.Cpu*100))
}
}
fmt.Println("_______________________________________________________________________________________________________________________________________________________________________")
}
}
}
func TestTopTicks(t *testing.T) {
top := new(Top)
top.StartCollectInfo()
for i := 0; i < 5; i++ {
Ticks, err := top.getTicksProcessor()
time.Sleep(500 * time.Millisecond)
if err != nil {
t.Errorf("")
return
} else {
fmt.Printf("Ticks: %v \n", Ticks)
fmt.Println("_______________________________________________________________________________________________________________________________________________________________________")
}
}
}*/
func ThreadTest() {
fmt.Println("Do some job")
time.Sleep(50 * time.Millisecond)
}
func StopThreadTest(batchJob *BatchJob) {
for i := 0; i < 50; i++ {
//fmt.Println("Try stop")
batchJob.Stop()
time.Sleep(120 * time.Millisecond)
}
}
func StartThreadTest(batchJob *BatchJob) {
for i := 0; i < 50; i++ {
//fmt.Println("Try start")
batchJob.Start()
time.Sleep(90 * time.Millisecond)
}
}
func TestBatchJob(t *testing.T) {
batchJob := BatchJob{}
err := batchJob.Start()
if err == nil {
t.Errorf("error: Test start not inited job failed ")
}
err = batchJob.Stop()
if err == nil {
t.Errorf("error: Test stop not inited job failed ")
}
batchJob.Job = ThreadTest
batchJob.Start()
time.Sleep(500 * time.Millisecond)
err = batchJob.Stop()
if err != nil {
t.Errorf("error: Test stop failed ")
}
go StartThreadTest(&batchJob)
go StopThreadTest(&batchJob)
time.Sleep(500 * time.Millisecond)
for i := 0; i < 100; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(batchJob.IsRunning())
}
time.Sleep(10000 * time.Millisecond)
}