remove informational informer methods again

This commit is contained in:
Alexander Zielenski 2022-06-16 00:09:20 -07:00
parent 7436af3302
commit f52f4a8e30
No known key found for this signature in database
GPG Key ID: 754BC11B447F7843
2 changed files with 36 additions and 53 deletions

View File

@ -132,16 +132,6 @@ import (
// state, except that its ResourceVersion is replaced with a
// ResourceVersion in which the object is actually absent.
//
// The informational methods (EventHandlerCount, IsStopped, IsStarted)
// are intended to be used to manage informers in any upper informer
// management layer for creating and destroying informers on-the fly when
// adding or removing handlers.
// Beware of race conditions: Such a layer must hide the basic informer
// objects from its users and offer a closed *synchronized* view for informers.
// Although these informational methods are synchronized each, in
// sequences the state queried first might have been changed before
// calling the next method, if other callers (or the stop channel)
// are able to interact with the informer interface in parallel.
type SharedInformer interface {
// AddEventHandler adds an event handler to the shared informer using the shared informer's resync
// period. Events to a single handler are delivered sequentially, but there is no coordination
@ -222,16 +212,10 @@ type SharedInformer interface {
// data races.
SetTransform(handler TransformFunc) error
// EventHandlerCount return the number of actually registered
// event handlers.
EventHandlerCount() int
// IsStopped reports whether the informer has already been stopped.
// Adding event handlers to already stopped informers is not possible.
// An informer already stopped will never be started again.
IsStopped() bool
// IsStarted reports whether the informer has already been started
IsStarted() bool
}
// ResourceEventHandlerHandle is a handle returned by the
@ -667,13 +651,6 @@ func (s *sharedIndexInformer) OnDelete(old interface{}) {
s.processor.distribute(deleteNotification{oldObj: old}, false)
}
// IsStarted reports whether the informer has already been started
func (s *sharedIndexInformer) IsStarted() bool {
s.startedLock.Lock()
defer s.startedLock.Unlock()
return s.started
}
// IsStopped reports whether the informer has already been stopped
func (s *sharedIndexInformer) IsStopped() bool {
s.startedLock.Lock()
@ -681,14 +658,6 @@ func (s *sharedIndexInformer) IsStopped() bool {
return s.stopped
}
// EventHandlerCount reports whether the informer still has registered
// event handlers
func (s *sharedIndexInformer) EventHandlerCount() int {
s.startedLock.Lock()
defer s.startedLock.Unlock()
return len(s.processor.listeners)
}
// RemoveEventHandlerByHandle tries to remove a formerly added event handler by its
// handle returned for its registration.
// If a handler has been added multiple times, only the registration for the

View File

@ -96,6 +96,20 @@ func (l *testListener) satisfiedExpectations() bool {
return sets.NewString(l.receivedItemNames...).Equal(l.expectedItemNames)
}
func eventHandlerCount(i SharedInformer) int {
s := i.(*sharedIndexInformer)
s.startedLock.Lock()
defer s.startedLock.Unlock()
return len(s.processor.listeners)
}
func isStarted(i SharedInformer) bool {
s := i.(*sharedIndexInformer)
s.startedLock.Lock()
defer s.startedLock.Unlock()
return s.started
}
func TestListenerResyncPeriods(t *testing.T) {
// source simulates an apiserver object endpoint.
source := fcache.NewFakeControllerSource()
@ -410,21 +424,21 @@ func TestSharedInformerRemoveHandler(t *testing.T) {
return
}
if informer.EventHandlerCount() != 2 {
t.Errorf("informer has %d registered handler, instead of 2", informer.EventHandlerCount())
if eventHandlerCount(informer) != 2 {
t.Errorf("informer has %d registered handler, instead of 2", eventHandlerCount(informer))
}
if err := informer.RemoveEventHandlerByHandle(handle2); err != nil {
t.Errorf("removing of first pointer handler failed: %s", err)
}
if informer.EventHandlerCount() != 1 {
t.Errorf("after removing handler informer has %d registered handler(s), instead of 1", informer.EventHandlerCount())
if eventHandlerCount(informer) != 1 {
t.Errorf("after removing handler informer has %d registered handler(s), instead of 1", eventHandlerCount(informer))
}
if err := informer.RemoveEventHandlerByHandle(handle1); err != nil {
t.Errorf("removing of second pointer handler failed: %s", err)
}
if informer.EventHandlerCount() != 0 {
if eventHandlerCount(informer) != 0 {
t.Errorf("informer still has registered handlers after removing both handlers")
}
}
@ -448,22 +462,22 @@ func TestSharedInformerRemoveNonComparableHandler(t *testing.T) {
return
}
if informer.EventHandlerCount() != 2 {
t.Errorf("informer has %d registered handler(s), instead of 2", informer.EventHandlerCount())
if eventHandlerCount(informer) != 2 {
t.Errorf("informer has %d registered handler(s), instead of 2", eventHandlerCount(informer))
}
if err := informer.RemoveEventHandlerByHandle(handle2); err != nil {
t.Errorf("removing of pointer handler failed: %s", err)
}
if informer.EventHandlerCount() != 1 {
t.Errorf("after removal informer has %d registered handler(s), instead of 1", informer.EventHandlerCount())
if eventHandlerCount(informer) != 1 {
t.Errorf("after removal informer has %d registered handler(s), instead of 1", eventHandlerCount(informer))
}
if err := informer.RemoveEventHandlerByHandle(handle1); err != nil {
t.Errorf("removing of non-pointer handler failed: %s", err)
}
if informer.EventHandlerCount() != 0 {
t.Errorf("after removal informer has %d registered handler(s), instead of 0", informer.EventHandlerCount())
if eventHandlerCount(informer) != 0 {
t.Errorf("after removal informer has %d registered handler(s), instead of 0", eventHandlerCount(informer))
}
}
@ -496,8 +510,8 @@ func TestSharedInformerMultipleRegistration(t *testing.T) {
return
}
if informer.EventHandlerCount() != 2 {
t.Errorf("informer has %d registered handler(s), instead of 1", informer.EventHandlerCount())
if eventHandlerCount(informer) != 2 {
t.Errorf("informer has %d registered handler(s), instead of 1", eventHandlerCount(informer))
}
if err := informer.RemoveEventHandlerByHandle(reg1); err != nil {
@ -513,11 +527,11 @@ func TestSharedInformerMultipleRegistration(t *testing.T) {
return
}
if informer.EventHandlerCount() != 1 {
if informer.EventHandlerCount() == 0 {
if eventHandlerCount(informer) != 1 {
if eventHandlerCount(informer) == 0 {
t.Errorf("informer has no registered handler anymore after removal of duplicate registrations")
} else {
t.Errorf("informer has unexpected number (%d) of handlers after removal of duplicate handler registration", informer.EventHandlerCount())
t.Errorf("informer has unexpected number (%d) of handlers after removal of duplicate handler registration", eventHandlerCount(informer))
}
}
@ -530,8 +544,8 @@ func TestSharedInformerMultipleRegistration(t *testing.T) {
return
}
if informer.EventHandlerCount() != 0 {
t.Errorf("informer has unexpected number (%d) of handlers after removal of second handler registrations", informer.EventHandlerCount())
if eventHandlerCount(informer) != 0 {
t.Errorf("informer has unexpected number (%d) of handlers after removal of second handler registrations", eventHandlerCount(informer))
}
}
@ -572,7 +586,7 @@ func TestStateSharedInformer(t *testing.T) {
listener := newTestListener("listener", 0, "pod1")
informer.AddEventHandlerWithResyncPeriod(listener, listener.resyncPeriod)
if informer.IsStarted() {
if isStarted(informer) {
t.Errorf("informer already started after creation")
return
}
@ -588,7 +602,7 @@ func TestStateSharedInformer(t *testing.T) {
return
}
if !informer.IsStarted() {
if !isStarted(informer) {
t.Errorf("informer does not report to be started although handling events")
close(stop)
return
@ -607,7 +621,7 @@ func TestStateSharedInformer(t *testing.T) {
t.Errorf("informer reports not to be stopped although stop channel closed")
return
}
if !informer.IsStarted() {
if !isStarted(informer) {
t.Errorf("informer reports not to be started after it has been started and stopped")
return
}