Skip to content

Commit 056e169

Browse files
authored
Merge pull request #601 from ginglis13/cache-list
Add List interface to snapshotter cache
2 parents 0531356 + 1225efd commit 056e169

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

snapshotter/demux/cache/cache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ type Cache interface {
3333

3434
// Releases the cache's internal resources and closes any cached snapshotters.
3535
Close() error
36+
37+
// Lists keys present in the cache.
38+
List() []string
3639
}

snapshotter/demux/cache/snapshotter_cache.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import (
2525
// SnapshotterCache implements a read, write protected cache mechanism
2626
// for keyed snapshotters.
2727
type SnapshotterCache struct {
28-
mutex *sync.Mutex
28+
mutex *sync.RWMutex
2929
snapshotters map[string]snapshots.Snapshotter
3030
}
3131

3232
// NewSnapshotterCache creates a new instance with an empty cache.
3333
func NewSnapshotterCache() *SnapshotterCache {
34-
return &SnapshotterCache{&sync.Mutex{}, make(map[string]snapshots.Snapshotter)}
34+
return &SnapshotterCache{&sync.RWMutex{}, make(map[string]snapshots.Snapshotter)}
3535
}
3636

3737
// Get fetches and caches the snapshotter for a given key.
@@ -58,7 +58,9 @@ func (cache *SnapshotterCache) Get(ctx context.Context, key string, fetch Snapsh
5858

5959
// Evict removes a cached snapshotter for a given key.
6060
func (cache *SnapshotterCache) Evict(key string) error {
61+
cache.mutex.RLock()
6162
snapshotter, ok := cache.snapshotters[key]
63+
cache.mutex.RUnlock()
6264

6365
if !ok {
6466
return fmt.Errorf("snapshotter %s not found in cache", key)
@@ -81,3 +83,15 @@ func (cache *SnapshotterCache) Close() error {
8183
}
8284
return compiledErr
8385
}
86+
87+
// List returns keys of a snapshotter cache.
88+
func (cache *SnapshotterCache) List() []string {
89+
cache.mutex.RLock()
90+
defer cache.mutex.RUnlock()
91+
keys := make([]string, 0, len(cache.snapshotters))
92+
for k := range cache.snapshotters {
93+
keys = append(keys, k)
94+
}
95+
96+
return keys
97+
}

snapshotter/demux/cache/snapshotter_cache_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,29 @@ func closeCacheReturnsAllOccurringErrors(uut *SnapshotterCache) error {
124124
return nil
125125
}
126126

127+
func listEmptyCache(uut *SnapshotterCache) error {
128+
keys := uut.List()
129+
if len(keys) > 0 {
130+
return errors.New("List did not return an empty list for an empty snapshotter cache")
131+
}
132+
133+
return nil
134+
}
135+
136+
func listNonEmptyCache(uut *SnapshotterCache) error {
137+
uut.Get(context.Background(), "OkSnapshotterKey", getSnapshotterOkFunction)
138+
139+
keys := uut.List()
140+
if len(keys) != 1 {
141+
return errors.New("List should return a non-empty list of keys")
142+
}
143+
if keys[0] != "OkSnapshotterKey" {
144+
return errors.New("List did not return correct list of keys")
145+
}
146+
147+
return nil
148+
}
149+
127150
func TestGetSnapshotterFromCache(t *testing.T) {
128151
t.Parallel()
129152

@@ -189,3 +212,25 @@ func TestCloseCache(t *testing.T) {
189212
})
190213
}
191214
}
215+
216+
func TestListSnapshotters(t *testing.T) {
217+
t.Parallel()
218+
219+
tests := []struct {
220+
name string
221+
run func(*SnapshotterCache) error
222+
}{
223+
{"ListEmptyCache", listEmptyCache},
224+
{"ListNonEmptyCache", listNonEmptyCache},
225+
}
226+
227+
for _, test := range tests {
228+
t.Run(test.name, func(t *testing.T) {
229+
uut := NewSnapshotterCache()
230+
if err := test.run(uut); err != nil {
231+
t.Fatal(test.name + ": " + err.Error())
232+
}
233+
})
234+
}
235+
236+
}

0 commit comments

Comments
 (0)