Class TestData
A mechanism for providing dynamically updatable feature flag state in a simplified form to an SDK client in test scenarios.
Implements
Inherited Members
Namespace: LaunchDarkly.Sdk.Server.Integrations
Assembly: LaunchDarkly.ServerSdk.dll
Syntax
public sealed class TestData : IComponentConfigurer<IDataSource>
Remarks
Unlike FileData, this mechanism does not use any external resources. It provides only the data that the application has put into it using the Update(FlagBuilder) method.
The example code below uses a simple boolean flag, but more complex configurations are possible using the methods of the TestData.FlagBuilder that is returned by Flag(string). TestData.FlagBuilder supports many of the ways a flag can be configured on the LaunchDarkly dashboard, but does not currently support 1. rule operators other than "in" and "not in", or 2. percentage rollouts.
If the same TestData instance is used to configure multiple LdClient instances, any changes made to the data will propagate to all of the LdClients.
Examples
var td = TestData.DataSource();
td.Update(td.Flag("flag-key-1").BooleanFlag().VariationForAll(true));
var config = Configuration.Builder("sdk-key")
.DataSource(td)
.Build();
var client = new LdClient(config);
// flags can be updated at any time:
td.update(testData.flag("flag-key-2")
.VariationForUser("some-user-key", true)
.FallthroughVariation(false));
Methods
| Edit this page View SourceBuild(LdClientContext)
Called internally by the SDK to create an implementation instance. Applications should not need to call this method.
Declaration
public IDataSource Build(LdClientContext context)
Parameters
Type | Name | Description |
---|---|---|
LdClientContext | context | provides configuration properties and other components from the current SDK client instance |
Returns
Type | Description |
---|---|
IDataSource | a instance of the component type |
DataSource()
Creates a new instance of the test data source.
Declaration
public static TestData DataSource()
Returns
Type | Description |
---|---|
TestData | a new configurable test data source |
Remarks
See TestData for details.
Flag(string)
Creates or copies a TestData.FlagBuilder for building a test flag configuration.
Declaration
public TestData.FlagBuilder Flag(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | the flag key |
Returns
Type | Description |
---|---|
TestData.FlagBuilder | a flag configuration builder |
Remarks
If this flag key has already been defined in this TestData instance, then the builder starts with the same configuration that was last provided for this flag.
Otherwise, it starts with a new default configuration in which the flag has true
and false
variations, is true
for all contexts when targeting is turned on
and false
otherwise, and currently has targeting turned on. You can change any
of those properties, and provide more complex behavior, using the
TestData.FlagBuilder methods.
Once you have set the desired configuration, pass the builder to Update(FlagBuilder).
See Also
| Edit this page View SourceUpdate(FlagBuilder)
Updates the test data with the specified flag configuration.
Declaration
public TestData Update(TestData.FlagBuilder flagBuilder)
Parameters
Type | Name | Description |
---|---|---|
TestData.FlagBuilder | flagBuilder | a flag configuration builder |
Returns
Type | Description |
---|---|
TestData | the same TestData instance |
Remarks
This has the same effect as if a flag were added or modified on the LaunchDarkly dashboard. It immediately propagates the flag change to any LdClient instance(s) that you have already configured to use this TestData. If no LdClient has been started yet, it simply adds this flag to the test data which will be provided to any LdClient that you subsequently configure.
Any subsequent changes to this TestData.FlagBuilder instance do not affect the test data, unless you call Update(FlagBuilder) again.
See Also
| Edit this page View SourceUpdateStatus(DataSourceState, ErrorInfo?)
Simulates a change in the data source status.
Declaration
public TestData UpdateStatus(DataSourceState newState, DataSourceStatus.ErrorInfo? newError)
Parameters
Type | Name | Description |
---|---|---|
DataSourceState | newState | one of the constants defined by DataSourceState |
DataSourceStatus.ErrorInfo? | newError | an optional DataSourceStatus.ErrorInfo instance |
Returns
Type | Description |
---|---|
TestData |
Remarks
Use this if you want to test the behavior of application code that uses DataSourceStatusProvider to track whether the data source is having problems (for example, a network failure interrupting the streaming connection). It does not actually stop the TestData data source from working, so even if you have simulated an outage, calling Update(FlagBuilder) will still send updates.