Skip to content

Commit 2f87a3b

Browse files
committed
tests created
1 parent 0714745 commit 2f87a3b

File tree

5 files changed

+267
-3
lines changed

5 files changed

+267
-3
lines changed

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/msrexe/patron
22

33
go 1.17
4+
5+
require github.com/stretchr/testify v1.8.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
12+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
13+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
15+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

job.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
var (
9-
ErrPayloadNotFound = errors.New("payload not found")
9+
ErrJobPayloadNotFound = errors.New("payload not found")
1010
)
1111

1212
type Job struct {
@@ -17,12 +17,12 @@ type Job struct {
1717

1818
func (j *Job) GetPayload(key string) (interface{}, error) {
1919
if j.Payload == nil {
20-
return nil, ErrPayloadNotFound
20+
return nil, ErrJobPayloadNotFound
2121
}
2222

2323
val, ok := j.Payload[key]
2424
if !ok {
25-
return nil, ErrPayloadNotFound
25+
return nil, ErrJobPayloadNotFound
2626
}
2727

2828
return val, nil

orchestrator_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,136 @@
11
package patron
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
"time"
8+
9+
"github.com/stretchr/testify/suite"
10+
)
11+
12+
type WorkerOrchestratorTestSuite struct {
13+
suite.Suite
14+
15+
workerOrchestrator WorkerOrchestrator
16+
}
17+
18+
func TestWorkerOrchestratorTestSuite(t *testing.T) {
19+
suite.Run(t, new(WorkerOrchestratorTestSuite))
20+
}
21+
22+
func (suite *WorkerOrchestratorTestSuite) SetupTest() {
23+
workerOrchestrator := NewWorkerOrchestrator(
24+
NewWorkerArray(func(job *Job) error {
25+
time.Sleep(1 * time.Second)
26+
payloadName, err := job.GetPayload("name")
27+
if err != nil {
28+
return err
29+
}
30+
31+
fmt.Printf("%d. job completed.\nJob payload name: %s\n", job.ID, payloadName)
32+
33+
return nil
34+
}, 5),
35+
)
36+
37+
suite.workerOrchestrator = workerOrchestrator
38+
}
39+
40+
func (suite *WorkerOrchestratorTestSuite) TestNewWorkerOrchestrator() {
41+
suite.NotNil(NewWorkerOrchestrator(
42+
NewWorkerArray(nil, 1),
43+
))
44+
}
45+
46+
func (suite *WorkerOrchestratorTestSuite) TestAddJobToQueue() {
47+
suite.workerOrchestrator.AddJobToQueue(&Job{
48+
ID: 10,
49+
Context: context.Background(),
50+
Payload: map[string]interface{}{
51+
"name": "HTTP Request",
52+
"dest_url": "http://localhost:8080/",
53+
},
54+
})
55+
56+
suite.Equal(1, suite.workerOrchestrator.GetQueueLength())
57+
}
58+
59+
// Note: this test is not reliable.
60+
func (suite *WorkerOrchestratorTestSuite) TestStartAsAsync() {
61+
suite.T().Skip()
62+
63+
workerResultCh := make(chan *WorkerResult)
64+
suite.workerOrchestrator.AddJobToQueue(&Job{
65+
ID: 10,
66+
Context: context.Background(),
67+
Payload: map[string]interface{}{
68+
"name": "HTTP Request",
69+
"dest_url": "http://localhost:8080/test",
70+
},
71+
})
72+
suite.workerOrchestrator.AddJobToQueue(&Job{
73+
ID: 11,
74+
Context: context.Background(),
75+
Payload: map[string]interface{}{
76+
"name": "HTTP Request",
77+
"dest_url": "http://localhost:8080/test2",
78+
},
79+
})
80+
81+
suite.workerOrchestrator.StartAsAsync(context.Background(), workerResultCh)
82+
suite.NotEmpty(<-workerResultCh)
83+
suite.NotEmpty(<-workerResultCh)
84+
}
85+
86+
func (suite *WorkerOrchestratorTestSuite) TestStartAllJobsSuccess() {
87+
suite.workerOrchestrator.AddJobToQueue(&Job{
88+
ID: 10,
89+
Context: context.Background(),
90+
Payload: map[string]interface{}{
91+
"name": "HTTP Request",
92+
"dest_url": "http://localhost:8080/test",
93+
},
94+
})
95+
suite.workerOrchestrator.AddJobToQueue(&Job{
96+
ID: 11,
97+
Context: context.Background(),
98+
Payload: map[string]interface{}{
99+
"name": "HTTP Request",
100+
"dest_url": "http://localhost:8080/test2",
101+
},
102+
})
103+
104+
results := suite.workerOrchestrator.Start(context.Background())
105+
106+
suite.Len(results, 2)
107+
suite.Equal(nil, results[0].Error)
108+
suite.Equal(nil, results[1].Error)
109+
}
110+
111+
func (suite *WorkerOrchestratorTestSuite) TestStartOneJobFailure() {
112+
suite.workerOrchestrator.AddJobToQueue(&Job{
113+
ID: 10,
114+
Context: context.Background(),
115+
Payload: map[string]interface{}{
116+
"name": "HTTP Request",
117+
"dest_url": "http://localhost:8080/test",
118+
},
119+
})
120+
suite.workerOrchestrator.AddJobToQueue(&Job{
121+
ID: 11,
122+
Context: context.Background(),
123+
Payload: map[string]interface{}{},
124+
})
125+
126+
results := suite.workerOrchestrator.Start(context.Background())
127+
128+
suite.Len(results, 2)
129+
for _, result := range results {
130+
if result.JobID == 11 {
131+
suite.ErrorIs(result.Error, ErrJobPayloadNotFound)
132+
} else {
133+
suite.Equal(nil, result.Error)
134+
}
135+
}
136+
}

worker_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,107 @@
11
package patron
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
"time"
8+
9+
"github.com/stretchr/testify/suite"
10+
)
11+
12+
type WorkerTestSuite struct {
13+
suite.Suite
14+
15+
worker Worker
16+
}
17+
18+
func TestWorkerTestSuite(t *testing.T) {
19+
suite.Run(t, new(WorkerTestSuite))
20+
}
21+
22+
func (suite *WorkerTestSuite) SetupTest() {
23+
workers := NewWorkerArray(func(job *Job) error {
24+
time.Sleep(1 * time.Second)
25+
payloadName, err := job.GetPayload("name")
26+
if err != nil {
27+
return err
28+
}
29+
30+
fmt.Printf("%d. job completed.\nJob payload name: %s\n", job.ID, payloadName)
31+
32+
return nil
33+
}, 5)
34+
35+
suite.worker = workers[4]
36+
}
37+
38+
func (suite *WorkerTestSuite) TestNewWorkerArray() {
39+
suite.Len(NewWorkerArray(nil, 2), 2)
40+
}
41+
42+
func (suite *WorkerTestSuite) TestGetID() {
43+
suite.Equal(suite.worker.GetID(), 4)
44+
}
45+
46+
func (suite *WorkerTestSuite) TestGetJob() {
47+
suite.worker.SetJob(&Job{
48+
ID: 10,
49+
})
50+
51+
suite.Equal(suite.worker.GetJob().ID, 10)
52+
}
53+
54+
func (suite *WorkerTestSuite) TestSetJob() {
55+
suite.worker.SetJob(&Job{
56+
ID: 10,
57+
Context: context.Background(),
58+
Payload: map[string]interface{}{
59+
"name": "HTTP Request",
60+
"dest_url": "http://localhost:8080/",
61+
},
62+
})
63+
64+
suite.NotEmpty(suite.worker.GetJob())
65+
}
66+
67+
func (suite *WorkerTestSuite) TestFinalizeJob() {
68+
suite.worker.SetJob(&Job{
69+
ID: 10,
70+
})
71+
suite.NotEmpty(suite.worker.GetJob())
72+
73+
suite.worker.FinalizeJob()
74+
suite.Empty(suite.worker.GetJob())
75+
}
76+
77+
func (suite *WorkerTestSuite) TestIsBusy() {
78+
suite.False(suite.worker.IsBusy())
79+
80+
suite.worker.SetJob(&Job{
81+
ID: 10,
82+
})
83+
suite.True(suite.worker.IsBusy())
84+
}
85+
86+
func (suite *WorkerTestSuite) TestWorkSuccess() {
87+
suite.worker.SetJob(&Job{
88+
ID: 10,
89+
Context: context.Background(),
90+
Payload: map[string]interface{}{
91+
"name": "HTTP Request",
92+
"dest_url": "http://localhost:8080/",
93+
},
94+
})
95+
96+
suite.NoError(suite.worker.Work())
97+
}
98+
99+
func (suite *WorkerTestSuite) TestWorkFailure() {
100+
suite.worker.SetJob(&Job{
101+
ID: 10,
102+
Context: context.Background(),
103+
Payload: map[string]interface{}{},
104+
})
105+
106+
suite.ErrorIs(suite.worker.Work(), ErrJobPayloadNotFound)
107+
}

0 commit comments

Comments
 (0)