-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmock.go
150 lines (114 loc) · 3.01 KB
/
mock.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gearman // import "github.com/nathanaelle/gearman/v2"
import (
"bytes"
"context"
"log"
"sync"
"github.com/nathanaelle/gearman/v2/protocol"
)
type (
// MockServer is a fake local gearman server
MockServer struct {
lock *sync.Mutex
handlers map[string]Job
}
)
var _ Worker = &MockServer{}
var _ Client = &MockServer{}
// NewMockServer create a MockServer
func NewMockServer() *MockServer {
return &MockServer{
lock: &sync.Mutex{},
handlers: make(map[string]Job),
}
}
// AddServers implements Client.AddServers and Worker.AddServers
func (mc *MockServer) AddServers(...Conn) {
}
// AddHandler implements Worker.AddHandler
func (mc *MockServer) AddHandler(name string, job Job) Worker {
mc.lock.Lock()
defer mc.lock.Unlock()
mc.handlers[name] = job
return mc
}
// DelHandler implements Worker.DelHandler
func (mc *MockServer) DelHandler(name string) Worker {
mc.lock.Lock()
defer mc.lock.Unlock()
delete(mc.handlers, name)
return mc
}
// DelAllHandlers implements Worker.DelAllHandlers
func (mc *MockServer) DelAllHandlers() Worker {
mc.lock.Lock()
defer mc.lock.Unlock()
mc.handlers = make(map[string]Job)
return mc
}
// GetHandler implements Worker.GetHandler
func (mc *MockServer) GetHandler(name string) Job {
mc.lock.Lock()
defer mc.lock.Unlock()
if job, ok := mc.handlers[name]; ok {
return job
}
return FailJob
}
// Receivers implements Client.Receivers and Worker.Receivers
func (mc *MockServer) Receivers() (<-chan Message, context.Context) {
return nil, nil
}
// Close implements Client.Close and Worker.Close
func (mc *MockServer) Close() error {
return nil
}
// Submit implements Client.Submit
func (mc *MockServer) Submit(req Task) Task {
pkt := req.Packet()
switch pkt.Cmd() {
case protocol.SubmitJob:
reply := mockPacketEmiter(req)
go runWorker(mc.GetHandler(string(pkt.At(0).Bytes())), bytes.NewReader(pkt.At(2).Bytes()), reply, TaskID{})
default:
log.Fatalf("unknown: %v %q", pkt.Cmd(), pkt.Payload())
}
return req
}
// AssignTask implements Client.AssignTask
func (mc *MockServer) AssignTask(tid TaskID) {
}
// GetTask implements Client.GetTask
func (mc *MockServer) GetTask(TaskID) Task {
return nil
}
// ExtractTask implements Client.ExtractTask
func (mc *MockServer) ExtractTask(TaskID) Task {
return nil
}
func mockPacketEmiter(req Task) PacketEmiter {
lock := sync.Mutex{}
return newFuncPacketEmiter(func(res protocol.Packet, fpe PacketEmiter) {
lock.Lock()
defer lock.Unlock()
switch res.Cmd() {
case protocol.WorkCompleteWorker:
taskRes, _ := protocol.WorkComplete.Borrow(res)
go req.Handle(taskRes)
return
case protocol.WorkFailWorker:
taskRes, _ := protocol.WorkFail.Borrow(res)
go req.Handle(taskRes)
return
case protocol.WorkExceptionWorker:
taskRes, _ := protocol.WorkException.Borrow(res)
go req.Handle(taskRes)
return
case protocol.WorkDataWorker:
taskRes, _ := protocol.WorkData.Borrow(res)
go req.Handle(taskRes)
return
}
log.Fatalf("res unknown: %v %q", res.Cmd(), res.Payload())
})
}