Skip to content

Commit 4cbf3eb

Browse files
committed
Stabilize PreparedStatementCancellationIT#will_cache_invalid_cql
The test method `will_cache_invalid_cql` is inherently flaky due to the usage of weak valued cache. This is a quick workaround that should achieve the same end result as the setup in PreparedStatementCachingIT. Here we use reflection to forcibly change the cache used by the request processor. The alternatives are: reimplementing similar setup as in PreparedStatementCachingIT with some changes. That means having a test version of DefaultDriverContext, CqlPrepareAsyncProcessor, SessionBuilder and necessary methods. The other option is moving this test method to the PreparedStatementCachingIT, but then the cache's remove callback which is used there needs to be adjusted, otherwise the test will still be flaky, but with different cause. The server-side error about invalid syntax can occasionally break the remove callback.
1 parent c7cee1f commit 4cbf3eb

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

integration-tests/src/test/java/com/datastax/oss/driver/core/cql/PreparedStatementCancellationIT.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import com.datastax.oss.driver.internal.core.cql.CqlPrepareAsyncProcessor;
3232
import com.datastax.oss.driver.shaded.guava.common.base.Predicates;
3333
import com.datastax.oss.driver.shaded.guava.common.cache.Cache;
34+
import com.datastax.oss.driver.shaded.guava.common.cache.CacheBuilder;
3435
import com.datastax.oss.driver.shaded.guava.common.collect.Iterables;
36+
import java.lang.reflect.Field;
3537
import java.util.concurrent.CompletableFuture;
3638
import org.junit.After;
3739
import org.junit.Before;
@@ -118,6 +120,15 @@ public void will_cache_invalid_cql() throws Exception {
118120

119121
CqlSession session = SessionUtils.newSession(ccmRule, sessionRule.keyspace());
120122
CqlPrepareAsyncProcessor processor = findProcessor(session);
123+
124+
// Forcibly replace the cache with strong-valued one to make sure this test does not fail due to
125+
// GC
126+
Field cacheField = CqlPrepareAsyncProcessor.class.getDeclaredField("cache");
127+
cacheField.setAccessible(true);
128+
Cache<PrepareRequest, CompletableFuture<PreparedStatement>> newCache =
129+
CacheBuilder.newBuilder().build();
130+
cacheField.set(processor, newCache);
131+
121132
Cache<PrepareRequest, CompletableFuture<PreparedStatement>> cache = processor.getCache();
122133
assertThat(cache.size()).isEqualTo(0);
123134

0 commit comments

Comments
 (0)