-
Notifications
You must be signed in to change notification settings - Fork 43
/
testing.go
43 lines (35 loc) · 975 Bytes
/
testing.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
package faktory_worker
import (
"context"
"encoding/json"
faktory "github.com/contribsys/faktory/client"
)
type PerformExecutor interface {
Execute(*faktory.Job, Perform) error
ExecuteContext(context.Context, *faktory.Job, Perform) error
}
type testExecutor struct {
*faktory.Pool
}
func NewTestExecutor(p *faktory.Pool) PerformExecutor {
return &testExecutor{Pool: p}
}
func (tp *testExecutor) Execute(specjob *faktory.Job, p Perform) error {
ctx := context.Background()
return tp.ExecuteContext(ctx, specjob, p)
}
func (tp *testExecutor) ExecuteContext(ctx context.Context, specjob *faktory.Job, p Perform) error {
// perform a JSON round trip to ensure Perform gets the arguments
// exactly how a round trip to Faktory would look.
data, err := json.Marshal(specjob)
if err != nil {
return err
}
var job faktory.Job
err = json.Unmarshal(data, &job)
if err != nil {
return err
}
c := jobContext(ctx, tp.Pool, &job)
return p(c, job.Args...)
}