public final class TestDataV2 extends java.lang.Object implements DataSourceBuilder<Synchronizer>
Synchronizer
for use with the FDv2 data system in test scenarios.
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(TestData.FlagBuilder) method.
Use TestData when you need a legacy DataSource;
use TestDataV2 when configuring the client with DataSystemBuilder.synchronizers(DataSourceBuilder[]).
TestDataV2 td = TestDataV2.synchronizer();
td.update(td.flag("flag-key-1").booleanFlag().variationForAllUsers(true));
LDConfig config = new LDConfig.Builder()
.dataSystem(new DataSystemBuilder().synchronizers(td))
.build();
LDClient client = new LDClient(sdkKey, config);
td.update(td.flag("flag-key-2")
.variationForUser("some-user-key", true)
.fallthroughVariation(false));
The above example uses a simple boolean flag. More complex configurations are possible using
the methods of the TestData.FlagBuilder 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 TestDataV2 instance is used to configure multiple clients, any changes
made via update(TestData.FlagBuilder), delete(String), and
updateStatus(DataSourceStatusProvider.State, DataSourceStatusProvider.ErrorInfo)
propagate to all configured synchronizers.
| Modifier and Type | Method and Description |
|---|---|
Synchronizer |
build(DataSourceBuildInputs context)
Builds a data source instance based on the provided context.
|
TestDataV2 |
delete(java.lang.String key)
Deletes a specific flag from the test data by create a versioned tombstone.
|
TestData.FlagBuilder |
flag(java.lang.String key)
Creates or copies a
TestData.FlagBuilder for building a test flag configuration. |
TestDataV2 |
shouldPersist(boolean shouldPersist)
Configures whether test data should be persisted to persistent stores.
|
static TestDataV2 |
synchronizer()
Creates a new instance of the test synchronizer.
|
TestDataV2 |
update(TestData.FlagBuilder flagBuilder)
Updates the test data with the specified flag configuration.
|
TestDataV2 |
updateStatus(DataSourceStatusProvider.State newState,
DataSourceStatusProvider.ErrorInfo newError)
Simulates a change in the synchronizer status.
|
public static TestDataV2 synchronizer()
See TestDataV2 for details.
public TestData.FlagBuilder flag(java.lang.String key)
TestData.FlagBuilder for building a test flag configuration.
If this flag key has already been defined in this TestDataV2 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 users 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(TestData.FlagBuilder).
key - the flag keyupdate(TestData.FlagBuilder)public TestDataV2 delete(java.lang.String key)
This has the same effect as if a flag were removed on the LaunchDarkly dashboard.
It immediately propagates the flag change to any LDClient instance(s) that you have
already configured to use this TestDataV2. If no LDClient has been started yet,
it simply adds tombstone to the test data which will be provided to any LDClient that
you subsequently configure.
key - the flag keypublic TestDataV2 update(TestData.FlagBuilder flagBuilder)
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 TestDataV2. 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(TestData.FlagBuilder) again.
flagBuilder - a flag configuration builderTestDataV2 instanceflag(String)public TestDataV2 updateStatus(DataSourceStatusProvider.State newState, DataSourceStatusProvider.ErrorInfo newError)
Use this if you want to test the behavior of application code that uses
LDClient.getDataSourceStatusProvider() to track whether the
synchronizer is having problems (for example, a network failure interrupting the streaming connection). It
does not actually stop the TestDataV2 synchronizer from working, so even if you have simulated
an outage, calling update(TestData.FlagBuilder) will still send updates.
newState - one of the constants defined by DataSourceStatusProvider.StatenewError - an DataSourceStatusProvider.ErrorInfo instance,
or nullTestDataV2 instancepublic TestDataV2 shouldPersist(boolean shouldPersist)
By default, test data is persisted (shouldPersist = true) to maintain consistency with
previous versions' behavior. When true, the test data will be written to any configured persistent
store (if the store is in READ_WRITE mode). This is useful for integration tests that verify
your persistent store configuration.
Set this to false if you want to prevent test data from being written to persistent stores.
This may be appropriate for unit testing scenarios where you want to test your application logic
without affecting a persistent store.
Example:
TestDataV2 td = TestDataV2.synchronizer()
.shouldPersist(false); // Disable persistence to avoid polluting the store
td.update(td.flag("flag-key").booleanFlag().variationForAllUsers(true));
shouldPersist - true if test data should be persisted to persistent stores, false otherwiseTestDataV2 instancepublic Synchronizer build(DataSourceBuildInputs context)
DataSourceBuilderbuild in interface DataSourceBuilder<Synchronizer>context - the context for building the data source