From eaee96efd30f899cabab37a7250a0bb1467015e3 Mon Sep 17 00:00:00 2001 From: utkarsh348 Date: Thu, 17 Feb 2022 22:11:41 +0530 Subject: [PATCH] Fixed race condition test manager shutdown --- .../nodeshutdown_manager_linux_test.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go b/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go index 9f093b9aa210..e7af66136411 100644 --- a/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go +++ b/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go @@ -614,6 +614,23 @@ func Test_groupByPriority(t *testing.T) { } } +type buffer struct { + b bytes.Buffer + rw sync.RWMutex +} + +func (b *buffer) String() string { + b.rw.RLock() + defer b.rw.RUnlock() + return b.b.String() +} + +func (b *buffer) Write(p []byte) (n int, err error) { + b.rw.Lock() + defer b.rw.Unlock() + return b.b.Write(p) +} + func Test_managerImpl_processShutdownEvent(t *testing.T) { var ( probeManager = probetest.FakeManager{} @@ -681,7 +698,7 @@ func Test_managerImpl_processShutdownEvent(t *testing.T) { l := klog.Level(1) l.Set("1") // hijack the klog output - tmpWriteBuffer := bytes.NewBuffer(nil) + tmpWriteBuffer := new(buffer) klog.SetOutput(tmpWriteBuffer) klog.LogToStderr(false)