istio

Форк
0
/
fakefilewatcher.go 
143 строки · 3.6 Кб
1
// Copyright Istio Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package filewatcher
16

17
import (
18
	"errors"
19
	"fmt"
20
	"sync"
21

22
	"github.com/fsnotify/fsnotify"
23
)
24

25
// NewFileWatcherFunc returns a function which creates a new file
26
// watcher. This may be used to provide test hooks for using the
27
// FakeWatcher implementation below.
28
type NewFileWatcherFunc func() FileWatcher
29

30
// FakeWatcher provides a fake file watcher implementation for unit
31
// tests. Production code should use the `NewWatcher()`.
32
type FakeWatcher struct {
33
	sync.Mutex
34

35
	events      map[string]chan fsnotify.Event
36
	errors      map[string]chan error
37
	changedFunc func(path string, added bool)
38
}
39

40
// InjectEvent injects an event into the fake file watcher.
41
func (w *FakeWatcher) InjectEvent(path string, event fsnotify.Event) {
42
	w.Lock()
43
	ch, ok := w.events[path]
44
	w.Unlock()
45

46
	if ok {
47
		ch <- event
48
	}
49
}
50

51
// InjectError injects an error into the fake file watcher.
52
func (w *FakeWatcher) InjectError(path string, err error) {
53
	w.Lock()
54
	ch, ok := w.errors[path]
55
	w.Unlock()
56

57
	if ok {
58
		ch <- err
59
	}
60
}
61

62
// NewFakeWatcher returns a function which creates a new fake watcher for unit
63
// testing. This allows observe callers to inject events and errors per-watched
64
// path. changedFunc() provides a callback notification when a new watch is added
65
// or removed. Production code should use `NewWatcher()`.
66
func NewFakeWatcher(changedFunc func(path string, added bool)) (NewFileWatcherFunc, *FakeWatcher) {
67
	w := &FakeWatcher{
68
		events:      make(map[string]chan fsnotify.Event),
69
		errors:      make(map[string]chan error),
70
		changedFunc: changedFunc,
71
	}
72
	return func() FileWatcher {
73
		return w
74
	}, w
75
}
76

77
// Add is a fake implementation of the FileWatcher interface.
78
func (w *FakeWatcher) Add(path string) error {
79
	w.Lock()
80

81
	// w.events and w.errors are always updated togeather. We only check
82
	// the first to determine existence.
83
	if _, ok := w.events[path]; ok {
84
		w.Unlock()
85
		return fmt.Errorf("path %v already exists", path)
86
	}
87

88
	w.events[path] = make(chan fsnotify.Event, 1000)
89
	w.errors[path] = make(chan error, 1000)
90

91
	w.Unlock()
92

93
	if w.changedFunc != nil {
94
		w.changedFunc(path, true)
95
	}
96
	return nil
97
}
98

99
// Remove is a fake implementation of the FileWatcher interface.
100
func (w *FakeWatcher) Remove(path string) error {
101
	w.Lock()
102
	defer w.Unlock()
103

104
	if _, ok := w.events[path]; !ok {
105
		return errors.New("path doesn't exist")
106
	}
107

108
	delete(w.events, path)
109
	delete(w.errors, path)
110
	if w.changedFunc != nil {
111
		w.changedFunc(path, false)
112
	}
113
	return nil
114
}
115

116
// Close is a fake implementation of the FileWatcher interface.
117
func (w *FakeWatcher) Close() error {
118
	w.Lock()
119
	for path, ch := range w.events {
120
		close(ch)
121
		delete(w.events, path)
122
	}
123
	for path, ch := range w.errors {
124
		close(ch)
125
		delete(w.errors, path)
126
	}
127
	defer w.Unlock()
128
	return nil
129
}
130

131
// Events is a fake implementation of the FileWatcher interface.
132
func (w *FakeWatcher) Events(path string) chan fsnotify.Event {
133
	w.Lock()
134
	defer w.Unlock()
135
	return w.events[path]
136
}
137

138
// Errors is a fake implementation of the FileWatcher interface.
139
func (w *FakeWatcher) Errors(path string) chan error {
140
	w.Lock()
141
	defer w.Unlock()
142
	return w.errors[path]
143
}
144

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.