sharedtest

package
v5.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

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

This section is empty.

Variables

View Source
var MockData = mockDataKind{/* contains filtered or unexported fields */}

MockData is an instance of ld.StoreDataKind corresponding to MockDataItem.

View Source
var MockOtherData = mockDataKind{/* contains filtered or unexported fields */}

MockOtherData is an instance of ld.StoreDataKind corresponding to another flavor of MockDataItem.

Functions

func AssertNotNil

func AssertNotNil(i interface{})

AssertNotNil forces a panic if the specified value is nil (either a nil interface value, or a nil pointer).

func AssertValuesJSONEqual added in v5.0.1

func AssertValuesJSONEqual(t *testing.T, expected interface{}, actual interface{})

AssertValuesJSONEqual serializes both values to JSON and calls assert.JSONEq.

func DataSetToMap

DataSetToMap converts the data format for Init into a map of maps.

func DataSourceThatIsAlwaysInitialized

func DataSourceThatIsAlwaysInitialized() interfaces.DataSourceFactory

DataSourceThatIsAlwaysInitialized returns a test DataSourceFactory that produces a data source that immediately reports success on startup, although it does not provide any data.

func DataSourceThatNeverInitializes

func DataSourceThatNeverInitializes() interfaces.DataSourceFactory

DataSourceThatNeverInitializes returns a test DataSourceFactory that produces a data source that immediately starts up in a failed state and does not provide any data.

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 ExpectNoMoreFlagChangeEvents

func ExpectNoMoreFlagChangeEvents(t *testing.T, ch <-chan interfaces.FlagChangeEvent)

ExpectNoMoreFlagChangeEvents asserts that a channel does not receive any flag change events for the next 100ms.

func ExpectNoMoreFlagValueChangeEvents

func ExpectNoMoreFlagValueChangeEvents(t *testing.T, ch <-chan interfaces.FlagValueChangeEvent)

ExpectNoMoreFlagValueChangeEvents asserts that a channel does not receive any flag value change events for the next 100ms.

func FlagDescriptor

FlagDescriptor is a shortcut for creating a StoreItemDescriptor from a flag.

func MakeMockDataSet

func MakeMockDataSet(items ...MockDataItem) []ldstoretypes.Collection

MakeMockDataSet constructs a data set to be passed to a data store's Init method.

func MakeSerializedMockDataSet

func MakeSerializedMockDataSet(items ...MockDataItem) []ldstoretypes.SerializedCollection

MakeSerializedMockDataSet constructs a data set to be passed to a persistent data store's Init method.

func NewClientContextWithDiagnostics

func NewClientContextWithDiagnostics(
	sdkKey string,
	headers http.Header,
	httpClientFactory func() *http.Client,
	diagnosticsManager *ldevents.DiagnosticsManager,
) interfaces.ClientContext

NewClientContextWithDiagnostics returns a ClientContext implementation for testing that includes a DiagnosticsManager.

func NewSimpleTestContext

func NewSimpleTestContext(sdkKey string) interfaces.ClientContext

NewSimpleTestContext returns a basic implementation of interfaces.ClientContext for use in test code.

func NewTestContext

NewTestContext returns a basic implementation of interfaces.ClientContext for use in test code.

func NewTestLoggers

func NewTestLoggers() ldlog.Loggers

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 added in v5.1.0

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 TestHTTP

TestHTTP returns a basic HTTPConfigurationFactory for test code.

func TestHTTPConfig

func TestHTTPConfig() interfaces.HTTPConfiguration

TestHTTPConfig returns a basic HTTPConfiguration for test code.

func TestHTTPConfigWithHeaders

func TestHTTPConfigWithHeaders(headers http.Header) interfaces.HTTPConfiguration

TestHTTPConfigWithHeaders returns a basic HTTPConfiguration with the specified HTTP headers.

func TestLogging

TestLogging returns a LoggingConfigurationFactory corresponding to NewTestLoggers().

func TestLoggingConfig

func TestLoggingConfig() interfaces.LoggingConfiguration

TestLoggingConfig returns a LoggingConfiguration corresponding to NewTestLoggers().

func TestLoggingConfigWithLoggers

func TestLoggingConfigWithLoggers(loggers ldlog.Loggers) interfaces.LoggingConfiguration

TestLoggingConfigWithLoggers returns a LoggingConfiguration with the specified Loggers.

func WithTempFileContaining

func WithTempFileContaining(data []byte, action func(filename string))

WithTempFileContaining runs the specified function with the file path of a temporary file that has been created with the specified data.

Types

type CapturingDataStore

type CapturingDataStore struct {
	// contains filtered or unexported fields
}

