Make tests pass again
This commit is contained in:
@@ -24,6 +24,11 @@ type FilterFunc func(in Event) (out Event, keep bool)
|
||||
// Putting a filter on a watch, as an unavoidable side-effect due to the way
|
||||
// go channels work, effectively causes the watch's event channel to have its
|
||||
// queue length increased by one.
|
||||
//
|
||||
// WARNING: filter has a fatal flaw, in that it can't properly update the
|
||||
// Type field (Add/Modified/Deleted) to reflect items beginning to pass the
|
||||
// filter when they previously didn't.
|
||||
//
|
||||
func Filter(w Interface, f FilterFunc) Interface {
|
||||
fw := &filteredWatch{
|
||||
incoming: w,
|
||||
|
@@ -23,16 +23,16 @@ import (
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
table := []Event{
|
||||
{Added, "foo"},
|
||||
{Added, "bar"},
|
||||
{Added, "baz"},
|
||||
{Added, "qux"},
|
||||
{Added, "zoo"},
|
||||
{Added, testType("foo")},
|
||||
{Added, testType("bar")},
|
||||
{Added, testType("baz")},
|
||||
{Added, testType("qux")},
|
||||
{Added, testType("zoo")},
|
||||
}
|
||||
|
||||
source := NewFake()
|
||||
filtered := Filter(source, func(e Event) (Event, bool) {
|
||||
return e, e.Object.(string)[0] != 'b'
|
||||
return e, e.Object.(testType)[0] != 'b'
|
||||
})
|
||||
|
||||
go func() {
|
||||
@@ -48,7 +48,7 @@ func TestFilter(t *testing.T) {
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
got = append(got, event.Object.(string))
|
||||
got = append(got, string(event.Object.(testType)))
|
||||
}
|
||||
|
||||
if e, a := []string{"foo", "qux", "zoo"}, got; !reflect.DeepEqual(e, a) {
|
||||
@@ -59,11 +59,11 @@ func TestFilter(t *testing.T) {
|
||||
func TestFilterStop(t *testing.T) {
|
||||
source := NewFake()
|
||||
filtered := Filter(source, func(e Event) (Event, bool) {
|
||||
return e, e.Object.(string)[0] != 'b'
|
||||
return e, e.Object.(testType)[0] != 'b'
|
||||
})
|
||||
|
||||
go func() {
|
||||
source.Add("foo")
|
||||
source.Add(testType("foo"))
|
||||
filtered.Stop()
|
||||
}()
|
||||
|
||||
@@ -73,7 +73,7 @@ func TestFilterStop(t *testing.T) {
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
got = append(got, event.Object.(string))
|
||||
got = append(got, string(event.Object.(testType)))
|
||||
}
|
||||
|
||||
if e, a := []string{"foo"}, got; !reflect.DeepEqual(e, a) {
|
||||
|
@@ -20,13 +20,15 @@ import (
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
type fakeDecoder struct {
|
||||
items chan Event
|
||||
}
|
||||
|
||||
func (f fakeDecoder) Decode() (action EventType, object interface{}, err error) {
|
||||
func (f fakeDecoder) Decode() (action EventType, object runtime.Object, err error) {
|
||||
item, open := <-f.items
|
||||
if !open {
|
||||
return action, nil, io.EOF
|
||||
@@ -40,7 +42,7 @@ func (f fakeDecoder) Close() {
|
||||
|
||||
func TestStreamWatcher(t *testing.T) {
|
||||
table := []Event{
|
||||
{Added, "foo"},
|
||||
{Added, testType("foo")},
|
||||
}
|
||||
|
||||
fd := fakeDecoder{make(chan Event, 5)}
|
||||
|
@@ -20,17 +20,21 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testType string
|
||||
|
||||
func (testType) IsAnAPIObject() {}
|
||||
|
||||
func TestFake(t *testing.T) {
|
||||
f := NewFake()
|
||||
|
||||
table := []struct {
|
||||
t EventType
|
||||
s string
|
||||
s testType
|
||||
}{
|
||||
{Added, "foo"},
|
||||
{Modified, "qux"},
|
||||
{Modified, "bar"},
|
||||
{Deleted, "bar"},
|
||||
{Added, testType("foo")},
|
||||
{Modified, testType("qux")},
|
||||
{Modified, testType("bar")},
|
||||
{Deleted, testType("bar")},
|
||||
}
|
||||
|
||||
// Prove that f implements Interface by phrasing this as a function.
|
||||
@@ -43,7 +47,7 @@ func TestFake(t *testing.T) {
|
||||
if e, a := expect.t, got.Type; e != a {
|
||||
t.Fatalf("Expected %v, got %v", e, a)
|
||||
}
|
||||
if a, ok := got.Object.(string); !ok || a != expect.s {
|
||||
if a, ok := got.Object.(testType); !ok || a != expect.s {
|
||||
t.Fatalf("Expected %v, got %v", expect.s, a)
|
||||
}
|
||||
}
|
||||
@@ -54,10 +58,10 @@ func TestFake(t *testing.T) {
|
||||
}
|
||||
|
||||
sender := func() {
|
||||
f.Add("foo")
|
||||
f.Action(Modified, "qux")
|
||||
f.Modify("bar")
|
||||
f.Delete("bar")
|
||||
f.Add(testType("foo"))
|
||||
f.Action(Modified, testType("qux"))
|
||||
f.Modify(testType("bar"))
|
||||
f.Delete(testType("bar"))
|
||||
f.Stop()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user