-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorchestrator_test.go
More file actions
121 lines (104 loc) · 2.73 KB
/
orchestrator_test.go
File metadata and controls
121 lines (104 loc) · 2.73 KB
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
112
113
114
115
116
117
118
119
120
121
package patron
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
func setupTestOrchestrator() WorkerOrchestrator {
return New(
Config{
WorkerCount: 5,
WorkerFunc: func(job *Job) error {
time.Sleep(1 * time.Second)
payloadName, err := job.Get("name")
if err != nil {
return err
}
fmt.Printf("%d. job completed.\nJob payload name: %s\n", job.ID, payloadName)
return nil
},
},
)
}
func TestNewWorkerOrchestrator(t *testing.T) {
orch := New(Config{WorkerCount: 5, WorkerFunc: nil})
if orch == nil {
t.Error("Expected new worker orchestrator to be not nil")
}
}
func TestAddJobToQueue(t *testing.T) {
workerOrchestrator := setupTestOrchestrator()
workerOrchestrator.AddJobToQueue(&Job{
ID: 10,
Context: context.Background(),
Payload: map[string]interface{}{
"name": "HTTP Request",
"dest_url": "http://localhost:8080/",
},
})
if len := workerOrchestrator.GetQueueLength(); len != 1 {
t.Errorf("Expected queue length 1, got %d", len)
}
}
func TestStartAllJobsSuccess(t *testing.T) {
workerOrchestrator := setupTestOrchestrator()
workerOrchestrator.AddJobToQueue(&Job{
ID: 10,
Context: context.Background(),
Payload: map[string]interface{}{
"name": "HTTP Request",
"dest_url": "http://localhost:8080/test",
},
})
workerOrchestrator.AddJobToQueue(&Job{
ID: 11,
Context: context.Background(),
Payload: map[string]interface{}{
"name": "HTTP Request",
"dest_url": "http://localhost:8080/test2",
},
})
results := workerOrchestrator.Start(context.Background())
if len(results) != 2 {
t.Errorf("Expected 2 results, got %d", len(results))
}
if results[0].Error != nil {
t.Errorf("Expected no error for job 1, got %v", results[0].Error)
}
if results[1].Error != nil {
t.Errorf("Expected no error for job 2, got %v", results[1].Error)
}
}
func TestStartOneJobFailure(t *testing.T) {
workerOrchestrator := setupTestOrchestrator()
workerOrchestrator.AddJobToQueue(&Job{
ID: 10,
Context: context.Background(),
Payload: map[string]interface{}{
"name": "HTTP Request",
"dest_url": "http://localhost:8080/test",
},
})
workerOrchestrator.AddJobToQueue(&Job{
ID: 11,
Context: context.Background(),
Payload: map[string]interface{}{},
})
results := workerOrchestrator.Start(context.Background())
if len(results) != 2 {
t.Errorf("Expected 2 results, got %d", len(results))
}
for _, result := range results {
if result.JobID == 11 {
if !errors.Is(result.Error, ErrJobPayloadNotFound) {
t.Errorf("Expected ErrJobPayloadNotFound for job 11, got %v", result.Error)
}
} else {
if result.Error != nil {
t.Errorf("Expected no error for job %d, got %v", result.JobID, result.Error)
}
}
}
}