mirror of
https://github.com/kopia/kopia.git
synced 2026-03-07 15:58:25 -05:00
* 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>
51 lines
1.2 KiB
Go
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"))
|
|
}
|