k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/config/mux_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 // mergeFunc implements the Merger interface 66 type mergeFunc func(source string, update interface{}) error 67 68 func (f mergeFunc) Merge(source string, update interface{}) error { 69 return f(source, update) 70 } 71 72 func TestSimultaneousMerge(t *testing.T) { 73 ctx, cancel := context.WithCancel(context.Background()) 74 defer cancel() 75 76 ch := make(chan bool, 2) 77 mux := newMux(mergeFunc(func(source string, update interface{}) error { 78 switch source { 79 case "one": 80 if update.(string) != "test" { 81 t.Errorf("Expected %s, Got %s", "test", update) 82 } 83 case "two": 84 if update.(string) != "test2" { 85 t.Errorf("Expected %s, Got %s", "test2", update) 86 } 87 default: 88 t.Errorf("Unexpected source, Got %s", update) 89 } 90 ch <- true 91 return nil 92 })) 93 source := mux.ChannelWithContext(ctx, "one") 94 source2 := mux.ChannelWithContext(ctx, "two") 95 source <- "test" 96 source2 <- "test2" 97 <-ch 98 <-ch 99 }