Skip to content

Commit

Permalink
Change client initialization to be lazy
Browse files Browse the repository at this point in the history
This reduces side effects of importing this pkg (due to the init func)
  • Loading branch information
fabiante committed Dec 18, 2023
1 parent 6032201 commit b45594c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
39 changes: 27 additions & 12 deletions eastest/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,42 @@ import (
"errors"
"fmt"
"os"
"sync"

"github.com/DEXPRO-Solutions-GmbH/easclient"
"github.com/joho/godotenv"
"gopkg.in/resty.v1"
)

var (
DefaultClient *easclient.StoreClient
DefaultServerClient *easclient.ServerClient
defaultClient *easclient.StoreClient
defaultClientOnce sync.Once
defaultServerClient *easclient.ServerClient
defaultServerOnce sync.Once
)

func DefaultClient() *easclient.StoreClient {
defaultClientOnce.Do(func() {
client := resty.New()
client.SetHostURL(fmt.Sprintf("http://%s/eas/archives/%s", os.Getenv("EAS_HOST"), os.Getenv("EAS_STORE")))
client.SetBasicAuth(os.Getenv("EAS_USER"), os.Getenv("EAS_PASSWORD"))
defaultClient = easclient.NewStoreClient(client)
})

return defaultClient
}

func DefaultServerClient() *easclient.ServerClient {
defaultServerOnce.Do(func() {
serverClient := resty.New()
serverClient.SetHostURL(fmt.Sprintf("http://%s/eas/archives", os.Getenv("EAS_HOST")))
serverClient.SetBasicAuth(os.Getenv("EAS_USER"), os.Getenv("EAS_PASSWORD"))
defaultServerClient = easclient.NewServerClient(serverClient)
})

return defaultServerClient
}

// init is run before any tests are executed.
// it loads the environment variables from .env and creates
// a default client for all tests to use.
Expand All @@ -23,14 +48,4 @@ func init() {
if errors.Is(err, os.ErrNotExist) {
return
}

client := resty.New()
client.SetHostURL(fmt.Sprintf("http://%s/eas/archives/%s", os.Getenv("EAS_HOST"), os.Getenv("EAS_STORE")))
client.SetBasicAuth(os.Getenv("EAS_USER"), os.Getenv("EAS_PASSWORD"))
DefaultClient = easclient.NewStoreClient(client)

serverClient := resty.New()
serverClient.SetHostURL(fmt.Sprintf("http://%s/eas/archives", os.Getenv("EAS_HOST")))
serverClient.SetBasicAuth(os.Getenv("EAS_USER"), os.Getenv("EAS_PASSWORD"))
DefaultServerClient = easclient.NewServerClient(serverClient)
}
2 changes: 1 addition & 1 deletion r_get_record_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestStoreClient_GetRecordAttachment(t *testing.T) {

buffer := new(bytes.Buffer)

record, err := eastest.DefaultClient.GetRecordAttachment(
record, err := eastest.DefaultClient().GetRecordAttachment(
ctx,
buffer,
uuid.MustParse("a65efcf9-8c74-4b84-8106-233c1c64a07c"),
Expand Down
2 changes: 1 addition & 1 deletion r_get_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestStoreClient_GetRecord(t *testing.T) {
user := easclient.NewUserClaims("[email protected]")
ctx = user.SetOnContext(ctx)

record, err := eastest.DefaultClient.GetRecord(ctx, uuid.MustParse("990ac5bf-1df5-45b8-82ca-41120621f826"))
record, err := eastest.DefaultClient().GetRecord(ctx, uuid.MustParse("990ac5bf-1df5-45b8-82ca-41120621f826"))
require.NoError(t, err)
require.NotNil(t, record)
}
2 changes: 1 addition & 1 deletion r_get_store_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestStoreClient_GetStoreStatus(t *testing.T) {
user := easclient.NewUserClaims("[email protected]")
ctx = user.SetOnContext(ctx)

status, err := eastest.DefaultClient.GetStoreStatus(ctx)
status, err := eastest.DefaultClient().GetStoreStatus(ctx)
require.NoError(t, err)
require.NotNil(t, status)
}
2 changes: 1 addition & 1 deletion r_put_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestStoreClient_PutStore(t *testing.T) {

storeName := "random-store"

err := eastest.DefaultServerClient.PutStore(ctx, storeName, &easclient.PutStoreRequest{
err := eastest.DefaultServerClient().PutStore(ctx, storeName, &easclient.PutStoreRequest{
ConfigurationTemplate: easclient.ConfigurationTemplate{
Name: "default",
Parameters: []easclient.ConfigurationParameter{
Expand Down

0 comments on commit b45594c

Please sign in to comment.