github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/watch/filter_test.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package watch_test 18 19 import ( 20 "reflect" 21 "testing" 22 23 . "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/watch" 24 ) 25 26 func TestFilter(t *testing.T) { 27 table := []Event{ 28 {Type: Added, Object: testType("foo")}, 29 {Type: Added, Object: testType("bar")}, 30 {Type: Added, Object: testType("baz")}, 31 {Type: Added, Object: testType("qux")}, 32 {Type: Added, Object: testType("zoo")}, 33 } 34 35 source := NewFake() 36 filtered := Filter(source, func(e Event) (Event, bool) { 37 return e, e.Object.(testType)[0] != 'b' 38 }) 39 40 go func() { 41 for _, item := range table { 42 source.Action(item.Type, item.Object) 43 } 44 source.Stop() 45 }() 46 47 var got []string 48 for { 49 event, ok := <-filtered.ResultChan() 50 if !ok { 51 break 52 } 53 got = append(got, string(event.Object.(testType))) 54 } 55 56 if e, a := []string{"foo", "qux", "zoo"}, got; !reflect.DeepEqual(e, a) { 57 t.Errorf("got %v, wanted %v", e, a) 58 } 59 } 60 61 func TestFilterStop(t *testing.T) { 62 source := NewFake() 63 filtered := Filter(source, func(e Event) (Event, bool) { 64 return e, e.Object.(testType)[0] != 'b' 65 }) 66 67 go func() { 68 source.Add(testType("foo")) 69 filtered.Stop() 70 }() 71 72 var got []string 73 for { 74 event, ok := <-filtered.ResultChan() 75 if !ok { 76 break 77 } 78 got = append(got, string(event.Object.(testType))) 79 } 80 81 if e, a := []string{"foo"}, got; !reflect.DeepEqual(e, a) { 82 t.Errorf("got %v, wanted %v", e, a) 83 } 84 } 85 86 func TestRecorder(t *testing.T) { 87 events := []Event{ 88 {Type: Added, Object: testType("foo")}, 89 {Type: Added, Object: testType("bar")}, 90 {Type: Added, Object: testType("baz")}, 91 {Type: Added, Object: testType("qux")}, 92 {Type: Added, Object: testType("zoo")}, 93 } 94 95 source := NewFake() 96 go func() { 97 for _, item := range events { 98 source.Action(item.Type, item.Object) 99 } 100 source.Stop() 101 }() 102 103 recorder := NewRecorder(source) 104 for { 105 _, ok := <-recorder.Interface.ResultChan() 106 if !ok { 107 break 108 } 109 } 110 recordedEvents := recorder.Events() 111 if !reflect.DeepEqual(recordedEvents, events) { 112 t.Errorf("got %v, expected %v", recordedEvents, events) 113 } 114 }