Skip to content

Commit

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

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

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

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

val.(*lockData).timestamp = time.Now();
val.(*lockData).isLocked = true;
lock.mutex.Lock();

lock.timestamp = time.Now();
lock.isLocked = true;
}


func Unlock(key string) error {
lockManagerMutex.Lock();
defer lockManagerMutex.Unlock();
Expand All @@ -54,14 +59,13 @@ func Unlock(key string) error {
}

lock.isLocked = false;
lock.timestamp = time.Now();
lock.timestamp = time.Now();
lock.mutex.Unlock();
return nil;
}


func removeStaleLocks() {
ticker := time.NewTicker(TICKER_DURATION_HOUR * time.Hour);
ticker = time.NewTicker(TICKER_DURATION_HOUR * time.Hour);

for range ticker.C {
RemoveStaleLocksOnce();
Expand Down
21 changes: 7 additions & 14 deletions common/lockmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ var (
doesNotExistKey = "dne"
staleDuration = time.Duration(config.STALELOCK_DURATION.Get()) * time.Second;
wg sync.WaitGroup;

)

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

testUnlockingKey(test)

testConcurrentLockingUnlocking(test)
testConcurrentLockingUnlocking()

testUnlockingAnUnlockedLock(test)

Expand All @@ -42,7 +41,7 @@ func testUnlockingKey(test *testing.T) {
}
}

func testConcurrentLockingUnlocking(test *testing.T) {
func testConcurrentLockingUnlocking() {
Lock(key);
Lock(key2);
wg.Add(2);
Expand Down Expand Up @@ -74,14 +73,10 @@ func testUnlockingMissingKey(test *testing.T) {
if err == nil {
test.Errorf("Lockmanager unlocked a key that doesn't exist");
}


}

func testLockConcurrencyWithStaleCheck(test *testing.T) {

testWithCondition := func(shouldPreventRemoval bool) bool {

Lock(key)
Unlock(key)

Expand All @@ -107,15 +102,13 @@ func testLockConcurrencyWithStaleCheck(test *testing.T) {
}

// 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")
}


// Test if a lock gets past both "if's" and gets removed from the map
// Test if a lock gets past both "if's" and gets removed from the map.
if testWithCondition(false) {
test.Errorf("Stale lock was not removed")
}

}
}

0 comments on commit c046367

Please sign in to comment.