longganhua
2019-07-18 2fcec5d0debb4819c651e8f3b1287f18de9efee9
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
package agent
 
import (
    "sync"
 
    "github.com/hashicorp/serf/serf"
)
 
// MockEventHandler is an EventHandler implementation that can be used
// for tests.
type MockEventHandler struct {
    Events []serf.Event
    sync.Mutex
}
 
func (h *MockEventHandler) HandleEvent(e serf.Event) {
    h.Lock()
    defer h.Unlock()
    h.Events = append(h.Events, e)
}
 
// MockQueryHandler is an EventHandler implementation used for tests,
// it always responds to a query with a given response
type MockQueryHandler struct {
    Response []byte
    Queries  []*serf.Query
    sync.Mutex
}
 
func (h *MockQueryHandler) HandleEvent(e serf.Event) {
    query, ok := e.(*serf.Query)
    if !ok {
        return
    }
 
    h.Lock()
    h.Queries = append(h.Queries, query)
    h.Unlock()
 
    query.Respond(h.Response)
}