22
33import static org .junit .Assert .assertNull ;
44import static org .junit .jupiter .api .Assertions .assertEquals ;
5+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
56import static org .junit .jupiter .api .Assertions .assertTrue ;
67
78import com .flagsmith .config .FlagsmithConfig .Protocol ;
89import java .net .InetSocketAddress ;
910import java .net .Proxy ;
1011import java .util .Collections ;
12+ import java .util .concurrent .TimeUnit ;
13+ import okhttp3 .ConnectionPool ;
1114import okhttp3 .mock .MockInterceptor ;
1215import org .junit .jupiter .api .Test ;
1316
@@ -53,6 +56,29 @@ public void configTest_multipleInterceptors() {
5356 assertEquals (2 , flagsmithConfig .getHttpClient ().interceptors ().size ());
5457 }
5558
59+ @ Test
60+ public void configTest_customConnectionPool_respectsKeepAliveAndMaxIdle () throws Exception {
61+ final ConnectionPool pool = new ConnectionPool (7 , 42 , TimeUnit .SECONDS );
62+
63+ final FlagsmithConfig flagsmithConfig = FlagsmithConfig .newBuilder ()
64+ .connectionPool (pool )
65+ .build ();
66+
67+ final ConnectionPool wired = flagsmithConfig .getHttpClient ().connectionPool ();
68+ final Object delegate = readField (wired , "delegate" );
69+ assertEquals (7 , readInt (delegate , "maxIdleConnections" ));
70+ assertEquals (TimeUnit .SECONDS .toNanos (42 ), readLong (delegate , "keepAliveDurationNs" ));
71+ }
72+
73+ @ Test
74+ public void configTest_nullConnectionPool_isSafeAndUsesDefault () {
75+ final FlagsmithConfig flagsmithConfig = FlagsmithConfig .newBuilder ()
76+ .connectionPool (null )
77+ .build ();
78+
79+ assertNotNull (flagsmithConfig .getHttpClient ().connectionPool ());
80+ }
81+
5682 @ Test
5783 public void configTest_supportedProtocols () {
5884 final FlagsmithConfig defaultFlagsmithConfig = FlagsmithConfig .newBuilder ().build ();
@@ -65,4 +91,18 @@ public void configTest_supportedProtocols() {
6591 assertEquals (1 , customFlagsmithConfig .getHttpClient ().protocols ().size ());
6692 assertEquals (okhttp3 .Protocol .HTTP_1_1 , customFlagsmithConfig .getHttpClient ().protocols ().get (0 ));
6793 }
94+
95+ private static Object readField (Object target , String fieldName ) throws Exception {
96+ java .lang .reflect .Field field = target .getClass ().getDeclaredField (fieldName );
97+ field .setAccessible (true );
98+ return field .get (target );
99+ }
100+
101+ private static int readInt (Object target , String fieldName ) throws Exception {
102+ return (int ) readField (target , fieldName );
103+ }
104+
105+ private static long readLong (Object target , String fieldName ) throws Exception {
106+ return (long ) readField (target , fieldName );
107+ }
68108}
0 commit comments