Skip to content

Commit

Permalink
styling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverLok committed May 17, 2024
1 parent c046367 commit 35078f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions common/lockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const TICKER_DURATION_HOUR = 1.0;
var (
lockManagerMutex sync.Mutex;
lockMap sync.Map;
ticker *time.Ticker;
ticker *time.Ticker;
)

func init() {
ticker = time.NewTicker(TICKER_DURATION_HOUR * time.Hour)
ticker = time.NewTicker(TICKER_DURATION_HOUR * time.Hour)
go removeStaleLocks();
}

Expand All @@ -33,7 +33,7 @@ func Lock(key string) {
defer lockManagerMutex.Unlock();

val, _ := lockMap.LoadOrStore(key, &lockData{});
lock := val.(*lockData)
lock := val.(*lockData)

lock.mutex.Lock();

Expand Down
40 changes: 20 additions & 20 deletions common/lockmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ var (
key2 = "testkey2"
doesNotExistKey = "dne"
staleDuration = time.Duration(config.STALELOCK_DURATION.Get()) * time.Second;
wg sync.WaitGroup;
wg sync.WaitGroup;
)

func TestLockManager(test *testing.T) {
testLockingKey()
testUnlockingKey(test)
testLockingKey()
testUnlockingKey(test)

testConcurrentLockingUnlocking()
testConcurrentLockingUnlocking()

testUnlockingAnUnlockedLock(test)

testUnlockingMissingKey(test)
testUnlockingMissingKey(test)

testLockConcurrencyWithStaleCheck(test)
testLockConcurrencyWithStaleCheck(test)
}

func testLockingKey() {
Lock(key)
Lock(key)
}

func testUnlockingKey(test *testing.T) {
Expand Down Expand Up @@ -83,26 +83,26 @@ func testLockConcurrencyWithStaleCheck(test *testing.T) {
val, _ := lockMap.Load(key)
val.(*lockData).timestamp = time.Now().Add(-1 * (staleDuration + (1 * time.Second)))

wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(100 * time.Millisecond)
RemoveStaleLocksOnce()
}()
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(100 * time.Millisecond)
RemoveStaleLocksOnce()
}()

if (shouldPreventRemoval) {
Lock(key)
Unlock(key)
}
if (shouldPreventRemoval) {
Lock(key)
Unlock(key)
}

wg.Wait()
wg.Wait()

_, exists := lockMap.Load(key)
return exists
}

// Test if a lock gets past the first "if" in RemoveStaleLocksOnce
// but not the second because it had been aquired.
// but not the second because it had been aquired.
if !testWithCondition(true) {
test.Errorf("Lock was unexpectedly removed even though it was accessed concurrently")
}
Expand Down

0 comments on commit 35078f1

Please sign in to comment.