CapturingDataStore is a DataStore implementation that records update operations for testing.

func NewCapturingDataStore

func NewCapturingDataStore(realStore interfaces.DataStore) *CapturingDataStore

NewCapturingDataStore creates an instance of CapturingDataStore.

func (*CapturingDataStore) Close

func (d *CapturingDataStore) Close() error

Close in this test type is a no-op.

func (*CapturingDataStore) Get

Get is a standard DataStore method.

func (*CapturingDataStore) GetAll

GetAll is a standard DataStore method.

func (*CapturingDataStore) Init

func (d *CapturingDataStore) Init(allData []ldstoretypes.Collection) error

Init is a standard DataStore method.

func (*CapturingDataStore) IsInitialized

func (d *CapturingDataStore) IsInitialized() bool

IsInitialized in this test type always returns true.

func (*CapturingDataStore) IsStatusMonitoringEnabled

func (d *CapturingDataStore) IsStatusMonitoringEnabled() bool

IsStatusMonitoringEnabled in this test type returns true by default, but can be changed with SetStatusMonitoringEnabled.

func (*CapturingDataStore) SetFakeError

func (d *CapturingDataStore) SetFakeError(fakeError error)

SetFakeError causes subsequent Init or Upsert calls to return an error.

func (*CapturingDataStore) SetStatusMonitoringEnabled

func (d *CapturingDataStore) SetStatusMonitoringEnabled(statusMonitoringEnabled bool)

SetStatusMonitoringEnabled changes the value returned by IsStatusMonitoringEnabled.

func (*CapturingDataStore) Upsert

Upsert in this test type does nothing but capture its parameters.

func (*CapturingDataStore) WaitForDelete

func (d *CapturingDataStore) WaitForDelete(
	t *testing.T,
	kind ldstoretypes.DataKind,
	key string,
	version int,
	timeout time.Duration,
)

WaitForDelete waits for an Upsert call that is expected to delete a data item.

func (*CapturingDataStore) WaitForInit

func (d *CapturingDataStore) WaitForInit(
	t *testing.T,
	data *ldservices.ServerSDKData,
	timeout time.Duration,
)

WaitForInit waits for an Init call and verifies that it matches the expected data.

func (*CapturingDataStore) WaitForNextInit

func (d *CapturingDataStore) WaitForNextInit(
	t *testing.T,
	timeout time.Duration,
) []ldstoretypes.Collection

WaitForNextInit waits for an Init call.

func (*CapturingDataStore) WaitForNextUpsert

func (d *CapturingDataStore) WaitForNextUpsert(
	t *testing.T,
	timeout time.Duration,
) UpsertParams

WaitForNextUpsert waits for an Upsert call.

func (*CapturingDataStore) WaitForUpsert

func (d *CapturingDataStore) WaitForUpsert(
	t *testing.T,
	kind ldstoretypes.DataKind,
	key string,
	version int,
	timeout time.Duration,
) UpsertParams

WaitForUpsert waits for an Upsert call and verifies that it matches the expected data.

type CapturingEventProcessor

type CapturingEventProcessor struct {
	Events []interface{}
}

CapturingEventProcessor is a test implementation of EventProcessor that accumulates all events.

func (*CapturingEventProcessor) Close

func (c *CapturingEventProcessor) Close() error

func (*CapturingEventProcessor) Flush

func (c *CapturingEventProcessor) Flush()

func (*CapturingEventProcessor) RecordAliasEvent added in v5.2.0

func (c *CapturingEventProcessor) RecordAliasEvent(e ldevents.AliasEvent)

func (*CapturingEventProcessor) RecordCustomEvent

func (c *CapturingEventProcessor) RecordCustomEvent(e ldevents.CustomEvent)

func (*CapturingEventProcessor) RecordFeatureRequestEvent

func (c *CapturingEventProcessor) RecordFeatureRequestEvent(e ldevents.FeatureRequestEvent)

func (*CapturingEventProcessor) RecordIdentifyEvent

func (c *CapturingEventProcessor) RecordIdentifyEvent(e ldevents.IdentifyEvent)

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 DataSourceFactoryThatExposesUpdater

type DataSourceFactoryThatExposesUpdater struct {
	UnderlyingFactory interfaces.DataSourceFactory
	DataSourceUpdates interfaces.DataSourceUpdates
}

DataSourceFactoryThatExposesUpdater is a test implementation of DataSourceFactory that captures the DataSourceUpdates instance provided by LDClient.

func (*DataSourceFactoryThatExposesUpdater) CreateDataSource

type DataSourceFactoryWithData

type DataSourceFactoryWithData struct {
	Data []ldstoretypes.Collection
}

