Skip to content

Commit

Permalink
system: fix test relying on reflection to determine if mutex is locked
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Penner <[email protected]>
  • Loading branch information
matthewpi committed Mar 10, 2025
1 parent 91e0162 commit 4b4e8f8
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions system/sink_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package system

import (
"fmt"
"reflect"
"sync"
"testing"
"time"
Expand All @@ -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) {
Expand Down

0 comments on commit 4b4e8f8

Please sign in to comment.