istio
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
15package filewatcher
16
17import (
18"fmt"
19"testing"
20
21"github.com/fsnotify/fsnotify"
22)
23
24func TestFakeFileWatcher(t *testing.T) {
25addedChan := make(chan string, 10)
26removedChan := make(chan string, 10)
27
28changed := func(path string, added bool) {
29if added {
30addedChan <- path
31} else {
32removedChan <- path
33}
34}
35
36newWatcher, fakeWatcher := NewFakeWatcher(changed)
37watcher := newWatcher()
38
39// verify Add()/Remove()
40for _, file := range []string{"foo", "bar", "baz"} {
41if err := watcher.Add(file); err != nil {
42t.Fatalf("Add() returned error: %v", err)
43}
44gotAdd := <-addedChan
45if gotAdd != file {
46t.Fatalf("Add() failed: got %v want %v", gotAdd, file)
47}
48select {
49case <-removedChan:
50t.Fatal("Add() failed: callback invoked with added=false")
51default:
52}
53
54wantEvent := fsnotify.Event{Name: file, Op: fsnotify.Write}
55fakeWatcher.InjectEvent(file, wantEvent)
56gotEvent := <-watcher.Events(file)
57if gotEvent != wantEvent {
58t.Fatalf("Events() failed: got %v want %v", gotEvent, wantEvent)
59}
60
61wantError := fmt.Errorf("error=%v", file)
62fakeWatcher.InjectError(file, wantError)
63gotError := <-watcher.Errors(file)
64if gotError != wantError {
65t.Fatalf("Errors() failed: got %v want %v", gotError, wantError)
66}
67
68if err := watcher.Remove(file); err != nil {
69t.Fatalf("Remove() returned error: %v", err)
70}
71gotRemove := <-removedChan
72if gotRemove != file {
73t.Fatalf("Remove() failed: got %v want %v", gotRemove, file)
74}
75select {
76case <-addedChan:
77t.Fatal("Remove() failed: callback invoked with added=false")
78default:
79}
80
81wantEvent = fsnotify.Event{Name: file, Op: fsnotify.Write}
82fakeWatcher.InjectEvent(file, wantEvent)
83select {
84case gotEvent := <-watcher.Events(file):
85t.Fatalf("Unexpected Events() after Remove(): got %v", gotEvent)
86default:
87}
88
89wantError = fmt.Errorf("error=%v", file)
90fakeWatcher.InjectError(file, wantError)
91select {
92case gotError := <-watcher.Errors(file):
93t.Fatalf("Unexpected Errors() after Remove(): got %v", gotError)
94default:
95}
96}
97
98// verify double Add() / Remove()
99for _, file := range []string{"foo2", "bar2", "baz2"} {
100if err := watcher.Add(file); err != nil {
101t.Fatalf("Add() returned error: %v", err)
102}
103if err := watcher.Add(file); err == nil {
104t.Fatal("Adding a path that already exists should fail")
105}
106if err := watcher.Remove(file); err != nil {
107t.Fatalf("Remove() returned error: %v", err)
108}
109if err := watcher.Remove(file); err == nil {
110t.Fatal("Removing a path that doesn't exist should fail")
111}
112}
113
114// verify Close()
115for _, file := range []string{"foo3", "bar3", "baz3"} {
116watcher.Add(file)
117}
118if err := watcher.Close(); err != nil {
119t.Fatalf("Close() failed: %v", err)
120}
121
122for _, file := range []string{"foo2", "bar2", "baz2"} {
123wantEvent := fsnotify.Event{Name: file, Op: fsnotify.Write}
124fakeWatcher.InjectEvent(file, wantEvent)
125select {
126case gotEvent := <-watcher.Events(file):
127t.Fatalf("Unexpected Events() after Remove(): got %v", gotEvent)
128default:
129}
130
131wantError := fmt.Errorf("error=%v", file)
132fakeWatcher.InjectError(file, wantError)
133select {
134case gotError := <-watcher.Errors(file):
135t.Fatalf("Unexpected Errors() after Remove(): got %v", gotError)
136default:
137}
138}
139}
140