istio

Форк
0
/
fakefilewatcher_test.go 
139 строк · 3.8 Кб
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
	"fmt"
19
	"testing"
20

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

24
func TestFakeFileWatcher(t *testing.T) {
25
	addedChan := make(chan string, 10)
26
	removedChan := make(chan string, 10)
27

28
	changed := func(path string, added bool) {
29
		if added {
30
			addedChan <- path
31
		} else {
32
			removedChan <- path
33
		}
34
	}
35

36
	newWatcher, fakeWatcher := NewFakeWatcher(changed)
37
	watcher := newWatcher()
38

39
	// verify Add()/Remove()
40
	for _, file := range []string{"foo", "bar", "baz"} {
41
		if err := watcher.Add(file); err != nil {
42
			t.Fatalf("Add() returned error: %v", err)
43
		}
44
		gotAdd := <-addedChan
45
		if gotAdd != file {
46
			t.Fatalf("Add() failed: got %v want %v", gotAdd, file)
47
		}
48
		select {
49
		case <-removedChan:
50
			t.Fatal("Add() failed: callback invoked with added=false")
51
		default:
52
		}
53

54
		wantEvent := fsnotify.Event{Name: file, Op: fsnotify.Write}
55
		fakeWatcher.InjectEvent(file, wantEvent)
56
		gotEvent := <-watcher.Events(file)
57
		if gotEvent != wantEvent {
58
			t.Fatalf("Events() failed: got %v want %v", gotEvent, wantEvent)
59
		}
60

61
		wantError := fmt.Errorf("error=%v", file)
62
		fakeWatcher.InjectError(file, wantError)
63
		gotError := <-watcher.Errors(file)
64
		if gotError != wantError {
65
			t.Fatalf("Errors() failed: got %v want %v", gotError, wantError)
66
		}
67

68
		if err := watcher.Remove(file); err != nil {
69
			t.Fatalf("Remove() returned error: %v", err)
70
		}
71
		gotRemove := <-removedChan
72
		if gotRemove != file {
73
			t.Fatalf("Remove() failed: got %v want %v", gotRemove, file)
74
		}
75
		select {
76
		case <-addedChan:
77
			t.Fatal("Remove() failed: callback invoked with added=false")
78
		default:
79
		}
80

81
		wantEvent = fsnotify.Event{Name: file, Op: fsnotify.Write}
82
		fakeWatcher.InjectEvent(file, wantEvent)
83
		select {
84
		case gotEvent := <-watcher.Events(file):
85
			t.Fatalf("Unexpected Events() after Remove(): got %v", gotEvent)
86
		default:
87
		}
88

89
		wantError = fmt.Errorf("error=%v", file)
90
		fakeWatcher.InjectError(file, wantError)
91
		select {
92
		case gotError := <-watcher.Errors(file):
93
			t.Fatalf("Unexpected Errors() after Remove(): got %v", gotError)
94
		default:
95
		}
96
	}
97

98
	// verify double Add() / Remove()
99
	for _, file := range []string{"foo2", "bar2", "baz2"} {
100
		if err := watcher.Add(file); err != nil {
101
			t.Fatalf("Add() returned error: %v", err)
102
		}
103
		if err := watcher.Add(file); err == nil {
104
			t.Fatal("Adding a path that already exists should fail")
105
		}
106
		if err := watcher.Remove(file); err != nil {
107
			t.Fatalf("Remove() returned error: %v", err)
108
		}
109
		if err := watcher.Remove(file); err == nil {
110
			t.Fatal("Removing a path that doesn't exist should fail")
111
		}
112
	}
113

114
	// verify Close()
115
	for _, file := range []string{"foo3", "bar3", "baz3"} {
116
		watcher.Add(file)
117
	}
118
	if err := watcher.Close(); err != nil {
119
		t.Fatalf("Close() failed: %v", err)
120
	}
121

122
	for _, file := range []string{"foo2", "bar2", "baz2"} {
123
		wantEvent := fsnotify.Event{Name: file, Op: fsnotify.Write}
124
		fakeWatcher.InjectEvent(file, wantEvent)
125
		select {
126
		case gotEvent := <-watcher.Events(file):
127
			t.Fatalf("Unexpected Events() after Remove(): got %v", gotEvent)
128
		default:
129
		}
130

131
		wantError := fmt.Errorf("error=%v", file)
132
		fakeWatcher.InjectError(file, wantError)
133
		select {
134
		case gotError := <-watcher.Errors(file):
135
			t.Fatalf("Unexpected Errors() after Remove(): got %v", gotError)
136
		default:
137
		}
138
	}
139
}
140

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

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

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

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