forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_handler.go
91 lines (69 loc) · 1.69 KB
/
fake_handler.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
package fakes
import (
"sync"
boshhandler "github.com/cloudfoundry/bosh-agent/handler"
)
type FakeHandler struct {
RunFunc boshhandler.Func
RunCallBack func()
RunErr error
ReceivedRun bool
ReceivedStart bool
ReceivedStop bool
// Keeps list of all receivd health manager requests
sendLock sync.Mutex
sendInputs []SendInput
RegisteredAdditionalFunc boshhandler.Func
SendCallback func(SendInput)
SendErr error
}
type SendInput struct {
Target boshhandler.Target
Topic boshhandler.Topic
Message interface{}
}
func NewFakeHandler() *FakeHandler {
return &FakeHandler{sendInputs: []SendInput{}}
}
func (h *FakeHandler) Run(handlerFunc boshhandler.Func) error {
h.ReceivedRun = true
h.RunFunc = handlerFunc
if h.RunCallBack != nil {
h.RunCallBack()
}
return h.RunErr
}
func (h *FakeHandler) KeepOnRunning() {
block := make(chan error)
h.RunCallBack = func() { <-block }
}
func (h *FakeHandler) Start(handlerFunc boshhandler.Func) error {
h.ReceivedStart = true
h.RunFunc = handlerFunc
return nil
}
func (h *FakeHandler) Stop() {
h.ReceivedStop = true
}
func (h *FakeHandler) RegisterAdditionalFunc(handlerFunc boshhandler.Func) {
h.RegisteredAdditionalFunc = handlerFunc
}
func (h *FakeHandler) Send(target boshhandler.Target, topic boshhandler.Topic, message interface{}) error {
h.sendLock.Lock()
defer h.sendLock.Unlock()
sendInput := SendInput{
Target: target,
Topic: topic,
Message: message,
}
h.sendInputs = append(h.sendInputs, sendInput)
if h.SendCallback != nil {
h.SendCallback(sendInput)
}
return h.SendErr
}
func (h *FakeHandler) SendInputs() []SendInput {
h.sendLock.Lock()
defer h.sendLock.Unlock()
return h.sendInputs
}