-
Notifications
You must be signed in to change notification settings - Fork 244
/
taskset_test.go
110 lines (96 loc) · 2.03 KB
/
taskset_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
package boomer
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Test taskset", func() {
It("test weighing taskset with single task", func() {
ts := NewWeighingTaskSet()
taskAIsRun := false
taskA := &Task{
Name: "A",
Weight: 1,
Fn: func() {
taskAIsRun = true
},
}
ts.AddTask(taskA)
Expect(ts.GetTask(0).Name).To(Equal("A"))
Expect(ts.GetTask(1)).To(BeNil())
Expect(ts.GetTask(-1)).To(BeNil())
ts.Run()
Expect(taskAIsRun).To(BeTrue())
})
It("test weighing taskset with two tasks", func() {
ts := NewWeighingTaskSet()
taskA := &Task{
Name: "A",
Weight: 1,
}
taskB := &Task{
Name: "B",
Weight: 2,
}
ts.AddTask(taskA)
ts.AddTask(taskB)
Expect(ts.GetTask(0).Name).To(Equal("A"))
Expect(ts.GetTask(1).Name).To(Equal("B"))
})
It("test weighing taskset get task with three tasks", func() {
ts := NewWeighingTaskSet()
taskA := &Task{
Name: "A",
Weight: 1,
}
taskB := &Task{
Name: "B",
Weight: 2,
}
taskC := &Task{
Name: "C",
Weight: 3,
}
ts.AddTask(taskA)
ts.AddTask(taskB)
ts.AddTask(taskC)
Expect(ts.GetTask(0).Name).To(Equal("A"))
Expect(ts.GetTask(1).Name).To(Equal("B"))
Expect(ts.GetTask(2).Name).To(Equal("B"))
Expect(ts.GetTask(3).Name).To(Equal("C"))
Expect(ts.GetTask(3).Name).To(Equal("C"))
Expect(ts.GetTask(3).Name).To(Equal("C"))
})
It("test smooth round robin taskset run", func() {
ts := NewSmoothRoundRobinTaskSet()
results := []string{}
taskA := &Task{
Name: "A",
Weight: 5,
Fn: func() {
results = append(results, "A")
},
}
taskB := &Task{
Name: "B",
Weight: 1,
Fn: func() {
results = append(results, "B")
},
}
taskC := &Task{
Name: "C",
Weight: 1,
Fn: func() {
results = append(results, "C")
},
}
ts.AddTask(taskA)
ts.AddTask(taskB)
ts.AddTask(taskC)
for i := 0; i < 7; i++ {
ts.Run()
}
expected := []string{"A", "A", "B", "A", "C", "A", "A"}
Expect(results).To(BeEquivalentTo(expected))
})
})