Skip to content

Commit

Permalink
Adds ItemCount()
Browse files Browse the repository at this point in the history
Signed-off-by: Elena Kolevska <[email protected]>
  • Loading branch information
elena-kolevska committed May 16, 2024
1 parent 1715b0a commit f73cbfb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 7 additions & 0 deletions concurrency/mutexmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type MutexMap[T comparable] interface {
RUnlock(key T)
Delete(key T)
Clear()
ItemCount() int
}

type mutexMap[T comparable] struct {
Expand Down Expand Up @@ -98,3 +99,9 @@ func (a *mutexMap[T]) Clear() {
a.items = make(map[T]*sync.RWMutex)
a.lock.Unlock()
}

func (a *mutexMap[T]) ItemCount() int {
a.lock.Lock()
defer a.lock.Unlock()
return len(a.items)
}
5 changes: 4 additions & 1 deletion concurrency/mutexmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ func TestNewMutexMap_Add_Delete(t *testing.T) {
require.False(t, ok)
})

t.Run("Clear all mutexes", func(t *testing.T) {
t.Run("Clear all mutexes, and check item count", func(t *testing.T) {
mm.Lock("key1")
mm.Unlock("key1")
mm.Lock("key2")
mm.Unlock("key2")

require.Equal(t, 2, mm.ItemCount())

mm.Clear()
require.Empty(t, mm.items)
})
Expand Down

0 comments on commit f73cbfb

Please sign in to comment.