launchdarkly / java-server-sdk

LaunchDarkly Server-Side SDK for Java
Other
83 stars 56 forks source link

TestData/ FlagData not working as documented #235

Closed kimayap03 closed 3 years ago

kimayap03 commented 3 years ago

Is this a support request? No

Describe the bug Using server-side SDK for JAVA, for Integration testing, I am trying to create LDClient such that it doesnt communicate with LaunchDarkly and access from file. As suggested here

/test/resources/FileData.json

{ "flagValues": { "my-string-flag-key": "value-1", "my-boolean-flag-key": true, "my-integer-flag-key": 3 } }

LDConfig using FlagData

     String filePath = "/test/resources/FileData.json";  << Absolute file path provided
        LDConfig ldConfig = new LDConfig.Builder() 
                .dataSource(FileData.dataSource()
                        .filePaths(filePath)
                        .autoUpdate(true))
                .offline(true)
                .events(Components.noEvents())
                .build();
     LDClient client = new LDClient ("test_sdk", ldConfig);

LDConfig using TestData

   TestData td = TestData.dataSource();
        td.flag("my-boolean-flag-key").booleanFlag().variationForAllUsers(true);
        LDConfig ldConfig = new LDConfig.Builder()
                .dataSource(td)
                .events(Components.noEvents())
                .build();
   LDClient client = new LDClient ("test_sdk", ldConfig);

In both cases LDClient is Initialized however the flag provided in file is not found

Following should pass -

assertTrue(ldService.getLDClient().boolVariation("my-boolean-flag-key", ldUser, false)); Error: Unknown feature flag "my-boolean-flag-key"; returning default value

How do I check if file provided is correctly setup for data source? Any help in this setup is highly appreciated as its blocking Integration testing.

Expected behavior Data source should get setup correctly

SDK version 5.3.0

Language version, developer tools Java

eli-darkly commented 3 years ago

TestData and FileData are completely different things, so this is really two issues. In the case of the TestData issue, the problem is that you're not using it as documented: calling td.flag does not update a flag, it just provides a builder that you can use to define a flag configuration. You've forgotten to call td.update which does the updating. So instead of doing this—

td.flag("my-boolean-flag-key").booleanFlag().variationForAllUsers(true);

—you need to do this (as shown in the code samples in the documentation):

td.update(testData.flag("my-boolean-flag-key").booleanFlag().variationForAllUsers(true));
kimayap03 commented 3 years ago

Yes My bad! You are right and this td.update(td.flag("my-boolean-flag-key").booleanFlag().variationForAllUsers(true)); works.

Any idea on why FlagData is not working or how to check if it is correctly setup? Since ideally I would like to use FlagData for testing

eli-darkly commented 3 years ago

Yes, hold on, I'm working on that (assuming you mean FileData)

eli-darkly commented 3 years ago

As for FileData, I believe the problem there is a counter-intuitive thing about the configuration, which the documentation does not describe correctly. The name offline is misleading, in that calling offline(true) does not only turn off network connections to LaunchDarkly— it turns off all data sources of any kind (as well as events). In other words, it's equivalent to calling ldConfig.DataSource(Components.externalUpdatesOnly()).Events(Components.noEvents()). And since FileData is a data source, that means it is disabling FileData too.

Unfortunately, the documentation for FileData gets this wrong and says that you can use offline in this situation. I think what you need to do is use the other option that it describes: just turn off events. And you are already doing that (.events(Components.noEvents())), so you can simply remove the .offline(true) from your code. It's not serving any purpose, since .dataSource(FileData etc.) has already overridden the default data source that would get flags from LD. The SDK always has one data source— by default, it is Components.streamingDataSource, but if you substitute FileData (or TestData as you did in your second example) then it uses that instead and does not make a streaming connection to LD.

eli-darkly commented 3 years ago

So I've made a note to fix that part of the docs— sorry about that.

kimayap03 commented 3 years ago

ok that worked Thank you!