Skip to content

Commit

Permalink
add CHash.RemoveAllGroup()
Browse files Browse the repository at this point in the history
add CHash.RemoveAllGroup()
  • Loading branch information
werbenhu committed Apr 14, 2023
1 parent 63aeee4 commit 888dde9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions chash.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func (c *CHash) RemoveGroup(groupName string) {
delete(c.groups, groupName)
}

func (c *CHash) RemoveAllGroup() {
c.Lock()
defer c.Unlock()

for k := range c.groups {
delete(c.groups, k)
}
}

func (c *CHash) Insert(groupName string, key string, payload []byte) error {
c.Lock()
group, ok := c.groups[groupName]
Expand Down
15 changes: 15 additions & 0 deletions chash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ func TestCHashRemoveGroup(t *testing.T) {
assert.Equal(t, ErrGroupNotFound, err)
}

func TestCHashRemoveAllGroup(t *testing.T) {
hash := New()
group1, err := hash.CreateGroup("werbenhu1", 2000)
assert.Nil(t, err)
assert.NotNil(t, group1)

group2, err := hash.CreateGroup("werbenhu2", 1000)
assert.Nil(t, err)
assert.NotNil(t, group2)

assert.Equal(t, 2, len(hash.groups))
hash.RemoveAllGroup()
assert.Equal(t, 0, len(hash.groups))
}

func TestCHashInsert(t *testing.T) {
hash := New()
hash.CreateGroup("werbenhu1", 10000)
Expand Down

0 comments on commit 888dde9

Please sign in to comment.