-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
80 lines (72 loc) · 1.84 KB
/
task.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
package nxsugar
import (
"fmt"
"sync/atomic"
"time"
"github.com/jaracil/ei"
nexus "github.com/nayarsystems/nxgo/nxcore"
)
type NexusConn struct {
*nexus.NexusConn
trackid string
isMocked bool
mockResponses []TaskMockResponse
responseCountRef *uint64
}
type Task struct {
nexus.Task
Service *Service `json:"-"`
isMocked bool
mockResponses []TaskMockResponse
responseCount uint64
}
type TaskMockResponse struct {
Result interface{}
Error error
}
func NewMockedTask(task *Task, mockedResponses []TaskMockResponse) *Task {
task.isMocked = true
task.mockResponses = mockedResponses
task.responseCount = 0
return task
}
func (t *Task) GetConn() *NexusConn {
tid := ei.N(t.Params).M("@metadata").M("trackid").StringZ()
if tid == "" {
tid = newTrackId()
}
if conn := t.Task.GetConn(); conn == nil {
return nil
} else {
return &NexusConn{conn, tid, t.isMocked, t.mockResponses, &t.responseCount}
}
}
func (nc *NexusConn) TaskPush(method string, params interface{}, timeout time.Duration, opts ...*nexus.TaskOpts) (interface{}, error) {
if params == nil {
params = ei.M{"@metadata": ei.M{"trackid": nc.trackid}}
} else if pm, err := ei.N(params).MapStr(); err == nil {
if pm == nil {
pm = map[string]interface{}{}
}
md, err := ei.N(pm).M("@metadata").MapStr()
if err != nil || md == nil {
md = map[string]interface{}{}
}
tid := ei.N(md).M("trackid").StringZ()
if tid == "" {
tid = nc.trackid
}
md["trackid"] = tid
pm["@metadata"] = md
params = pm
}
if nc.isMocked {
mockResIdx := atomic.AddUint64(nc.responseCountRef, 1)
if len(nc.mockResponses) < int(mockResIdx) {
return nil, fmt.Errorf("No more mock responses")
}
response := nc.mockResponses[mockResIdx-1]
return response.Result, response.Error
}
return nc.NexusConn.TaskPush(method, params, timeout, opts...)
}