From 597e3189cae9708b5638d4e8d8d4b1170eb7b12e Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 29 Dec 2024 02:33:32 -0500 Subject: [PATCH] sync: Add tests for RWMonitor as well --- sync/monitor_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/sync/monitor_test.go b/sync/monitor_test.go index a7b3eb5..1ca4c15 100644 --- a/sync/monitor_test.go +++ b/sync/monitor_test.go @@ -45,3 +45,46 @@ func TestMonitorConcurrent (test *testing.T) { } () } } + +func TestRWMonitor (test *testing.T) { + mon := NewRWMonitor(9) + func () { + value, done := mon.Borrow() + defer done() + test.Log(value) + if value != 9 { test.Fatal("not equal") } + } () + func () { + value, done := mon.BorrowReturn() + value += 3 + defer done(value) + } () + func () { + value, done := mon.RBorrow() + defer done() + test.Log(value) + if value != 12 { test.Fatal("not equal") } + } () + mon.Set(11) + func () { + value, done := mon.RBorrow() + defer done() + test.Log(value) + if value != 11 { test.Fatal("not equal") } + } () +} + +func TestRWMonitorConcurrent (test *testing.T) { + mon := NewRWMonitor(map[int] int { }) + for index := 0; index < 16; index ++ { + go func () { + for index := 0; index < 8192; index ++ { + func () { + value, done := mon.Borrow() + defer done() + value[rand.Int()] = rand.Int() + } () + } + } () + } +}