From 049261aca1bd558f9de6c261ff5054713c5e82f0 Mon Sep 17 00:00:00 2001 From: Tim Allclair Date: Tue, 30 Jan 2024 11:35:48 -0800 Subject: [PATCH] Delete unused config utilities --- pkg/util/config/config.go | 62 ---------------------------------- pkg/util/config/config_test.go | 44 +++--------------------- 2 files changed, 5 insertions(+), 101 deletions(-) diff --git a/pkg/util/config/config.go b/pkg/util/config/config.go index 66fce13866d..5aea63f4f8f 100644 --- a/pkg/util/config/config.go +++ b/pkg/util/config/config.go @@ -30,13 +30,6 @@ type Merger interface { Merge(source string, update interface{}) error } -// MergeFunc implements the Merger interface -type MergeFunc func(source string, update interface{}) error - -func (f MergeFunc) Merge(source string, update interface{}) error { - return f(source, update) -} - // Mux is a class for merging configuration from multiple sources. Changes are // pushed via channels and sent to the merge function. type Mux struct { @@ -85,58 +78,3 @@ func (m *Mux) listen(source string, listenChannel <-chan interface{}) { m.merger.Merge(source, update) } } - -// Accessor is an interface for retrieving the current merge state. -type Accessor interface { - // MergedState returns a representation of the current merge state. - // Must be reentrant when more than one source is defined. - MergedState() interface{} -} - -// AccessorFunc implements the Accessor interface. -type AccessorFunc func() interface{} - -func (f AccessorFunc) MergedState() interface{} { - return f() -} - -type Listener interface { - // OnUpdate is invoked when a change is made to an object. - OnUpdate(instance interface{}) -} - -// ListenerFunc receives a representation of the change or object. -type ListenerFunc func(instance interface{}) - -func (f ListenerFunc) OnUpdate(instance interface{}) { - f(instance) -} - -type Broadcaster struct { - // Listeners for changes and their lock. - listenerLock sync.RWMutex - listeners []Listener -} - -// NewBroadcaster registers a set of listeners that support the Listener interface -// and notifies them all on changes. -func NewBroadcaster() *Broadcaster { - return &Broadcaster{} -} - -// Add registers listener to receive updates of changes. -func (b *Broadcaster) Add(listener Listener) { - b.listenerLock.Lock() - defer b.listenerLock.Unlock() - b.listeners = append(b.listeners, listener) -} - -// Notify notifies all listeners. -func (b *Broadcaster) Notify(instance interface{}) { - b.listenerLock.RLock() - listeners := b.listeners - b.listenerLock.RUnlock() - for _, listener := range listeners { - listener.OnUpdate(instance) - } -} diff --git a/pkg/util/config/config_test.go b/pkg/util/config/config_test.go index 060e4ec4c98..767d4130e50 100644 --- a/pkg/util/config/config_test.go +++ b/pkg/util/config/config_test.go @@ -62,23 +62,11 @@ func TestMergeInvoked(t *testing.T) { mux.ChannelWithContext(ctx, "one") <- "test" } -func TestMergeFuncInvoked(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() +// mergeFunc implements the Merger interface +type mergeFunc func(source string, update interface{}) error - ch := make(chan bool) - mux := NewMux(MergeFunc(func(source string, update interface{}) error { - if source != "one" { - t.Errorf("Expected %s, Got %s", "one", source) - } - if update.(string) != "test" { - t.Errorf("Expected %s, Got %s", "test", update) - } - ch <- true - return nil - })) - mux.ChannelWithContext(ctx, "one") <- "test" - <-ch +func (f mergeFunc) Merge(source string, update interface{}) error { + return f(source, update) } func TestSimultaneousMerge(t *testing.T) { @@ -86,7 +74,7 @@ func TestSimultaneousMerge(t *testing.T) { defer cancel() ch := make(chan bool, 2) - mux := NewMux(MergeFunc(func(source string, update interface{}) error { + mux := NewMux(mergeFunc(func(source string, update interface{}) error { switch source { case "one": if update.(string) != "test" { @@ -109,25 +97,3 @@ func TestSimultaneousMerge(t *testing.T) { <-ch <-ch } - -func TestBroadcaster(t *testing.T) { - b := NewBroadcaster() - b.Notify(struct{}{}) - - ch := make(chan bool, 2) - b.Add(ListenerFunc(func(object interface{}) { - if object != "test" { - t.Errorf("Expected %s, Got %s", "test", object) - } - ch <- true - })) - b.Add(ListenerFunc(func(object interface{}) { - if object != "test" { - t.Errorf("Expected %s, Got %s", "test", object) - } - ch <- true - })) - b.Notify("test") - <-ch - <-ch -}