upda/util/locker_memory_test.go
Varakh 165b992629
All checks were successful
/ build (push) Successful in 3m11s
feature(locking): add proper locking and overhaul existing locking service (#34)
Reviewed-on: #34
Co-authored-by: Varakh <varakh@varakh.de>
Co-committed-by: Varakh <varakh@varakh.de>
2024-05-24 00:54:35 +02:00

43 lines
826 B
Go

package util
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
const (
testLockName = "test_lock"
)
func TestLockExpires(t *testing.T) {
a := assert.New(t)
r := NewInMemoryLockRegistry()
r.LockWithTTL(testLockName, 250*time.Millisecond)
a.True(r.Exists(testLockName))
time.Sleep(251 * time.Millisecond)
a.False(r.Exists(testLockName))
}
func TestLockNeverExpires(t *testing.T) {
a := assert.New(t)
r := NewInMemoryLockRegistry()
r.Lock(testLockName)
a.True(r.Exists(testLockName))
time.Sleep(2 * time.Second)
a.True(r.Exists(testLockName))
}
func TestLockLocksAndUnlocks(t *testing.T) {
a := assert.New(t)
r := NewInMemoryLockRegistry()
r.LockWithTTL(testLockName, 250*time.Millisecond)
a.True(r.Exists(testLockName))
_ = r.Unlock(testLockName)
a.False(r.Exists(testLockName))
}