Skip to content

Commit

Permalink
fix: concurrent map iteration and map write
Browse files Browse the repository at this point in the history
Signed-off-by: Cookie Wang <[email protected]>
  • Loading branch information
luckymrwang committed Jul 12, 2023
1 parent 0610339 commit 0a000f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (r *Memory) Current() (*Counts, error) {
r.mut.RLock()
defer r.mut.RUnlock()
cts := NewCounts()
cts.Counts = r.countMap
for key, val := range r.countMap {
cts.Counts[key] = val
}
return cts, nil
}
21 changes: 21 additions & 0 deletions pkg/queue/queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package queue

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestCurrent(t *testing.T) {
r := require.New(t)
memory := NewMemory()
memory.Resize("host1", 1)
memory.Resize("host2", 1)
current, err := memory.Current()
r.NoError(err)
r.Equal(current.Counts, memory.countMap)

memory.Resize("host1", 1)
memory.Resize("host3", 1)
r.NotEqual(current.Counts, memory.countMap)
}

0 comments on commit 0a000f8

Please sign in to comment.