Skip to content

Commit

Permalink
memstore: add unit tests for clear
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen committed Jan 9, 2024
1 parent 1890be5 commit 29a280a
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions internal/memstore/memstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ func TestStore(t *testing.T) {
assert.Contains(values, 1)
assert.Contains(values, 2)
})

t.Run("clear elements", func(t *testing.T) {
assert := assert.New(t)

s := memstore.New[string, int]()
s.Set("foo", 1)
s.Set("bar", 2)

s.Clear()
assert.Empty(s.GetAll())
})

t.Run("clear empty", func(t *testing.T) {
assert := assert.New(t)

s := memstore.New[string, int]()
s.Clear()
assert.Empty(s.GetAll())
})
}

func TestStoreConcurrent(t *testing.T) {
Expand Down Expand Up @@ -143,4 +162,47 @@ func TestStoreConcurrent(t *testing.T) {

assert.Len(s.GetAll(), 4)
})

t.Run("clear", func(t *testing.T) {
assert := assert.New(t)

s := memstore.New[string, int]()

var wg sync.WaitGroup

set := func(key string, value int) {
defer wg.Done()
s.Set(key, value)
}
clear := func() {
defer wg.Done()
s.Clear()
}
getAll := func() {
defer wg.Done()
_ = s.GetAll()
}

wg.Add(16)
go clear()
go clear()
go clear()
go clear()
go set("foo", 1)
go set("bar", 2)
go set("baz", 3)
go set("pil", 4)
go getAll()
go getAll()
go getAll()
go getAll()
go clear()
go clear()
go clear()
go clear()
wg.Wait()

s.Clear()
assert.Empty(s.GetAll())
})
}

0 comments on commit 29a280a

Please sign in to comment.