From 4b4e8f8fa0ef19e9aa95940bb073ac8ca653cc09 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sun, 9 Mar 2025 20:00:45 -0600 Subject: [PATCH] system: fix test relying on reflection to determine if mutex is locked Signed-off-by: Matthew Penner --- system/sink_pool_test.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/system/sink_pool_test.go b/system/sink_pool_test.go index 7d0fa09..e6cd53a 100644 --- a/system/sink_pool_test.go +++ b/system/sink_pool_test.go @@ -2,7 +2,6 @@ package system import ( "fmt" - "reflect" "sync" "testing" "time" @@ -11,20 +10,11 @@ import ( ) func MutexLocked(m *sync.RWMutex) bool { - v := reflect.ValueOf(m).Elem() - - state := v.FieldByName("w").FieldByName("state") - - readerCountField := v.FieldByName("readerCount") - // go1.20 changed readerCount to an atomic - // ref; https://github.com/golang/go/commit/e509452727b469d89a3fc4a7d1cbf9d3f110efee - var readerCount int64 - if readerCountField.Kind() == reflect.Struct { - readerCount = readerCountField.FieldByName("v").Int() - } else { - readerCount = readerCountField.Int() + unlocked := m.TryLock() + if unlocked { + m.Unlock() } - return state.Int()&1 == 1 || readerCount > 0 + return !unlocked } func TestSink(t *testing.T) {