Files
kopia/internal/cache/mutex_map_test.go
Jarek Kowalski fe55dcb6a2 feat(repository): added hard size limit to the on-disk cache (#3238)
* test(providers): added capacity limits to blobtesting.mapStorage

* refactor(general): added mutex map which dynamically allocates and releases named mutexes

* refactor(repository): refactored cache cleanup and limit enforcement

* refactor(repository): plumb through cache size limits in the repository

* feat(cli): added CLI options to set cache size limits

* unified flag setting and field naming

* Update cli/command_cache_set.go

Co-authored-by: Shikhar Mall <mall.shikhar.in@gmail.com>

* pr feedback

---------

Co-authored-by: Shikhar Mall <mall.shikhar.in@gmail.com>
2023-08-24 09:38:56 -07: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.Len(t, m.entries, 0)
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.Len(t, m.entries, 0)
}
func TestMutexMap_SharedLock(t *testing.T) {
var m mutexMap
require.Len(t, m.entries, 0)
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"))
}