From 07997efbaa70d0382134e327096583ce363910ce Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Fri, 8 Jul 2022 21:54:35 +0800 Subject: [PATCH] fix: add inmemory lock test for PR#14555 (#14570) Co-authored-by: QIU Jian --- pkg/cloudcommon/db/lockman/inmemory_test.go | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/cloudcommon/db/lockman/inmemory_test.go b/pkg/cloudcommon/db/lockman/inmemory_test.go index 293f4b10a2..0efc249bfd 100644 --- a/pkg/cloudcommon/db/lockman/inmemory_test.go +++ b/pkg/cloudcommon/db/lockman/inmemory_test.go @@ -15,7 +15,10 @@ package lockman import ( + "context" + "math/rand" "testing" + "time" ) func TestInMemoryLockManager(t *testing.T) { @@ -29,3 +32,27 @@ func TestInMemoryLockManager(t *testing.T) { } testLockManager(t, cfg) } + +func TestRunManu(t *testing.T) { + rand.Seed(100) + lockman := NewInMemoryLockManager() + counter := 0 + MAX := 512 + for i := 0; i < MAX; i++ { + go func() { + ctx := context.Background() + now := time.Now() + ms := now.UnixMilli() + ctx = context.WithValue(ctx, "Time", ms) + lockman.LockKey(ctx, "test") + defer lockman.UnlockKey(ctx, "test") + counter++ + time.Sleep(1 + time.Duration(rand.Intn(50))*time.Millisecond) + t.Logf("counter: %d", counter) + }() + } + for counter < MAX { + time.Sleep(1) + } + t.Logf("complete") +}