forked from google/mtail
/
fake_watcher.go
114 lines (101 loc) · 2.38 KB
/
fake_watcher.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
// Copyright 2015 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package watcher
import (
"path"
"sync"
"github.com/golang/glog"
)
// FakeWatcher implements an in-memory Watcher.
type FakeWatcher struct {
watchesMu sync.RWMutex
watches map[string]bool
eventsMu sync.RWMutex
events []chan Event
isClosed bool
}
// NewFakeWatcher returns a fake Watcher for use in tests.
func NewFakeWatcher() *FakeWatcher {
return &FakeWatcher{
watches: make(map[string]bool)}
}
// Add adds a watch to the FakeWatcher
func (w *FakeWatcher) Add(name string) error {
w.watchesMu.Lock()
w.watches[name] = true
w.watchesMu.Unlock()
return nil
}
// Close closes down the FakeWatcher
func (w *FakeWatcher) Close() error {
w.eventsMu.Lock()
defer w.eventsMu.Unlock()
if !w.isClosed {
for _, c := range w.events {
close(c)
}
w.isClosed = true
}
return nil
}
// Remove removes a watch from the FakeWatcher
func (w *FakeWatcher) Remove(name string) error {
w.watchesMu.Lock()
delete(w.watches, name)
w.watchesMu.Unlock()
return nil
}
// Events returns a new channel of messages.
func (w *FakeWatcher) Events() <-chan Event {
w.eventsMu.Lock()
defer w.eventsMu.Unlock()
if w.isClosed {
panic("closed")
}
r := make(chan Event, 1)
w.events = append(w.events, r)
return r
}
func (w *FakeWatcher) sendEvent(e Event) {
w.eventsMu.RLock()
defer w.eventsMu.RUnlock()
for _, c := range w.events {
c <- e
}
}
// InjectCreate lets a test inject a fake creation event.
func (w *FakeWatcher) InjectCreate(name string) {
dirname := path.Dir(name)
w.watchesMu.RLock()
dir_watched := w.watches[dirname]
w.watchesMu.RUnlock()
if dir_watched {
w.sendEvent(CreateEvent{name})
w.Add(name)
} else {
glog.Warningf("not watching %s to see %s", dirname, name)
}
}
// InjectUpdate lets a test inject a fake update event.
func (w *FakeWatcher) InjectUpdate(name string) {
w.watchesMu.RLock()
watched := w.watches[name]
w.watchesMu.RUnlock()
if watched {
w.sendEvent(UpdateEvent{name})
} else {
glog.Warningf("can't update: not watching %s", name)
}
}
// InjectDelete lets a test inject a fake deletion event.
func (w *FakeWatcher) InjectDelete(name string) {
w.watchesMu.RLock()
watched := w.watches[name]
w.watchesMu.RUnlock()
if watched {
w.sendEvent(DeleteEvent{name})
w.Remove(name)
} else {
glog.Warningf("can't delete: not watching %s", name)
}
}