Skip to content

Commit

Permalink
fix: kvsmock race condition (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Ferenc Sárai <[email protected]>
  • Loading branch information
2 people authored and aranyia committed Jul 25, 2024
1 parent 46c7fa0 commit ba7effe
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/accesscontrol/kvs/mock/kvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

var lock = &sync.Mutex{}
var lockGetPut = &sync.Mutex{}

type single struct {
memoryMock map[string]map[string][]byte
Expand Down Expand Up @@ -51,12 +52,16 @@ type mockKeyValueStore struct {
var _ kvs.KeyValueStore = (*mockKeyValueStore)(nil)

func (m *mockKeyValueStore) Get(_ context.Context, key []byte) ([]byte, error) {
lockGetPut.Lock()
defer lockGetPut.Unlock()
mem := getMemory()
val := mem[m.address.String()][hex.EncodeToString(key)]
return val, nil
}

func (m *mockKeyValueStore) Put(_ context.Context, key []byte, value []byte) error {
lockGetPut.Lock()
defer lockGetPut.Unlock()
mem := getMemory()
if _, ok := mem[m.address.String()]; !ok {
mem[m.address.String()] = make(map[string][]byte)
Expand Down

0 comments on commit ba7effe

Please sign in to comment.