-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfake_device.go
More file actions
41 lines (33 loc) · 784 Bytes
/
Copy pathfake_device.go
File metadata and controls
41 lines (33 loc) · 784 Bytes
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 ra
import "context"
type fakeDeviceWatcher struct {
watchers map[string]chan deviceState
}
var _ deviceWatcher = &fakeDeviceWatcher{}
func newFakeDeviceWatcher(devs ...string) *fakeDeviceWatcher {
fdw := &fakeDeviceWatcher{
watchers: make(map[string]chan deviceState),
}
for _, dev := range devs {
fdw.watchers[dev] = make(chan deviceState, 1)
}
return fdw
}
func (w *fakeDeviceWatcher) watch(ctx context.Context, name string) (<-chan deviceState, error) {
devCh := make(chan deviceState)
go func() {
defer close(devCh)
for {
select {
case <-ctx.Done():
return
case dev := <-w.watchers[name]:
devCh <- dev
}
}
}()
return devCh, nil
}
func (w *fakeDeviceWatcher) update(name string, dev deviceState) {
w.watchers[name] <- dev
}