mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 03:03:59 +00:00
Delete unused config utilities
This commit is contained in:
parent
7f5cd1961c
commit
049261aca1
@ -30,13 +30,6 @@ type Merger interface {
|
|||||||
Merge(source string, update interface{}) error
|
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
|
// Mux is a class for merging configuration from multiple sources. Changes are
|
||||||
// pushed via channels and sent to the merge function.
|
// pushed via channels and sent to the merge function.
|
||||||
type Mux struct {
|
type Mux struct {
|
||||||
@ -85,58 +78,3 @@ func (m *Mux) listen(source string, listenChannel <-chan interface{}) {
|
|||||||
m.merger.Merge(source, update)
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -62,23 +62,11 @@ func TestMergeInvoked(t *testing.T) {
|
|||||||
mux.ChannelWithContext(ctx, "one") <- "test"
|
mux.ChannelWithContext(ctx, "one") <- "test"
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMergeFuncInvoked(t *testing.T) {
|
// mergeFunc implements the Merger interface
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
type mergeFunc func(source string, update interface{}) error
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
ch := make(chan bool)
|
func (f mergeFunc) Merge(source string, update interface{}) error {
|
||||||
mux := NewMux(MergeFunc(func(source string, update interface{}) error {
|
return f(source, update)
|
||||||
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 TestSimultaneousMerge(t *testing.T) {
|
func TestSimultaneousMerge(t *testing.T) {
|
||||||
@ -86,7 +74,7 @@ func TestSimultaneousMerge(t *testing.T) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
ch := make(chan bool, 2)
|
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 {
|
switch source {
|
||||||
case "one":
|
case "one":
|
||||||
if update.(string) != "test" {
|
if update.(string) != "test" {
|
||||||
@ -109,25 +97,3 @@ func TestSimultaneousMerge(t *testing.T) {
|
|||||||
<-ch
|
<-ch
|
||||||
<-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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user