DataSourceFactoryWithData is a test implementation of DataSourceFactory that will cause the data source to provide a specific set of data when it starts.

func (DataSourceFactoryWithData) CreateDataSource

func (f DataSourceFactoryWithData) CreateDataSource(
	context interfaces.ClientContext,
	dataSourceUpdates interfaces.DataSourceUpdates,
) (interfaces.DataSource, error)

type DataStoreFactoryThatExposesUpdater

type DataStoreFactoryThatExposesUpdater struct {
	UnderlyingFactory interfaces.DataStoreFactory
	DataStoreUpdates  interfaces.DataStoreUpdates
}

DataStoreFactoryThatExposesUpdater is a test implementation of DataStoreFactory that captures the DataStoreUpdates instance provided by LDClient.

func (*DataStoreFactoryThatExposesUpdater) CreateDataStore

type MockDataItem

type MockDataItem struct {
	Key         string
	Version     int
	Deleted     bool
	Name        string
	IsOtherKind bool
}

MockDataItem is a test replacement for FeatureFlag/Segment.

func (MockDataItem) ToItemDescriptor

func (m MockDataItem) ToItemDescriptor() ldstoretypes.ItemDescriptor

ToItemDescriptor converts the test item to a StoreItemDescriptor.

func (MockDataItem) ToKeyedItemDescriptor

func (m MockDataItem) ToKeyedItemDescriptor() ldstoretypes.KeyedItemDescriptor

ToKeyedItemDescriptor converts the test item to a StoreKeyedItemDescriptor.

func (MockDataItem) ToSerializedItemDescriptor

func (m MockDataItem) ToSerializedItemDescriptor() ldstoretypes.SerializedItemDescriptor

ToSerializedItemDescriptor converts the test item to a StoreSerializedItemDescriptor.

type MockDataSourceUpdates

type MockDataSourceUpdates struct {
	DataStore *CapturingDataStore
	Statuses  chan interfaces.DataSourceStatus
	// contains filtered or unexported fields
}

MockDataSourceUpdates is a mock implementation of DataSourceUpdates for testing data sources.

func NewMockDataSourceUpdates

func NewMockDataSourceUpdates(realStore interfaces.DataStore) *MockDataSourceUpdates

NewMockDataSourceUpdates creates an instance of MockDataSourceUpdates.

The DataStoreStatusProvider can be nil if we are not doing a test that requires manipulation of that component.

func (*MockDataSourceUpdates) GetDataStoreStatusProvider

func (d *MockDataSourceUpdates) GetDataStoreStatusProvider() interfaces.DataStoreStatusProvider

GetDataStoreStatusProvider returns a stub implementation that does not have full functionality but enough to test a data source with.

func (*MockDataSourceUpdates) Init

Init in this test implementation, delegates to d.DataStore.CapturedUpdates.

func (*MockDataSourceUpdates) RequireStatus

RequireStatus blocks until a new data source status is available.

func (*MockDataSourceUpdates) RequireStatusOf

RequireStatusOf blocks until a new data source status is available, and verifies its state.

func (*MockDataSourceUpdates) UpdateStatus

func (d *MockDataSourceUpdates) UpdateStatus(
	newState interfaces.DataSourceState,
	newError interfaces.DataSourceErrorInfo,
)

UpdateStatus in this test implementation, pushes a value onto the Statuses channel.

func (*MockDataSourceUpdates) UpdateStoreStatus

func (d *MockDataSourceUpdates) UpdateStoreStatus(newStatus interfaces.DataStoreStatus)

UpdateStoreStatus simulates a change in the data store status.

func (*MockDataSourceUpdates) Upsert

Upsert in this test implementation, delegates to d.DataStore.CapturedUpdates.

type MockDatabaseInstance

type MockDatabaseInstance struct {
	// contains filtered or unexported fields
}

MockDatabaseInstance can be used with MockPersistentDataStore to simulate multiple data store instances sharing the same underlying data space.

func NewMockDatabaseInstance

func NewMockDatabaseInstance() *MockDatabaseInstance

NewMockDatabaseInstance creates an instance of MockDatabaseInstance.

func (*MockDatabaseInstance) Clear

func (db *MockDatabaseInstance) Clear(prefix string)

Clear removes all shared data.

type MockPersistentDataStore

type MockPersistentDataStore struct {
	InitQueriedCount int
	// contains filtered or unexported fields
}

MockPersistentDataStore is a test implementation of PersistentDataStore.

func NewMockPersistentDataStore

func NewMockPersistentDataStore() *MockPersistentDataStore

NewMockPersistentDataStore creates a test implementation of a persistent data store.

func NewMockPersistentDataStoreWithPrefix

