k8s.io/kubernetes@v1.29.3/pkg/util/config/config_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 config
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestConfigurationChannels(t *testing.T) {
    26  	ctx, cancel := context.WithCancel(context.Background())
    27  	defer cancel()
    28  
    29  	mux := NewMux(nil)
    30  	channelOne := mux.ChannelWithContext(ctx, "one")
    31  	if channelOne != mux.ChannelWithContext(ctx, "one") {
    32  		t.Error("Didn't get the same muxuration channel back with the same name")
    33  	}
    34  	channelTwo := mux.ChannelWithContext(ctx, "two")
    35  	if channelOne == channelTwo {
    36  		t.Error("Got back the same muxuration channel for different names")
    37  	}
    38  }
    39  
    40  type MergeMock struct {
    41  	source string
    42  	update interface{}
    43  	t      *testing.T
    44  }
    45  
    46  func (m MergeMock) Merge(source string, update interface{}) error {
    47  	if m.source != source {
    48  		m.t.Errorf("Expected %s, Got %s", m.source, source)
    49  	}
    50  	if !reflect.DeepEqual(m.update, update) {
    51  		m.t.Errorf("Expected %s, Got %s", m.update, update)
    52  	}
    53  	return nil
    54  }
    55  
    56  func TestMergeInvoked(t *testing.T) {
    57  	ctx, cancel := context.WithCancel(context.Background())
    58  	defer cancel()
    59  
    60  	merger := MergeMock{"one", "test", t}
    61  	mux := NewMux(&merger)
    62  	mux.ChannelWithContext(ctx, "one") <- "test"
    63  }
    64  
    65  func TestMergeFuncInvoked(t *testing.T) {
    66  	ctx, cancel := context.WithCancel(context.Background())
    67  	defer cancel()
    68  
    69  	ch := make(chan bool)
    70  	mux := NewMux(MergeFunc(func(source string, update interface{}) error {
    71  		if source != "one" {
    72  			t.Errorf("Expected %s, Got %s", "one", source)
    73  		}
    74  		if update.(string) != "test" {
    75  			t.Errorf("Expected %s, Got %s", "test", update)
    76  		}
    77  		ch <- true
    78  		return nil
    79  	}))
    80  	mux.ChannelWithContext(ctx, "one") <- "test"
    81  	<-ch
    82  }
    83  
    84  func TestSimultaneousMerge(t *testing.T) {
    85  	ctx, cancel := context.WithCancel(context.Background())
    86  	defer cancel()
    87  
    88  	ch := make(chan bool, 2)
    89  	mux := NewMux(MergeFunc(func(source string, update interface{}) error {
    90  		switch source {
    91  		case "one":
    92  			if update.(string) != "test" {
    93  				t.Errorf("Expected %s, Got %s", "test", update)
    94  			}
    95  		case "two":
    96  			if update.(string) != "test2" {
    97  				t.Errorf("Expected %s, Got %s", "test2", update)
    98  			}
    99  		default:
   100  			t.Errorf("Unexpected source, Got %s", update)
   101  		}
   102  		ch <- true
   103  		return nil
   104  	}))
   105  	source := mux.ChannelWithContext(ctx, "one")
   106  	source2 := mux.ChannelWithContext(ctx, "two")
   107  	source <- "test"
   108  	source2 <- "test2"
   109  	<-ch
   110  	<-ch
   111  }
   112  
   113  func TestBroadcaster(t *testing.T) {
   114  	b := NewBroadcaster()
   115  	b.Notify(struct{}{})
   116  
   117  	ch := make(chan bool, 2)
   118  	b.Add(ListenerFunc(func(object interface{}) {
   119  		if object != "test" {
   120  			t.Errorf("Expected %s, Got %s", "test", object)
   121  		}
   122  		ch <- true
   123  	}))
   124  	b.Add(ListenerFunc(func(object interface{}) {
   125  		if object != "test" {
   126  			t.Errorf("Expected %s, Got %s", "test", object)
   127  		}
   128  		ch <- true
   129  	}))
   130  	b.Notify("test")
   131  	<-ch
   132  	<-ch
   133  }