From f20c6cb2d9060920cae9ff5cade1739c7e0b7f7a Mon Sep 17 00:00:00 2001 From: Abu Kashem Date: Wed, 13 Jan 2021 11:06:17 -0500 Subject: [PATCH] fix apf controller unit test - don't expose the internal states of the apf controller to the caller - return a boolean, instead of the priority level states --- .../apiserver/pkg/util/flowcontrol/apf_controller.go | 7 ------- .../pkg/util/flowcontrol/controller_test.go | 12 ++++++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/apf_controller.go b/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/apf_controller.go index 8909c32f125a..8c34b2b06155 100644 --- a/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/apf_controller.go +++ b/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/apf_controller.go @@ -234,13 +234,6 @@ func (cfgCtlr *configController) updateObservations() { } } -// used from the unit tests only. -func (cfgCtlr *configController) getPriorityLevelState(plName string) *priorityLevelState { - cfgCtlr.lock.Lock() - defer cfgCtlr.lock.Unlock() - return cfgCtlr.priorityLevelStates[plName] -} - func (cfgCtlr *configController) Run(stopCh <-chan struct{}) error { defer utilruntime.HandleCrash() diff --git a/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/controller_test.go b/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/controller_test.go index 3e18e31f230e..115984805ac7 100644 --- a/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/controller_test.go +++ b/staging/src/k8s.io/apiserver/pkg/util/flowcontrol/controller_test.go @@ -54,6 +54,13 @@ var mandPLs = func() map[string]*flowcontrol.PriorityLevelConfiguration { return ans }() +// in general usage, the boolean returned may be inaccurate by the time the caller examines it. +func (cfgCtlr *configController) hasPriorityLevelState(plName string) bool { + cfgCtlr.lock.Lock() + defer cfgCtlr.lock.Unlock() + return cfgCtlr.priorityLevelStates[plName] != nil +} + type ctlrTestState struct { t *testing.T cfgCtlr *configController @@ -380,10 +387,7 @@ func TestAPFControllerWithGracefulShutdown(t *testing.T) { // ensure that the controller has run its first loop. err := wait.PollImmediate(100*time.Millisecond, 5*time.Second, func() (done bool, err error) { - if controller.getPriorityLevelState(plName) == nil { - return false, nil - } - return true, nil + return controller.hasPriorityLevelState(plName), nil }) if err != nil { t.Errorf("expected the controller to reconcile the priority level configuration object: %s, error: %s", plName, err)