@@ -334,7 +334,7 @@ public void testErrorGracefulModeOff() {
334
334
@ Test
335
335
public void testInvalidConfigJSON () {
336
336
337
- mockHttpResponse (TEST_BASE_URL , "{}" );
337
+ mockHttpResponse ("{}" );
338
338
339
339
initClient (false , false );
340
340
@@ -569,4 +569,139 @@ public void testAssignmentLogErrorNonFatal() {
569
569
ArgumentCaptor <Assignment > assignmentLogCaptor = ArgumentCaptor .forClass (Assignment .class );
570
570
verify (mockAssignmentLogger , times (1 )).logAssignment (assignmentLogCaptor .capture ());
571
571
}
572
+
573
+ @ Test
574
+ public void testPolling () {
575
+ EppoHttpClient httpClient = mockHttpResponse (BOOL_FLAG_CONFIG );
576
+
577
+ BaseEppoClient client =
578
+ eppoClient =
579
+ new BaseEppoClient (
580
+ DUMMY_FLAG_API_KEY ,
581
+ "java" ,
582
+ "100.1.0" ,
583
+ null ,
584
+ TEST_BASE_URL ,
585
+ mockAssignmentLogger ,
586
+ null ,
587
+ null ,
588
+ false ,
589
+ false ,
590
+ true ,
591
+ null ,
592
+ null ,
593
+ null );
594
+
595
+ client .loadConfiguration ();
596
+ client .startPolling (20 );
597
+
598
+ // Method will be called immediately on init
599
+ verify (httpClient , times (1 )).get (anyString ());
600
+ assertTrue (eppoClient .getBooleanAssignment ("bool_flag" , "subject1" , false ));
601
+
602
+ // Sleep for 25 ms to allow another polling cycle to complete
603
+ sleepUninterruptedly (25 );
604
+
605
+ // Now, the method should have been called twice
606
+ verify (httpClient , times (2 )).get (anyString ());
607
+
608
+ eppoClient .stopPolling ();
609
+ assertTrue (eppoClient .getBooleanAssignment ("bool_flag" , "subject1" , false ));
610
+
611
+ sleepUninterruptedly (25 );
612
+
613
+ // No more calls since stopped
614
+ verify (httpClient , times (2 )).get (anyString ());
615
+
616
+ // Set up a different config to be served
617
+ when (httpClient .get (anyString ())).thenReturn (DISABLED_BOOL_FLAG_CONFIG .getBytes ());
618
+ client .startPolling (20 );
619
+
620
+ // True until the next config is fetched.
621
+ assertTrue (eppoClient .getBooleanAssignment ("bool_flag" , "subject1" , false ));
622
+
623
+ sleepUninterruptedly (25 );
624
+
625
+ assertFalse (eppoClient .getBooleanAssignment ("bool_flag" , "subject1" , false ));
626
+
627
+ eppoClient .stopPolling ();
628
+ }
629
+
630
+ @ SuppressWarnings ("SameParameterValue" )
631
+ private void sleepUninterruptedly (long sleepMs ) {
632
+ try {
633
+ Thread .sleep (sleepMs );
634
+ } catch (InterruptedException e ) {
635
+ throw new RuntimeException (e );
636
+ }
637
+ }
638
+
639
+ private static final String BOOL_FLAG_CONFIG =
640
+ ("{\n "
641
+ + " \" createdAt\" : \" 2024-04-17T19:40:53.716Z\" ,\n "
642
+ + " \" format\" : \" SERVER\" ,\n "
643
+ + " \" environment\" : {\n "
644
+ + " \" name\" : \" Test\" \n "
645
+ + " },\n "
646
+ + " \" flags\" : {\n "
647
+ + " \" bool_flag\" : {\n "
648
+ + " \" key\" : \" bool_flag\" ,\n "
649
+ + " \" enabled\" : true,\n "
650
+ + " \" variationType\" : \" BOOLEAN\" ,\n "
651
+ + " \" variations\" : {\n "
652
+ + " \" on\" : {\n "
653
+ + " \" key\" : \" on\" ,\n "
654
+ + " \" value\" : true\n "
655
+ + " }\n "
656
+ + " },\n "
657
+ + " \" allocations\" : [\n "
658
+ + " {\n "
659
+ + " \" key\" : \" on\" ,\n "
660
+ + " \" doLog\" : true,\n "
661
+ + " \" splits\" : [\n "
662
+ + " {\n "
663
+ + " \" variationKey\" : \" on\" ,\n "
664
+ + " \" shards\" : []\n "
665
+ + " }\n "
666
+ + " ]\n "
667
+ + " }\n "
668
+ + " ],\n "
669
+ + " \" totalShards\" : 10000\n "
670
+ + " }\n "
671
+ + " }\n "
672
+ + "}" );
673
+ private static final String DISABLED_BOOL_FLAG_CONFIG =
674
+ ("{\n "
675
+ + " \" createdAt\" : \" 2024-04-17T19:40:53.716Z\" ,\n "
676
+ + " \" format\" : \" SERVER\" ,\n "
677
+ + " \" environment\" : {\n "
678
+ + " \" name\" : \" Test\" \n "
679
+ + " },\n "
680
+ + " \" flags\" : {\n "
681
+ + " \" bool_flag\" : {\n "
682
+ + " \" key\" : \" bool_flag\" ,\n "
683
+ + " \" enabled\" : false,\n "
684
+ + " \" variationType\" : \" BOOLEAN\" ,\n "
685
+ + " \" variations\" : {\n "
686
+ + " \" on\" : {\n "
687
+ + " \" key\" : \" on\" ,\n "
688
+ + " \" value\" : true\n "
689
+ + " }\n "
690
+ + " },\n "
691
+ + " \" allocations\" : [\n "
692
+ + " {\n "
693
+ + " \" key\" : \" on\" ,\n "
694
+ + " \" doLog\" : true,\n "
695
+ + " \" splits\" : [\n "
696
+ + " {\n "
697
+ + " \" variationKey\" : \" on\" ,\n "
698
+ + " \" shards\" : []\n "
699
+ + " }\n "
700
+ + " ]\n "
701
+ + " }\n "
702
+ + " ],\n "
703
+ + " \" totalShards\" : 10000\n "
704
+ + " }\n "
705
+ + " }\n "
706
+ + "}" );
572
707
}
0 commit comments