github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/watch/watch_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/runtime"
    24  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    25  	. "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/watch"
    26  )
    27  
    28  type testType string
    29  
    30  func (obj testType) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
    31  func (obj testType) DeepCopyObject() runtime.Object   { return obj }
    32  
    33  func TestFake(t *testing.T) {
    34  	f := NewFake()
    35  
    36  	table := []struct {
    37  		t EventType
    38  		s testType
    39  	}{
    40  		{Added, testType("foo")},
    41  		{Modified, testType("qux")},
    42  		{Modified, testType("bar")},
    43  		{Deleted, testType("bar")},
    44  		{Error, testType("error: blah")},
    45  	}
    46  
    47  	// Prove that f implements Interface by phrasing this as a function.
    48  	consumer := func(w Interface) {
    49  		for _, expect := range table {
    50  			got, ok := <-w.ResultChan()
    51  			if !ok {
    52  				t.Fatalf("closed early")
    53  			}
    54  			if e, a := expect.t, got.Type; e != a {
    55  				t.Fatalf("Expected %v, got %v", e, a)
    56  			}
    57  			if a, ok := got.Object.(testType); !ok || a != expect.s {
    58  				t.Fatalf("Expected %v, got %v", expect.s, a)
    59  			}
    60  		}
    61  		_, stillOpen := <-w.ResultChan()
    62  		if stillOpen {
    63  			t.Fatal("Never stopped")
    64  		}
    65  	}
    66  
    67  	sender := func() {
    68  		f.Add(testType("foo"))
    69  		f.Action(Modified, testType("qux"))
    70  		f.Modify(testType("bar"))
    71  		f.Delete(testType("bar"))
    72  		f.Error(testType("error: blah"))
    73  		f.Stop()
    74  	}
    75  
    76  	go sender()
    77  	consumer(f)
    78  }
    79  
    80  func TestRaceFreeFake(t *testing.T) {
    81  	f := NewRaceFreeFake()
    82  
    83  	table := []struct {
    84  		t EventType
    85  		s testType
    86  	}{
    87  		{Added, testType("foo")},
    88  		{Modified, testType("qux")},
    89  		{Modified, testType("bar")},
    90  		{Deleted, testType("bar")},
    91  		{Error, testType("error: blah")},
    92  	}
    93  
    94  	// Prove that f implements Interface by phrasing this as a function.
    95  	consumer := func(w Interface) {
    96  		for _, expect := range table {
    97  			got, ok := <-w.ResultChan()
    98  			if !ok {
    99  				t.Fatalf("closed early")
   100  			}
   101  			if e, a := expect.t, got.Type; e != a {
   102  				t.Fatalf("Expected %v, got %v", e, a)
   103  			}
   104  			if a, ok := got.Object.(testType); !ok || a != expect.s {
   105  				t.Fatalf("Expected %v, got %v", expect.s, a)
   106  			}
   107  		}
   108  		_, stillOpen := <-w.ResultChan()
   109  		if stillOpen {
   110  			t.Fatal("Never stopped")
   111  		}
   112  	}
   113  
   114  	sender := func() {
   115  		f.Add(testType("foo"))
   116  		f.Action(Modified, testType("qux"))
   117  		f.Modify(testType("bar"))
   118  		f.Delete(testType("bar"))
   119  		f.Error(testType("error: blah"))
   120  		f.Stop()
   121  	}
   122  
   123  	go sender()
   124  	consumer(f)
   125  }
   126  
   127  func TestEmpty(t *testing.T) {
   128  	w := NewEmptyWatch()
   129  	_, ok := <-w.ResultChan()
   130  	if ok {
   131  		t.Errorf("unexpected result channel result")
   132  	}
   133  	w.Stop()
   134  	_, ok = <-w.ResultChan()
   135  	if ok {
   136  		t.Errorf("unexpected result channel result")
   137  	}
   138  }
   139  
   140  func TestProxyWatcher(t *testing.T) {
   141  	events := []Event{
   142  		{Added, testType("foo")},
   143  		{Modified, testType("qux")},
   144  		{Modified, testType("bar")},
   145  		{Deleted, testType("bar")},
   146  		{Error, testType("error: blah")},
   147  	}
   148  
   149  	ch := make(chan Event, len(events))
   150  	w := NewProxyWatcher(ch)
   151  
   152  	for _, e := range events {
   153  		ch <- e
   154  	}
   155  
   156  	for _, e := range events {
   157  		g := <-w.ResultChan()
   158  		if !reflect.DeepEqual(e, g) {
   159  			t.Errorf("Expected %#v, got %#v", e, g)
   160  			continue
   161  		}
   162  	}
   163  
   164  	w.Stop()
   165  
   166  	select {
   167  	// Closed channel always reads immediately
   168  	case <-w.StopChan():
   169  	default:
   170  		t.Error("Channel isn't closed")
   171  	}
   172  
   173  	// Test double close
   174  	w.Stop()
   175  }