Skip to content

Commit

Permalink
test client: prevent races with mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmueller committed Oct 27, 2023
1 parent 76c8c17 commit b6e4c72
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/client/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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{}
Expand Down

0 comments on commit b6e4c72

Please sign in to comment.