Files
kopia/internal/cache/mutex_map_test.go
Jarek Kowalski a8e4d50600 build(deps): upgraded linter to v1.55.2, fixed warnings (#3611)
* build(deps): upgraded linter to v1.55.2, fixed warnings

* removed unsafe hacks with better equivalents

* test fixes
2024-02-02 23:34:34 -08:00

51 lines
1.2 KiB
Go

package cache
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestMutexMap_ExclusiveLock(t *testing.T) {
var m mutexMap
require.Empty(t, m.entries)
m.exclusiveLock("foo")
require.Len(t, m.entries, 1)
require.False(t, m.tryExclusiveLock("foo"))
require.True(t, m.tryExclusiveLock("bar"))
require.False(t, m.trySharedLock("bar"))
require.Len(t, m.entries, 2)
m.exclusiveUnlock("foo")
require.Len(t, m.entries, 1)
require.True(t, m.tryExclusiveLock("foo"))
require.Len(t, m.entries, 2)
m.exclusiveUnlock("foo")
require.Len(t, m.entries, 1)
m.exclusiveUnlock("bar")
require.Empty(t, m.entries)
}
func TestMutexMap_SharedLock(t *testing.T) {
var m mutexMap
require.Empty(t, m.entries)
m.sharedLock("foo")
require.Len(t, m.entries, 1)
m.sharedLock("foo")
require.Len(t, m.entries, 1)
require.True(t, m.trySharedLock("foo"))
require.Len(t, m.entries, 1)
// exclusive lock can't be acquired while shared lock is held
require.False(t, m.tryExclusiveLock("foo"))
m.sharedUnlock("foo")
require.False(t, m.tryExclusiveLock("foo"))
m.sharedUnlock("foo")
require.False(t, m.tryExclusiveLock("foo"))
m.sharedUnlock("foo")
// now exclusive lock can be acquired
require.True(t, m.tryExclusiveLock("foo"))
}