1414import java .time .Duration ;
1515import java .time .Instant ;
1616import java .util .*;
17+ import java .util .concurrent .CompletableFuture ;
1718import java .util .concurrent .ExecutionException ;
1819import java .util .concurrent .TimeUnit ;
1920import java .util .concurrent .atomic .AtomicBoolean ;
@@ -752,22 +753,22 @@ void testHooks() throws IOException {
752753 server .enqueue (new MockResponse ().setResponseCode (500 ).setBody ("" ));
753754
754755 AtomicBoolean changed = new AtomicBoolean (false );
755- AtomicBoolean ready = new AtomicBoolean ( false );
756+ AtomicReference < ClientCacheState > ready = new AtomicReference ( null );
756757 AtomicReference <String > error = new AtomicReference <>("" );
757758
758759 ConfigCatClient cl = ConfigCatClient .get (Helpers .SDK_KEY , options -> {
759760 options .pollingMode (PollingModes .manualPoll ());
760761 options .baseUrl (server .url ("/" ).toString ());
761762 options .hooks ().addOnConfigChanged (map -> changed .set (true ));
762- options .hooks ().addOnClientReady (() -> ready .set (true ));
763+ options .hooks ().addOnClientReady (clientReadyState -> ready .set (clientReadyState ));
763764 options .hooks ().addOnError (error ::set );
764765 });
765766
766767 cl .forceRefresh ();
767768 cl .forceRefresh ();
768769
769770 assertTrue (changed .get ());
770- assertTrue ( ready .get ());
771+ assertEquals ( ClientCacheState . NO_FLAG_DATA , ready .get ());
771772 assertEquals ("Unexpected HTTP response was received while trying to fetch config JSON: 500 Server Error" , error .get ());
772773
773774 server .shutdown ();
@@ -803,6 +804,38 @@ void testHooksSub() throws IOException {
803804 cl .close ();
804805 }
805806
807+ @ Test
808+ void testReadyHookManualPollWithCache () throws IOException {
809+
810+ AtomicReference <ClientCacheState > ready = new AtomicReference (null );
811+ ConfigCache cache = new SingleValueCache (Helpers .cacheValueFromConfigJson (String .format (TEST_JSON , "test" )));
812+
813+ ConfigCatClient cl = ConfigCatClient .get (Helpers .SDK_KEY , options -> {
814+ options .pollingMode (PollingModes .manualPoll ());
815+ options .cache (cache );
816+ options .hooks ().addOnClientReady (clientReadyState -> ready .set (clientReadyState ));
817+ });
818+
819+ assertEquals (ClientCacheState .HAS_CACHED_FLAG_DATA_ONLY , ready .get ());
820+
821+ cl .close ();
822+ }
823+
824+ @ Test
825+ void testReadyHookLocalOnly () throws IOException {
826+ AtomicReference <ClientCacheState > ready = new AtomicReference (null );
827+
828+ ConfigCatClient cl = ConfigCatClient .get (Helpers .SDK_KEY , options -> {
829+ options .pollingMode (PollingModes .manualPoll ());
830+ options .flagOverrides (OverrideDataSourceBuilder .map (Collections .EMPTY_MAP ), OverrideBehaviour .LOCAL_ONLY );
831+ options .hooks ().addOnClientReady (clientReadyState -> ready .set (clientReadyState ));
832+ });
833+
834+ assertEquals (ClientCacheState .HAS_LOCAL_OVERRIDE_FLAG_DATA_ONLY , ready .get ());
835+
836+ cl .close ();
837+ }
838+
806839 @ Test
807840 void testHooksAutoPollSub () throws IOException {
808841 MockWebServer server = new MockWebServer ();
@@ -812,7 +845,7 @@ void testHooksAutoPollSub() throws IOException {
812845 server .enqueue (new MockResponse ().setResponseCode (500 ).setBody ("" ));
813846
814847 AtomicBoolean changed = new AtomicBoolean (false );
815- AtomicBoolean ready = new AtomicBoolean ( false );
848+ AtomicReference < ClientCacheState > ready = new AtomicReference ( null );
816849 AtomicReference <String > error = new AtomicReference <>("" );
817850
818851 ConfigCatClient cl = ConfigCatClient .get (Helpers .SDK_KEY , options -> {
@@ -821,14 +854,14 @@ void testHooksAutoPollSub() throws IOException {
821854 });
822855
823856 cl .getHooks ().addOnConfigChanged (map -> changed .set (true ));
824- cl .getHooks ().addOnClientReady (() -> ready .set (true ));
857+ cl .getHooks ().addOnClientReady (clientReadyState -> ready .set (clientReadyState ));
825858 cl .getHooks ().addOnError (error ::set );
826859
827860 cl .forceRefresh ();
828861 cl .forceRefresh ();
829862
830863 assertTrue (changed .get ());
831- assertTrue ( ready .get ());
864+ assertEquals ( ClientCacheState . HAS_UP_TO_DATE_FLAG_DATA , ready .get ());
832865 assertEquals ("Unexpected HTTP response was received while trying to fetch config JSON: 500 Server Error" , error .get ());
833866
834867 server .shutdown ();
@@ -980,4 +1013,26 @@ void testGetValueInvalidTypes(String settingKey, Class callType, Object defaultV
9801013 cl .close ();
9811014 }
9821015
1016+ @ Test
1017+ void testWaitForReady () throws IOException , InterruptedException , ExecutionException {
1018+ MockWebServer server = new MockWebServer ();
1019+ server .start ();
1020+
1021+ server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (TEST_JSON ));
1022+
1023+ ConfigCatClient cl = ConfigCatClient .get (Helpers .SDK_KEY , options -> {
1024+ options .pollingMode (PollingModes .autoPoll (2 ));
1025+ options .baseUrl (server .url ("/" ).toString ());
1026+ });
1027+
1028+ CompletableFuture <ClientCacheState > clientReadyStateCompletableFuture = cl .waitForReadyAsync ();
1029+ if (clientReadyStateCompletableFuture .isDone ()) {
1030+ assertEquals (clientReadyStateCompletableFuture .get (), ClientCacheState .HAS_UP_TO_DATE_FLAG_DATA );
1031+ }
1032+
1033+ server .shutdown ();
1034+ cl .close ();
1035+ }
1036+
1037+
9831038}
0 commit comments