Documentation
¶
Overview ¶
Package sharedtest contains types and functions used by SDK unit tests in multiple packages.
Since it is inside internal/, none of this code can be seen by application code and it can be freely changed without breaking any public APIs. Test helpers that we want to be available to application code should be in testhelpers/ instead.
It is important that no non-test code ever imports this package, so that it will not be compiled into applications as a transitive dependency.
Note that this package is not allowed to reference the "internal" package, because the tests in that package use sharedtest helpers so it would be a circular reference.
Index ¶
- Constants
- func BasicClientContext() subsystems.ClientContext
- func DataSetToMap(allData []ldstoretypes.Collection) map[ldstoretypes.DataKind]map[string]ldstoretypes.ItemDescriptor
- func ExpectFlagChangeEvents(t *testing.T, ch <-chan interfaces.FlagChangeEvent, keys ...string)
- func FirstClauseValues(flag *ldmodel.FeatureFlag) []ldvalue.Value
- func FirstSegmentClauseValues(segment *ldmodel.Segment) []ldvalue.Value
- func FlagDescriptor(f ldmodel.FeatureFlag) ldstoretypes.ItemDescriptor
- func FlagWithManyInClauseValues(key string, version int) ldmodel.FeatureFlag
- func ManyInClauseValues() []ldvalue.Value
- func NewSimpleTestContext(sdkKey string) subsystems.ClientContext
- func NewTestContext(sdkKey string, optHTTPConfig *subsystems.HTTPConfiguration, ...) subsystems.BasicClientContext
- func NewTestLoggers() ldlog.Loggers
- func NormalizeDataSet(in []ldstoretypes.Collection) []ldstoretypes.Collection
- func SegmentDescriptor(s ldmodel.Segment) ldstoretypes.ItemDescriptor
- func SegmentWithManyInClauseValues(key string, version int) ldmodel.Segment
- func TestLoggingConfig() subsystems.LoggingConfiguration
- type DataSetBuilder
- type HookEvalCapture
- type HookExpectedCall
- type HookStage
- type HookTrackCapture
- type TestHook
- func (h TestHook) AfterEvaluation(ctx context.Context, seriesContext ldhooks.EvaluationSeriesContext, ...) (ldhooks.EvaluationSeriesData, error)
- func (h TestHook) AfterTrack(ctx context.Context, seriesContext ldhooks.TrackSeriesContext) error
- func (h TestHook) BeforeEvaluation(ctx context.Context, seriesContext ldhooks.EvaluationSeriesContext, ...) (ldhooks.EvaluationSeriesData, error)
- func (h TestHook) Metadata() ldhooks.Metadata
- func (h TestHook) Verify(t *testing.T, calls ...HookExpectedCall)
- func (h TestHook) VerifyNoCalls(t *testing.T)
- type TestOverrideSource
- func (t *TestOverrideSource) Build(context subsystems.ClientContext) (subsystems.OverrideSource, error)
- func (t *TestOverrideSource) Close() error
- func (t *TestOverrideSource) IsClosed() bool
- func (t *TestOverrideSource) IsStarted() bool
- func (t *TestOverrideSource) SetOverrides(data []ldstoretypes.Collection)
- func (t *TestOverrideSource) Start(sink subsystems.OverrideSink)
- type TestPlugin
- func (p TestPlugin) ExpectGetHooksCalled(t *testing.T, expectedMetadata ldplugins.EnvironmentMetadata)
- func (p TestPlugin) ExpectRegisterCalled(t *testing.T, expectedClient interfaces.LDClientInterface, ...)
- func (p TestPlugin) GetHooks(metadata ldplugins.EnvironmentMetadata) []ldhooks.Hook
- func (p TestPlugin) Metadata() ldplugins.Metadata
- func (p TestPlugin) Register(client interfaces.LDClientInterface, metadata ldplugins.EnvironmentMetadata)
- type TestPluginHook
- func (h TestPluginHook) AfterEvaluation(ctx context.Context, seriesContext ldhooks.EvaluationSeriesContext, ...) (ldhooks.EvaluationSeriesData, error)
- func (h TestPluginHook) BeforeEvaluation(ctx context.Context, seriesContext ldhooks.EvaluationSeriesContext, ...) (ldhooks.EvaluationSeriesData, error)
- func (h TestPluginHook) ExpectAfterEvaluationCalled(t *testing.T)
- func (h TestPluginHook) ExpectBeforeEvaluationCalled(t *testing.T)
- func (h TestPluginHook) Metadata() ldhooks.Metadata
Constants ¶
const ( // HookStageBeforeEvaluation is the stage executed before evaluation. HookStageBeforeEvaluation = HookStage("beforeEvaluation") // HookStageAfterEvaluation is the stage executed after evaluation. HookStageAfterEvaluation = HookStage("afterEvaluation") // HookStageAfterTrack is the stage executed after track. HookStageAfterTrack = HookStage("afterTrack") )
const TestSDKKey = "test-sdk-key"
TestSDKKey is a test SDK key that can be used in test code.
Variables ¶
This section is empty.
Functions ¶
func BasicClientContext ¶ added in v7.9.0
func BasicClientContext() subsystems.ClientContext
BasicClientContext returns a basic implementation of interfaces.ClientContext for use in test code.
func DataSetToMap ¶
func DataSetToMap( allData []ldstoretypes.Collection, ) map[ldstoretypes.DataKind]map[string]ldstoretypes.ItemDescriptor
DataSetToMap converts the data format for Init into a map of maps.
func ExpectFlagChangeEvents ¶
func ExpectFlagChangeEvents(t *testing.T, ch <-chan interfaces.FlagChangeEvent, keys ...string)
ExpectFlagChangeEvents asserts that a channel receives flag change events for the specified keys (in any order) and then does not receive any more events for the next 100ms.
func FirstClauseValues ¶ added in v7.19.0
func FirstClauseValues(flag *ldmodel.FeatureFlag) []ldvalue.Value
FirstClauseValues returns the Values list of the first clause in the first rule of the flag.
func FirstSegmentClauseValues ¶ added in v7.19.0
FirstSegmentClauseValues returns the Values list of the first clause in the first rule of the segment.
func FlagDescriptor ¶
func FlagDescriptor(f ldmodel.FeatureFlag) ldstoretypes.ItemDescriptor
FlagDescriptor is a shortcut for creating a StoreItemDescriptor from a flag.
func FlagWithManyInClauseValues ¶ added in v7.19.0
func FlagWithManyInClauseValues(key string, version int) ldmodel.FeatureFlag
FlagWithManyInClauseValues returns a flag with one rule. The rule has an "in" clause on the context key with the values of ManyInClauseValues, and it returns variation 0. Otherwise the flag returns variation 1.
func ManyInClauseValues ¶ added in v7.19.0
ManyInClauseValues returns enough values that an "in" clause with them gets a lookup set. If the SDK releases clause values, such a clause does not keep its Values list.
func NewSimpleTestContext ¶
func NewSimpleTestContext(sdkKey string) subsystems.ClientContext
NewSimpleTestContext returns a basic implementation of interfaces.ClientContext for use in test code.
func NewTestContext ¶
func NewTestContext( sdkKey string, optHTTPConfig *subsystems.HTTPConfiguration, optLoggingConfig *subsystems.LoggingConfiguration, ) subsystems.BasicClientContext
NewTestContext returns a basic implementation of interfaces.ClientContext for use in test code. We can't use internal.NewClientContextImpl for this because of circular references.
func NewTestLoggers ¶
NewTestLoggers returns a standardized logger instance used by unit tests. If you want to temporarily enable log output for tests, change testLogLevel to for instance ldlog.Debug. Note that "go test" normally suppresses output anyway unless a test fails.
func NormalizeDataSet ¶
func NormalizeDataSet(in []ldstoretypes.Collection) []ldstoretypes.Collection
NormalizeDataSet sorts the data set by kind and key for test determinacy.
func SegmentDescriptor ¶
func SegmentDescriptor(s ldmodel.Segment) ldstoretypes.ItemDescriptor
SegmentDescriptor is a shortcut for creating a StoreItemDescriptor from a segment.
func SegmentWithManyInClauseValues ¶ added in v7.19.0
SegmentWithManyInClauseValues returns a segment with one rule. The rule has an "in" clause on the context key with the values of ManyInClauseValues.
func TestLoggingConfig ¶
func TestLoggingConfig() subsystems.LoggingConfiguration
TestLoggingConfig returns a LoggingConfiguration corresponding to NewTestLoggers().
Types ¶
type DataSetBuilder ¶
type DataSetBuilder struct {
// contains filtered or unexported fields
}
DataSetBuilder is a helper for creating collections of flags and segments.
func NewDataSetBuilder ¶
func NewDataSetBuilder() *DataSetBuilder
NewDataSetBuilder creates a DataSetBuilder.
func (*DataSetBuilder) Build ¶
func (d *DataSetBuilder) Build() []ldstoretypes.Collection
Build returns the built data sest.
func (*DataSetBuilder) Flags ¶
func (d *DataSetBuilder) Flags(flags ...ldmodel.FeatureFlag) *DataSetBuilder
Flags adds flags to the data set.
func (*DataSetBuilder) Segments ¶
func (d *DataSetBuilder) Segments(segments ...ldmodel.Segment) *DataSetBuilder
Segments adds segments to the data set.
func (*DataSetBuilder) ToServerSDKData ¶
func (d *DataSetBuilder) ToServerSDKData() *ldservices.ServerSDKData
ToServerSDKData converts the data set to the format used by the ldservices helpers.
type HookEvalCapture ¶ added in v7.3.0
type HookEvalCapture struct {
GoContext context.Context
EvaluationSeriesContext ldhooks.EvaluationSeriesContext
EvaluationSeriesData ldhooks.EvaluationSeriesData
EvaluationDetail ldreason.EvaluationDetail
}
HookEvalCapture is used to capture the information provided to a hook during execution.
type HookExpectedCall ¶ added in v7.3.0
type HookExpectedCall struct {
HookStage HookStage
EvalCapture HookEvalCapture
TrackCapture HookTrackCapture
}
HookExpectedCall represents an expected call to a hook.
type HookStage ¶ added in v7.3.0
type HookStage string
HookStage is the stage of a hook being executed.
type HookTrackCapture ¶ added in v7.12.0
type HookTrackCapture struct {
GoContext context.Context
TrackSeriesContext ldhooks.TrackSeriesContext
}
HookTrackCapture is used to capture the information provided to a hook during execution.
type TestHook ¶ added in v7.3.0
type TestHook struct {
ldhooks.Unimplemented
BeforeEvaluationInject func(context.Context, ldhooks.EvaluationSeriesContext,
ldhooks.EvaluationSeriesData) (ldhooks.EvaluationSeriesData, error)
AfterEvaluationInject func(context.Context, ldhooks.EvaluationSeriesContext,
ldhooks.EvaluationSeriesData, ldreason.EvaluationDetail) (ldhooks.EvaluationSeriesData, error)
AfterTrackInject func(context.Context, ldhooks.TrackSeriesContext) error
// contains filtered or unexported fields
}
TestHook is a hook for testing to be used only by the SDK tests.
func NewTestHook ¶ added in v7.3.0
NewTestHook creates a new test hook.
func (TestHook) AfterEvaluation ¶ added in v7.3.0
func (h TestHook) AfterEvaluation( ctx context.Context, seriesContext ldhooks.EvaluationSeriesContext, data ldhooks.EvaluationSeriesData, detail ldreason.EvaluationDetail, ) (ldhooks.EvaluationSeriesData, error)
AfterEvaluation testing implementation of the AfterEvaluation stage.
func (TestHook) AfterTrack ¶ added in v7.12.0
AfterTrack testing implementation of the AfterTrack stage.
func (TestHook) BeforeEvaluation ¶ added in v7.3.0
func (h TestHook) BeforeEvaluation( ctx context.Context, seriesContext ldhooks.EvaluationSeriesContext, data ldhooks.EvaluationSeriesData, ) (ldhooks.EvaluationSeriesData, error)
BeforeEvaluation testing implementation of the BeforeEvaluation stage.
func (TestHook) Verify ¶ added in v7.3.0
func (h TestHook) Verify(t *testing.T, calls ...HookExpectedCall)
Verify is used to verify that the hook received calls it expected.
func (TestHook) VerifyNoCalls ¶ added in v7.3.0
VerifyNoCalls will assert if the hook has received any calls.
type TestOverrideSource ¶ added in v7.18.0
type TestOverrideSource struct {
// contains filtered or unexported fields
}
TestOverrideSource is a simple programmatic OverrideSource for testing the override system without any file machinery. It also serves as a reference for the seam an override source implements. Start delivers the initial data synchronously. SetOverrides pushes replacement snapshots to the sink at any time afterward.
func NewTestOverrideSource ¶ added in v7.18.0
func NewTestOverrideSource(initialData []ldstoretypes.Collection) *TestOverrideSource
NewTestOverrideSource creates a TestOverrideSource that will supply the given data as soon as it is started. A nil initialData means the source supplies nothing until SetOverrides is called.
func (*TestOverrideSource) Build ¶ added in v7.18.0
func (t *TestOverrideSource) Build(context subsystems.ClientContext) (subsystems.OverrideSource, error)
Build implements ComponentConfigurer, so the source can be used directly in the data system configuration builder's Overrides method.
func (*TestOverrideSource) Close ¶ added in v7.18.0
func (t *TestOverrideSource) Close() error
Close marks the source closed. Subsequent SetOverrides calls are ignored.
func (*TestOverrideSource) IsClosed ¶ added in v7.18.0
func (t *TestOverrideSource) IsClosed() bool
IsClosed reports whether Close has been called.
func (*TestOverrideSource) IsStarted ¶ added in v7.18.0
func (t *TestOverrideSource) IsStarted() bool
IsStarted reports whether Start has been called.
func (*TestOverrideSource) SetOverrides ¶ added in v7.18.0
func (t *TestOverrideSource) SetOverrides(data []ldstoretypes.Collection)
SetOverrides replaces the override layer contents, as if the source's backing data had changed. It must not be called before the source is started.
func (*TestOverrideSource) Start ¶ added in v7.18.0
func (t *TestOverrideSource) Start(sink subsystems.OverrideSink)
Start supplies the initial data, if any, before returning.
type TestPlugin ¶ added in v7.11.0
type TestPlugin struct {
// contains filtered or unexported fields
}
TestPlugin is a plugin for testing to be used only by the SDK tests.
func NewTestPlugin ¶ added in v7.11.0
func NewTestPlugin(name string, hooks []ldhooks.Hook) TestPlugin
NewTestPlugin creates a new test plugin.
func (TestPlugin) ExpectGetHooksCalled ¶ added in v7.11.0
func (p TestPlugin) ExpectGetHooksCalled(t *testing.T, expectedMetadata ldplugins.EnvironmentMetadata)
ExpectGetHooksCalled asserts that GetHooks was called with the given arguments.
func (TestPlugin) ExpectRegisterCalled ¶ added in v7.11.0
func (p TestPlugin) ExpectRegisterCalled( t *testing.T, expectedClient interfaces.LDClientInterface, expectedMetadata ldplugins.EnvironmentMetadata, )
ExpectRegisterCalled asserts that Register was called with the given arguments.
func (TestPlugin) GetHooks ¶ added in v7.11.0
func (p TestPlugin) GetHooks(metadata ldplugins.EnvironmentMetadata) []ldhooks.Hook
GetHooks testing implementation of the GetHooks method.
func (TestPlugin) Metadata ¶ added in v7.11.0
func (p TestPlugin) Metadata() ldplugins.Metadata
Metadata gets the meta-data for the plugin.
func (TestPlugin) Register ¶ added in v7.11.0
func (p TestPlugin) Register(client interfaces.LDClientInterface, metadata ldplugins.EnvironmentMetadata)
Register testing implementation of the Register method.
type TestPluginHook ¶ added in v7.11.0
type TestPluginHook struct {
ldhooks.Unimplemented
// contains filtered or unexported fields
}
TestPluginHook is a plugin hook for testing to be used only by the SDK tests.
This differs from TestHook in that we only care if the evaluation hook methods are called and not about testing evaluation hook logic itself.
func NewTestPluginHook ¶ added in v7.11.0
func NewTestPluginHook(name string) TestPluginHook
NewTestPluginHook creates a new test plugin hook.
func (TestPluginHook) AfterEvaluation ¶ added in v7.11.0
func (h TestPluginHook) AfterEvaluation( ctx context.Context, seriesContext ldhooks.EvaluationSeriesContext, data ldhooks.EvaluationSeriesData, detail ldreason.EvaluationDetail, ) (ldhooks.EvaluationSeriesData, error)
AfterEvaluation testing implementation of the AfterEvaluation stage.
func (TestPluginHook) BeforeEvaluation ¶ added in v7.11.0
func (h TestPluginHook) BeforeEvaluation( ctx context.Context, seriesContext ldhooks.EvaluationSeriesContext, data ldhooks.EvaluationSeriesData, ) (ldhooks.EvaluationSeriesData, error)
BeforeEvaluation testing implementation of the BeforeEvaluation stage.
func (TestPluginHook) ExpectAfterEvaluationCalled ¶ added in v7.11.0
func (h TestPluginHook) ExpectAfterEvaluationCalled(t *testing.T)
ExpectAfterEvaluationCalled asserts that AfterEvaluation was called.
func (TestPluginHook) ExpectBeforeEvaluationCalled ¶ added in v7.11.0
func (h TestPluginHook) ExpectBeforeEvaluationCalled(t *testing.T)
ExpectBeforeEvaluationCalled asserts that BeforeEvaluation was called.
func (TestPluginHook) Metadata ¶ added in v7.11.0
func (h TestPluginHook) Metadata() ldhooks.Metadata
Metadata gets the meta-data for the plugin hook.