-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmock.go
76 lines (64 loc) · 1.52 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
package targetsync
import (
"context"
"fmt"
"sync"
)
type mockLocker struct{}
func (m *mockLocker) Lock(context.Context, *LockOptions) (<-chan bool, error) {
ch := make(chan bool, 1)
ch <- true
return ch, nil
}
func newmockSource() *mockSource {
return &mockSource{
ch: make(chan []*Target),
}
}
type mockSource struct {
ch chan []*Target
}
func (m *mockSource) Subscribe(context.Context) (chan []*Target, error) {
return m.ch, nil
}
func newmockDestination() *mockDestination {
return &mockDestination{
targets: make([]*Target, 0),
}
}
type mockDestination struct {
targets []*Target
l sync.RWMutex
}
// GetTargets returns the current set of targets at the destination
func (m *mockDestination) GetTargets(context.Context) ([]*Target, error) {
m.l.RLock()
defer m.l.RUnlock()
return m.targets, nil
}
// AddTargets simply adds the targets described
func (m *mockDestination) AddTargets(_ context.Context, tgts []*Target) error {
m.l.Lock()
defer m.l.Unlock()
m.targets = append(m.targets, tgts...)
return nil
}
// RemoveTargets simply removes the targets described
func (m *mockDestination) RemoveTargets(_ context.Context, tgts []*Target) error {
m.l.Lock()
defer m.l.Unlock()
for _, tgt := range tgts {
foundIdx := -1
for idx, currentTgt := range m.targets {
if tgt.Key() == currentTgt.Key() {
foundIdx = idx
break
}
}
if foundIdx < 0 {
return fmt.Errorf("Unable to remove target we don't have")
}
m.targets = append(m.targets[:foundIdx], m.targets[foundIdx+1:]...)
}
return nil
}