3030import static io .trino .gateway .ha .TestingJdbcConnectionManager .createTestingJdbcConnectionManager ;
3131import static io .trino .gateway .ha .TestingJdbcConnectionManager .dataStoreConfig ;
3232import static io .trino .gateway .ha .TestingJdbcConnectionManager .destroyTestingDatabase ;
33+ import static java .util .concurrent .TimeUnit .SECONDS ;
3334import static org .assertj .core .api .Assertions .assertThat ;
3435import static org .assertj .core .api .Assertions .assertThatThrownBy ;
3536
@@ -42,7 +43,7 @@ void testGatewayManagerWithCache()
4243 JdbcConnectionManager connectionManager = createTestingJdbcConnectionManager (dataStoreConfig ());
4344 DatabaseCacheConfiguration cacheConfiguration = new DatabaseCacheConfiguration ();
4445 cacheConfiguration .setEnabled (true );
45- cacheConfiguration .setRefreshAfterWrite (new Duration (5 , TimeUnit . SECONDS ));
46+ cacheConfiguration .setRefreshAfterWrite (new Duration (5 , SECONDS ));
4647 testGatewayManager (new HaGatewayManager (connectionManager .getJdbi (), new RoutingConfiguration (), cacheConfiguration ));
4748 }
4849
@@ -53,7 +54,7 @@ void testGatewayManagerWithoutCache()
5354 testGatewayManager (new HaGatewayManager (connectionManager .getJdbi (), new RoutingConfiguration (), new DatabaseCacheConfiguration ()));
5455 }
5556
56- void testGatewayManager (HaGatewayManager haGatewayManager )
57+ static void testGatewayManager (HaGatewayManager haGatewayManager )
5758 {
5859 ProxyBackendConfiguration backend = new ProxyBackendConfiguration ();
5960 backend .setActive (true );
@@ -120,8 +121,8 @@ void testGatewayManagerCacheExpire()
120121 JdbcConnectionManager connectionManager = createTestingJdbcConnectionManager (dataStoreConfig );
121122 DatabaseCacheConfiguration cacheConfiguration = new DatabaseCacheConfiguration ();
122123 cacheConfiguration .setEnabled (true );
123- cacheConfiguration .setRefreshAfterWrite (new Duration (3 , TimeUnit . SECONDS ));
124- cacheConfiguration .setExpireAfterWrite (new Duration (5 , TimeUnit . SECONDS ));
124+ cacheConfiguration .setRefreshAfterWrite (new Duration (3 , SECONDS ));
125+ cacheConfiguration .setExpireAfterWrite (new Duration (5 , SECONDS ));
125126 TestingTicker ticker = new TestingTicker ();
126127 HaGatewayManager haGatewayManager = new HaGatewayManager (connectionManager .getJdbi (), new RoutingConfiguration (), cacheConfiguration , ticker );
127128
@@ -134,18 +135,18 @@ void testGatewayManagerCacheExpire()
134135 haGatewayManager .addBackend (etl );
135136
136137 // Initial fetch
137- assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo ). orElseThrow ()). isEqualTo ("https://etl1.trino.gateway.io:443" );
138+ assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo )). hasValue ("https://etl1.trino.gateway.io:443" );
138139
139- // Read from cache
140+ // Test read from cache when DB is not available
140141 destroyTestingDatabase (dataStoreConfig );
141- assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo ). orElseThrow ()). isEqualTo ("https://etl1.trino.gateway.io:443" );
142+ assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo )). hasValue ("https://etl1.trino.gateway.io:443" );
142143
143144 // Failed to refresh from DB, but still read from cache
144- ticker .increment (4 , TimeUnit . SECONDS );
145- assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo ). orElseThrow ()). isEqualTo ("https://etl1.trino.gateway.io:443" );
145+ ticker .increment (4 , SECONDS );
146+ assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo )). hasValue ("https://etl1.trino.gateway.io:443" );
146147
147148 // Expired from cache, failed to read from DB
148- ticker .increment (2 , TimeUnit . SECONDS );
149+ ticker .increment (2 , SECONDS );
149150 assertThatThrownBy (() -> haGatewayManager .getBackendByName ("new-etl1" )).hasMessage ("Failed to load backends from database to cache" );
150151 }
151152
@@ -163,8 +164,8 @@ void testRemoveTrailingSlashInUrl()
163164 etl .setExternalUrl ("https://etl1.trino.gateway.io:443/" );
164165 haGatewayManager .addBackend (etl );
165166
166- assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo ). orElseThrow ()). isEqualTo ("https://etl1.trino.gateway.io:443" );
167- assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getExternalUrl ). orElseThrow ()). isEqualTo ("https://etl1.trino.gateway.io:443" );
167+ assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo )). hasValue ("https://etl1.trino.gateway.io:443" );
168+ assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getExternalUrl )). hasValue ("https://etl1.trino.gateway.io:443" );
168169
169170 ProxyBackendConfiguration etl2 = new ProxyBackendConfiguration ();
170171 etl2 .setActive (false );
@@ -174,11 +175,11 @@ void testRemoveTrailingSlashInUrl()
174175 etl2 .setExternalUrl ("https://etl2.trino.gateway.io:443/" );
175176 haGatewayManager .updateBackend (etl2 );
176177
177- assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo ). orElseThrow ()). isEqualTo ("https://etl2.trino.gateway.io:443" );
178- assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getExternalUrl ). orElseThrow ()). isEqualTo ("https://etl2.trino.gateway.io:443" );
178+ assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getProxyTo )). hasValue ("https://etl2.trino.gateway.io:443" );
179+ assertThat (haGatewayManager .getBackendByName ("new-etl1" ).map (ProxyBackendConfiguration ::getExternalUrl )). hasValue ("https://etl2.trino.gateway.io:443" );
179180 }
180181
181- public static class TestingTicker
182+ private static class TestingTicker
182183 implements Ticker
183184 {
184185 private long time ;
@@ -189,10 +190,10 @@ public synchronized long read()
189190 return this .time ;
190191 }
191192
192- public synchronized void increment (long delta , TimeUnit unit )
193+ private synchronized void increment (long delta , TimeUnit unit )
193194 {
194195 checkArgument (delta >= 0L , "delta is negative" );
195- this . time += unit .toNanos (delta );
196+ time += unit .toNanos (delta );
196197 }
197198 }
198199}
0 commit comments