Skip to content

Commit

Permalink
chore: cleanup FGA server resources (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Jan 30, 2024
2 parents f604f71 + d56af5d commit 9552180
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 13 additions & 6 deletions internal/storetest/localstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,42 @@ func initLocalStore(
func getLocalServerModelAndTuples(
storeData *StoreData,
format authorizationmodel.ModelFormat,
) (*server.Server, *authorizationmodel.AuthzModel, error) {
) (*server.Server, *authorizationmodel.AuthzModel, func(), error) {
var fgaServer *server.Server

var authModel *authorizationmodel.AuthzModel

stopServerFn := func() {}

if storeData.Model == "" {
return fgaServer, authModel, nil
return fgaServer, authModel, stopServerFn, nil
}

// If we have at least one local test, initialize the local server
datastore := memory.New()

fgaServer, err := server.NewServerWithOpts(server.WithDatastore(datastore))
if err != nil {
return nil, nil, err //nolint:wrapcheck
return nil, nil, stopServerFn, err //nolint:wrapcheck
}

tempModel := authorizationmodel.AuthzModel{}
if format == authorizationmodel.ModelFormatJSON {
if err := tempModel.ReadFromJSONString(storeData.Model); err != nil {
return nil, nil, err //nolint:wrapcheck
return nil, nil, stopServerFn, err //nolint:wrapcheck
}
} else {
if err := tempModel.ReadFromDSLString(storeData.Model); err != nil {
return nil, nil, err //nolint:wrapcheck
return nil, nil, stopServerFn, err //nolint:wrapcheck
}
}

authModel = &tempModel

return fgaServer, authModel, nil
stopServerFn = func() {
datastore.Close()
fgaServer.Close()
}

return fgaServer, authModel, stopServerFn, nil
}
4 changes: 3 additions & 1 deletion internal/storetest/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ func RunTests(
) (TestResults, error) {
test := TestResults{}

fgaServer, authModel, err := getLocalServerModelAndTuples(storeData, format)
fgaServer, authModel, stopServerFn, err := getLocalServerModelAndTuples(storeData, format)
if err != nil {
return test, err
}

defer stopServerFn()

for index := 0; index < len(storeData.Tests); index++ {
result, err := RunTest(
fgaClient,
Expand Down

0 comments on commit 9552180

Please sign in to comment.