func NewMockPersistentDataStoreWithPrefix(
	db *MockDatabaseInstance,
	prefix string,
) *MockPersistentDataStore

NewMockPersistentDataStoreWithPrefix creates a test implementation of a persistent data store that uses a MockDatabaseInstance to simulate a shared database.

func (*MockPersistentDataStore) Close

func (m *MockPersistentDataStore) Close() error

Close is a standard PersistentDataStore method.

func (*MockPersistentDataStore) EnableInstrumentedQueries

func (m *MockPersistentDataStore) EnableInstrumentedQueries(queryDelay time.Duration) <-chan struct{}

EnableInstrumentedQueries puts the test store into a mode where all get operations begin by posting a signal to a channel and then waiting for some amount of time, to test coalescing of requests.

func (*MockPersistentDataStore) ForceGet

ForceGet retrieves a serialized item directly from the test data with no other processing.

func (*MockPersistentDataStore) ForceRemove

func (m *MockPersistentDataStore) ForceRemove(kind ldstoretypes.DataKind, key string)

ForceRemove deletes an item from the test data.

func (*MockPersistentDataStore) ForceSet

ForceSet directly modifies an item in the test data.

func (*MockPersistentDataStore) ForceSetInited

func (m *MockPersistentDataStore) ForceSetInited(inited bool)

ForceSetInited changes the value that will be returned by IsInitialized().

func (*MockPersistentDataStore) Get

Get is a standard PersistentDataStore method.

func (*MockPersistentDataStore) GetAll

GetAll is a standard PersistentDataStore method.

func (*MockPersistentDataStore) Init

Init is a standard PersistentDataStore method.

func (*MockPersistentDataStore) IsInitialized

func (m *MockPersistentDataStore) IsInitialized() bool

IsInitialized is a standard PersistentDataStore method.

func (*MockPersistentDataStore) IsStoreAvailable

func (m *MockPersistentDataStore) IsStoreAvailable() bool

IsStoreAvailable is a standard PersistentDataStore method.

func (*MockPersistentDataStore) SetAvailable

func (m *MockPersistentDataStore) SetAvailable(available bool)

SetAvailable changes the value that will be returned by IsStoreAvailable().

func (*MockPersistentDataStore) SetFakeError

func (m *MockPersistentDataStore) SetFakeError(fakeError error)

SetFakeError causes subsequent store operations to return an error.

func (*MockPersistentDataStore) SetPersistOnlyAsString

func (m *MockPersistentDataStore) SetPersistOnlyAsString(value bool)

SetPersistOnlyAsString sets whether the mock data store should behave like our Redis implementation, where the item version is *not* persisted separately from the serialized item string (so the latter must be parsed to get the version). If this is false (the default), it behaves instead like our DynamoDB implementation, where the version metadata exists separately from the serialized string.

func (*MockPersistentDataStore) SetTestTxHook

func (m *MockPersistentDataStore) SetTestTxHook(hook func())

SetTestTxHook sets a callback function that will be called during updates, to support the concurrent modification tests in PersistentDataStoreTestSuite.

func (*MockPersistentDataStore) Upsert

Upsert is a standard PersistentDataStore method.

type SingleDataStoreFactory

type SingleDataStoreFactory struct {
	Instance interfaces.DataStore
}

SingleDataStoreFactory is a test implementation of DataStoreFactory that always returns the same pre-existing instance.

func (SingleDataStoreFactory) CreateDataStore

func (f SingleDataStoreFactory) CreateDataStore(
	context interfaces.ClientContext,
	dataStoreUpdates interfaces.DataStoreUpdates,
) (interfaces.DataStore, error)

type SingleEventProcessorFactory

type SingleEventProcessorFactory struct {
	Instance ldevents.EventProcessor
}

SingleEventProcessorFactory is a test implementation of EventProcessorFactory that always returns the same pre-existing instance.

func (SingleEventProcessorFactory) CreateEventProcessor

func (f SingleEventProcessorFactory) CreateEventProcessor(
	context interfaces.ClientContext,
) (ldevents.EventProcessor, error)

type SinglePersistentDataStoreFactory

type SinglePersistentDataStoreFactory struct {
	Instance interfaces.PersistentDataStore
}

SinglePersistentDataStoreFactory is a test implementation of PersistentDataStoreFactory that always returns the same pre-existing instance.

func (SinglePersistentDataStoreFactory) CreatePersistentDataStore

type UpsertParams

type UpsertParams struct {
	Kind ldstoretypes.DataKind
	Key  string
	Item ldstoretypes.ItemDescriptor
}

UpsertParams holds the parameters of an Upsert operation captured by CapturingDataStore.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL