Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.25.0

require (
github.com/cucumber/godog v0.15.1
github.com/go-logr/logr v1.4.3
github.com/stretchr/testify v1.11.1
go.uber.org/goleak v1.3.0
go.uber.org/mock v0.6.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ github.com/cucumber/messages/go/v22 v22.0.0/go.mod h1:aZipXTKc0JnjCsXrJnuZpWhtay
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
Expand Down
34 changes: 5 additions & 29 deletions openfeature/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"slices"
"sync"
"unicode/utf8"

"github.com/go-logr/logr"
)

// ClientMetadata provides a client's metadata
Expand Down Expand Up @@ -38,7 +36,7 @@ func (cm ClientMetadata) Domain() string {

// Client implements the behaviour required of an openfeature client
type Client struct {
api evaluationImpl
providerBinding providerBindingFn
clientEventing clientEvent
metadata ClientMetadata
hooks []Hook
Expand All @@ -51,38 +49,16 @@ type Client struct {
// interface guard to ensure that Client implements IClient
var _ IClient = (*Client)(nil)

// NewClient returns a new Client. Name is a unique identifier for this client
// This helper exists for historical reasons. It is recommended to interact with IEvaluation to derive IClient instances.
// NewClient returns a new [Client] bound to the provider registered for the given domain.
func NewClient(domain string) *Client {
apiRef := api()
return newClient(domain, apiRef, apiRef.eventExecutor)
}

func newClient(domain string, apiRef evaluationImpl, eventRef clientEvent) *Client {
return &Client{
domain: domain,
api: apiRef,
clientEventing: eventRef,
metadata: ClientMetadata{domain: domain},
hooks: []Hook{},
evaluationContext: EvaluationContext{},
}
return api().NewClient(WithDomain(domain))
}

// State returns the state of the associated provider
func (c *Client) State() State {
return c.clientEventing.State(c.domain)
}

// WithLogger sets the logger of the client
//
// Deprecated: use [github.com/open-feature/go-sdk/openfeature/hooks.LoggingHook] instead.
func (c *Client) WithLogger(l logr.Logger) *Client {
c.mx.Lock()
defer c.mx.Unlock()
return c
}

// Metadata returns the client's metadata
func (c *Client) Metadata() ClientMetadata {
c.mx.RLock()
Expand Down Expand Up @@ -666,7 +642,7 @@ func (c *Client) Track(ctx context.Context, trackingEventName string, evalCtx Ev
// - client
// - invocation (highest precedence)
func (c *Client) forTracking(ctx context.Context, evalCtx EvaluationContext) (Tracker, EvaluationContext) {
provider, _, globalEvalCtx := c.api.ForEvaluation(c.metadata.domain)
provider, _, globalEvalCtx := c.providerBinding(c.metadata.domain)
evalCtx = mergeContexts(evalCtx, c.evaluationContext, TransactionContext(ctx), globalEvalCtx)
trackingProvider, ok := provider.(Tracker)
if !ok {
Expand All @@ -691,7 +667,7 @@ func (c *Client) evaluate(
}

// ensure that the same provider & hooks are used across this transaction to avoid unexpected behaviour
provider, globalHooks, globalEvalCtx := c.api.ForEvaluation(c.metadata.domain)
provider, globalHooks, globalEvalCtx := c.providerBinding(c.metadata.domain)

evalCtx = mergeContexts(evalCtx, c.evaluationContext, TransactionContext(ctx), globalEvalCtx) // API (global) -> transaction -> client -> invocation
hooks := slices.Concat(globalHooks, c.hooks, options.hooks, provider.Hooks()) // API, Client, Invocation, Provider
Expand Down
81 changes: 49 additions & 32 deletions openfeature/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"reflect"
"sync/atomic"
"testing"
"time"

Expand All @@ -12,27 +13,34 @@ import (

type clientMocks struct {
clientHandlerAPI *MockclientEvent
evaluationAPI *MockevaluationImpl
providerBinding providerBindingFn
providerAPI *MockFeatureProvider
}

func hydratedMocksForClientTests(t *testing.T, expectedEvaluations int) clientMocks {
ctrl := gomock.NewController(t)
mockClientAPI := NewMockclientEvent(ctrl)
mockEvaluationAPI := NewMockevaluationImpl(ctrl)
mockProvider := NewMockFeatureProvider(ctrl)

mockClientAPI.EXPECT().State(gomock.Any()).AnyTimes().Return(ReadyState)

mockProvider.EXPECT().Metadata().AnyTimes()
mockProvider.EXPECT().Hooks().AnyTimes()
mockEvaluationAPI.EXPECT().ForEvaluation(gomock.Any()).Times(expectedEvaluations).DoAndReturn(func(_ string) (*MockFeatureProvider, []Hook, EvaluationContext) {
var callCount atomic.Int64
mockBindFn := func(string) (FeatureProvider, []Hook, EvaluationContext) {
callCount.Add(1)
return mockProvider, nil, EvaluationContext{}
}

t.Cleanup(func() {
if got := callCount.Load(); got != int64(expectedEvaluations) {
t.Errorf("expected %d resolver calls, got %d", expectedEvaluations, got)
}
})

return clientMocks{
clientHandlerAPI: mockClientAPI,
evaluationAPI: mockEvaluationAPI,
providerBinding: mockBindFn,
providerAPI: mockProvider,
}
}
Expand Down Expand Up @@ -163,7 +171,7 @@ func TestRequirement_1_4_2__1_4_5__1_4_6(t *testing.T) {

t.Run("BooleanValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: booleanValue,
Expand All @@ -184,7 +192,7 @@ func TestRequirement_1_4_2__1_4_5__1_4_6(t *testing.T) {

t.Run("StringValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().StringEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(StringResolutionDetail{
Value: stringValue,
Expand All @@ -211,7 +219,7 @@ func TestRequirement_1_4_2__1_4_5__1_4_6(t *testing.T) {

t.Run("FloatValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().FloatEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(FloatResolutionDetail{
Value: floatValue,
Expand All @@ -238,7 +246,7 @@ func TestRequirement_1_4_2__1_4_5__1_4_6(t *testing.T) {

t.Run("IntValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().IntEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(IntResolutionDetail{
Value: intValue,
Expand All @@ -265,7 +273,7 @@ func TestRequirement_1_4_2__1_4_5__1_4_6(t *testing.T) {

t.Run("ObjectValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().ObjectEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(InterfaceResolutionDetail{
Value: objectValue,
Expand Down Expand Up @@ -300,7 +308,7 @@ func TestRequirement_1_4_4(t *testing.T) {
flagKey := "foo"
t.Run("BooleanValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: booleanValue,
Expand All @@ -323,7 +331,7 @@ func TestRequirement_1_4_4(t *testing.T) {

t.Run("StringValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().StringEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(StringResolutionDetail{
Value: stringValue,
Expand All @@ -346,7 +354,7 @@ func TestRequirement_1_4_4(t *testing.T) {

t.Run("FloatValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().FloatEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(FloatResolutionDetail{
Value: floatValue,
Expand All @@ -369,7 +377,7 @@ func TestRequirement_1_4_4(t *testing.T) {

t.Run("IntValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().IntEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(IntResolutionDetail{
Value: intValue,
Expand All @@ -392,7 +400,7 @@ func TestRequirement_1_4_4(t *testing.T) {

t.Run("ObjectValueDetails", func(t *testing.T) {
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().ObjectEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(InterfaceResolutionDetail{
Value: objectValue,
Expand All @@ -419,7 +427,7 @@ func TestRequirement_1_4_4(t *testing.T) {
func TestRequirement_1_4_7(t *testing.T) {
t.Cleanup(resetSingleton)
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)

mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Expand Down Expand Up @@ -447,7 +455,7 @@ func TestRequirement_1_4_7(t *testing.T) {
func TestRequirement_1_4_8(t *testing.T) {
t.Cleanup(resetSingleton)
mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: false,
Expand Down Expand Up @@ -485,7 +493,7 @@ func TestRequirement_1_4_9(t *testing.T) {
t.Cleanup(resetSingleton)

mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)

defaultValue := true
mocks.providerAPI.EXPECT().BooleanEvaluation(t.Context(), flagKey, defaultValue, flatCtx).
Expand Down Expand Up @@ -519,7 +527,7 @@ func TestRequirement_1_4_9(t *testing.T) {
t.Cleanup(resetSingleton)

mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)

defaultValue := "default"
mocks.providerAPI.EXPECT().StringEvaluation(t.Context(), flagKey, defaultValue, flatCtx).
Expand Down Expand Up @@ -552,7 +560,7 @@ func TestRequirement_1_4_9(t *testing.T) {
t.Run("Float", func(t *testing.T) {
t.Cleanup(resetSingleton)
mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)

defaultValue := 3.14159
mocks.providerAPI.EXPECT().FloatEvaluation(t.Context(), flagKey, defaultValue, flatCtx).
Expand Down Expand Up @@ -585,7 +593,7 @@ func TestRequirement_1_4_9(t *testing.T) {
t.Run("Int", func(t *testing.T) {
t.Cleanup(resetSingleton)
mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
var defaultValue int64 = 3
mocks.providerAPI.EXPECT().IntEvaluation(t.Context(), flagKey, defaultValue, flatCtx).
Return(IntResolutionDetail{
Expand Down Expand Up @@ -617,7 +625,7 @@ func TestRequirement_1_4_9(t *testing.T) {
t.Run("Object", func(t *testing.T) {
t.Cleanup(resetSingleton)
mocks := hydratedMocksForClientTests(t, 2)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
type obj struct {
foo string
}
Expand Down Expand Up @@ -664,7 +672,7 @@ func TestRequirement_1_4_12(t *testing.T) {
errMessage := "error forced by test"

mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: true,
Expand Down Expand Up @@ -700,7 +708,7 @@ func TestRequirement_1_4_13(t *testing.T) {
t.Cleanup(resetSingleton)

mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
defaultValue := true
mocks.providerAPI.EXPECT().BooleanEvaluation(t.Context(), flagKey, defaultValue, flatCtx).
Return(BoolResolutionDetail{
Expand All @@ -726,7 +734,7 @@ func TestRequirement_1_4_13(t *testing.T) {
t.Cleanup(resetSingleton)

mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
defaultValue := true
metadata := FlagMetadata{
"bing": "bong",
Expand Down Expand Up @@ -882,13 +890,11 @@ func TestTrack(t *testing.T) {
t.Run(name, func(t *testing.T) {
// arrange
mocks := hydratedMocksForClientTests(t, 0)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)

provider := test.provider(test, mocks.providerAPI)

mocks.evaluationAPI.EXPECT().ForEvaluation("test-client").AnyTimes().DoAndReturn(func(_ string) (FeatureProvider, []Hook, EvaluationContext) {
resolver := func(_ string) (FeatureProvider, []Hook, EvaluationContext) {
return provider, nil, test.inCtx.api
})
}
client := newClient("test-client", resolver, mocks.clientHandlerAPI)
client.evaluationContext = test.inCtx.client
ctx := WithTransactionContext(t.Context(), test.inCtx.txn)

Expand Down Expand Up @@ -983,7 +989,7 @@ func TestBeforeHookNilContext(t *testing.T) {
hookNilContext := UnimplementedHook{}

mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)

attributes := map[string]any{"should": "persist"}
evalCtx := EvaluationContext{attributes: attributes}
Expand All @@ -1003,7 +1009,7 @@ func TestErrorCodeFromProviderReturnedInEvaluationDetails(t *testing.T) {
generalErrorCode := GeneralCode

mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().BooleanEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(BoolResolutionDetail{
Value: true,
Expand Down Expand Up @@ -1035,7 +1041,7 @@ func TestObjectEvaluationShouldSupportNilValue(t *testing.T) {
var value any = nil

mocks := hydratedMocksForClientTests(t, 1)
client := newClient("test-client", mocks.evaluationAPI, mocks.clientHandlerAPI)
client := newClient("test-client", mocks.providerBinding, mocks.clientHandlerAPI)
mocks.providerAPI.EXPECT().ObjectEvaluation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(InterfaceResolutionDetail{
Value: value,
Expand Down Expand Up @@ -1455,3 +1461,14 @@ func TestRequirement_5_3_5(t *testing.T) {
return NewClient(t.Name()).State() == FatalState
}, time.Second, 100*time.Millisecond, "expected client to report FATAL state")
}

func newClient(domain string, providerBinding providerBindingFn, eventRef clientEvent) *Client {
return &Client{
domain: domain,
providerBinding: providerBinding,
clientEventing: eventRef,
metadata: ClientMetadata{domain: domain},
hooks: []Hook{},
evaluationContext: EvaluationContext{},
}
}
Loading
Loading