From b6e4c722fdf014895f09c479ee1fad77105a2860 Mon Sep 17 00:00:00 2001 From: franzmueller Date: Fri, 27 Oct 2023 08:23:07 +0200 Subject: [PATCH] test client: prevent races with mutex --- lib/client/test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/client/test.go b/lib/client/test.go index fbea1d1..2cff65b 100644 --- a/lib/client/test.go +++ b/lib/client/test.go @@ -19,17 +19,21 @@ package client import ( "errors" "github.com/SENERGY-Platform/permission-search/lib/model" + "sync" ) type TestClient struct { resourceRights map[string]map[string]model.ResourceRights + mux sync.Mutex } func NewTestClient() *TestClient { - return &TestClient{resourceRights: map[string]map[string]model.ResourceRights{}} + return &TestClient{resourceRights: map[string]map[string]model.ResourceRights{}, mux: sync.Mutex{}} } func (this *TestClient) GetRights(_ string, resource string, id string) (result model.ResourceRights, err error) { + this.mux.Lock() + defer this.mux.Unlock() resources, ok := this.resourceRights[resource] if !ok { return model.ResourceRights{}, errors.New("not found") @@ -42,6 +46,8 @@ func (this *TestClient) GetRights(_ string, resource string, id string) (result } func (this *TestClient) SetRights(resource string, id string, rights model.ResourceRights) { + this.mux.Lock() + defer this.mux.Unlock() resources, ok := this.resourceRights[resource] if !ok { resources = map[string]model.ResourceRights{}