-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_controller_test.go
110 lines (102 loc) · 3.08 KB
/
task_controller_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 weightask
import (
"context"
"fmt"
"testing"
"time"
)
type MockTask struct {
weight int
work func() (any, error)
}
func (m MockTask) Weight() int {
return m.weight
}
func (m MockTask) PerformTask(_ context.Context) (any, error) {
return m.work()
}
func TestTasker_ProcessTasks(t *testing.T) {
tests := []struct {
name string
tasks []Task
want any
expectErr bool
}{
{
name: "Test with non-err",
tasks: []Task{
MockTask{weight: 1, work: func() (any, error) { return "Task1Result", nil }},
MockTask{weight: 2, work: func() (any, error) { return "Task2Result", nil }},
},
want: "Task2Result",
expectErr: false,
},
{
name: "Test with err",
tasks: []Task{
MockTask{weight: 1, work: func() (any, error) { return "Task1Result", nil }},
MockTask{weight: 2, work: func() (any, error) { time.Sleep(time.Microsecond * 500); return "Task2Result", nil }},
MockTask{weight: 3, work: func() (any, error) { return "Task3Result", fmt.Errorf("Task3Error") }},
MockTask{weight: 4, work: func() (any, error) { time.Sleep(time.Second); return "Task4Result", nil }},
MockTask{weight: 1, work: func() (any, error) { time.Sleep(time.Microsecond * 300); return "Task5Result", nil }},
},
want: "Task4Result",
expectErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := NewTaskController()
for _, task := range tt.tasks {
tc.AddTask(task)
}
got, err := tc.ProcessTasks(context.Background())
if (err != nil) != tt.expectErr {
t.Errorf("TaskController.ProcessTasks() error = %v, expectErr %v", err, tt.expectErr)
return
}
if got != tt.want {
t.Errorf("TaskController.ProcessTasks() = %v, want %v", got, tt.want)
}
})
}
}
func TestTasker_ProcessTasksWithTimeoutCtx(t *testing.T) {
tests := []struct {
name string
tasks []Task
want any
expectErr bool
}{
{
name: "Test",
tasks: []Task{
MockTask{weight: 1, work: func() (any, error) { return "Task1Result", nil }},
MockTask{weight: 2, work: func() (any, error) { time.Sleep(time.Second * 900); return "Task2Result", nil }},
MockTask{weight: 3, work: func() (any, error) { return "Task3Result", fmt.Errorf("Task3Error") }},
MockTask{weight: 4, work: func() (any, error) { time.Sleep(time.Second); return "Task4Result", nil }},
MockTask{weight: 1, work: func() (any, error) { time.Sleep(time.Microsecond * 300); return "Task5Result", nil }},
},
want: "Task1Result",
expectErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := NewTaskController(WithWeightList(new(WeightSlice)))
for _, task := range tt.tasks {
tc.AddTask(task)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond*500)
defer cancel()
got, err := tc.ProcessTasks(ctx)
if (err != nil) != tt.expectErr {
t.Errorf("TaskController.ProcessTasks() error = %v, expectErr %v", err, tt.expectErr)
return
}
if got != tt.want {
t.Errorf("TaskController.ProcessTasks() = %v, want %v", got, tt.want)
}
})
}
}