From ff4b68a4901b023351d76009fd42cce926a16133 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Wed, 5 Jun 2024 21:10:10 +0200 Subject: [PATCH 1/3] Quote current test param when test fails --- .../src/test/java/io/trino/cache/TestEvictableCache.java | 6 ++++++ .../test/java/io/trino/cache/TestEvictableLoadingCache.java | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableCache.java b/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableCache.java index ce3788a3b8bb..946451e224f5 100644 --- a/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableCache.java +++ b/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableCache.java @@ -475,6 +475,9 @@ public void testInvalidateOngoingLoad() assertThat(threadA.get()).isEqualTo("stale value"); assertThat(threadB.get()).isEqualTo("fresh value"); } + catch (AssertionError e) { + throw new AssertionError("Error for invalidation=%s: %s".formatted(invalidation, e.getMessage()), e); + } finally { executor.shutdownNow(); assertThat(executor.awaitTermination(10, SECONDS)).isTrue(); @@ -548,6 +551,9 @@ public void testInvalidateAndLoadConcurrently() assertThat(remoteState.get()).isEqualTo(2 * 3 * 5 * 7); assertThat((long) cache.get(key, remoteState::get)).isEqualTo(remoteState.get()); } + catch (AssertionError e) { + throw new AssertionError("Error for invalidation=%s: %s".formatted(invalidation, e.getMessage()), e); + } finally { executor.shutdownNow(); assertThat(executor.awaitTermination(10, SECONDS)).isTrue(); diff --git a/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableLoadingCache.java b/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableLoadingCache.java index bb2e2f72f06a..ed3a6dfa6e6c 100644 --- a/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableLoadingCache.java +++ b/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableLoadingCache.java @@ -596,6 +596,9 @@ public String load(Integer key) assertThat(threadA.get()).isEqualTo("stale value"); assertThat(threadB.get()).isEqualTo("fresh value"); } + catch (AssertionError e) { + throw new AssertionError("Error for invalidation=%s: %s".formatted(invalidation, e.getMessage()), e); + } finally { executor.shutdownNow(); assertThat(executor.awaitTermination(10, SECONDS)).isTrue(); @@ -672,6 +675,9 @@ public void testInvalidateAndLoadConcurrently() assertThat(remoteState.get(key).get()).isEqualTo(2 * 3 * 5 * 7); assertThat((long) cache.get(key)).isEqualTo(2 * 3 * 5 * 7); } + catch (AssertionError e) { + throw new AssertionError("Error for invalidation=%s: %s".formatted(invalidation, e.getMessage()), e); + } finally { executor.shutdownNow(); assertThat(executor.awaitTermination(10, SECONDS)).isTrue(); From 5fc5041ece3570e78f143b1efcd70d5e496de1c6 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Wed, 5 Jun 2024 21:13:37 +0200 Subject: [PATCH 2/3] Use enhanced switch statement --- .../io/trino/cache/TestEvictableCache.java | 32 ++++++------------- .../cache/TestEvictableLoadingCache.java | 32 ++++++------------- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableCache.java b/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableCache.java index 946451e224f5..32d8a384e8be 100644 --- a/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableCache.java +++ b/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableCache.java @@ -448,21 +448,15 @@ public void testInvalidateOngoingLoad() assertThat(loadOngoing.await(10, SECONDS)).isTrue(); // 1 switch (invalidation) { - case INVALIDATE_KEY: - cache.invalidate(key); - break; - case INVALIDATE_PREDEFINED_KEYS: - cache.invalidateAll(ImmutableList.of(key)); - break; - case INVALIDATE_SELECTED_KEYS: + case INVALIDATE_KEY -> cache.invalidate(key); + case INVALIDATE_PREDEFINED_KEYS -> cache.invalidateAll(ImmutableList.of(key)); + case INVALIDATE_SELECTED_KEYS -> { Set keys = cache.asMap().keySet().stream() .filter(foundKey -> (int) foundKey == key) .collect(toImmutableSet()); cache.invalidateAll(keys); - break; - case INVALIDATE_ALL: - cache.invalidateAll(); - break; + } + case INVALIDATE_ALL -> cache.invalidateAll(); } invalidated.countDown(); // 2 @@ -519,21 +513,15 @@ public void testInvalidateAndLoadConcurrently() // invalidate switch (invalidation) { - case INVALIDATE_KEY: - cache.invalidate(key); - break; - case INVALIDATE_PREDEFINED_KEYS: - cache.invalidateAll(ImmutableList.of(key)); - break; - case INVALIDATE_SELECTED_KEYS: + case INVALIDATE_KEY -> cache.invalidate(key); + case INVALIDATE_PREDEFINED_KEYS -> cache.invalidateAll(ImmutableList.of(key)); + case INVALIDATE_SELECTED_KEYS -> { Set keys = cache.asMap().keySet().stream() .filter(foundKey -> (int) foundKey == key) .collect(toImmutableSet()); cache.invalidateAll(keys); - break; - case INVALIDATE_ALL: - cache.invalidateAll(); - break; + } + case INVALIDATE_ALL -> cache.invalidateAll(); } // read through cache diff --git a/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableLoadingCache.java b/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableLoadingCache.java index ed3a6dfa6e6c..e8dc05ef099c 100644 --- a/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableLoadingCache.java +++ b/lib/trino-cache/src/test/java/io/trino/cache/TestEvictableLoadingCache.java @@ -568,21 +568,15 @@ public String load(Integer key) assertThat(loadOngoing.await(10, SECONDS)).isTrue(); // 1 switch (invalidation) { - case INVALIDATE_KEY: - cache.invalidate(key); - break; - case INVALIDATE_PREDEFINED_KEYS: - cache.invalidateAll(ImmutableList.of(key)); - break; - case INVALIDATE_SELECTED_KEYS: + case INVALIDATE_KEY -> cache.invalidate(key); + case INVALIDATE_PREDEFINED_KEYS -> cache.invalidateAll(ImmutableList.of(key)); + case INVALIDATE_SELECTED_KEYS -> { Set keys = cache.asMap().keySet().stream() .filter(foundKey -> (int) foundKey == key) .collect(toImmutableSet()); cache.invalidateAll(keys); - break; - case INVALIDATE_ALL: - cache.invalidateAll(); - break; + } + case INVALIDATE_ALL -> cache.invalidateAll(); } remoteState.put(key, "fresh value"); @@ -642,21 +636,15 @@ public void testInvalidateAndLoadConcurrently() // invalidate switch (invalidation) { - case INVALIDATE_KEY: - cache.invalidate(key); - break; - case INVALIDATE_PREDEFINED_KEYS: - cache.invalidateAll(ImmutableList.of(key)); - break; - case INVALIDATE_SELECTED_KEYS: + case INVALIDATE_KEY -> cache.invalidate(key); + case INVALIDATE_PREDEFINED_KEYS -> cache.invalidateAll(ImmutableList.of(key)); + case INVALIDATE_SELECTED_KEYS -> { Set keys = cache.asMap().keySet().stream() .filter(foundKey -> (int) foundKey == key) .collect(toImmutableSet()); cache.invalidateAll(keys); - break; - case INVALIDATE_ALL: - cache.invalidateAll(); - break; + } + case INVALIDATE_ALL -> cache.invalidateAll(); } // read through cache From 23061aec3a2f84dbb89944845c0c6391da3b6bcb Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Wed, 5 Jun 2024 21:37:52 +0200 Subject: [PATCH 3/3] Remove redundant explicit generics --- lib/trino-cache/src/main/java/io/trino/cache/EmptyCache.java | 2 +- .../src/main/java/io/trino/cache/EvictableCache.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/trino-cache/src/main/java/io/trino/cache/EmptyCache.java b/lib/trino-cache/src/main/java/io/trino/cache/EmptyCache.java index 73e587c5fe0d..8f5ced18cf10 100644 --- a/lib/trino-cache/src/main/java/io/trino/cache/EmptyCache.java +++ b/lib/trino-cache/src/main/java/io/trino/cache/EmptyCache.java @@ -138,7 +138,7 @@ public CacheStats stats() @Override public ConcurrentMap asMap() { - return new ConcurrentMap() + return new ConcurrentMap<>() { @Override public V putIfAbsent(K key, V value) diff --git a/lib/trino-cache/src/main/java/io/trino/cache/EvictableCache.java b/lib/trino-cache/src/main/java/io/trino/cache/EvictableCache.java index 007209def03f..18e87eb2557e 100644 --- a/lib/trino-cache/src/main/java/io/trino/cache/EvictableCache.java +++ b/lib/trino-cache/src/main/java/io/trino/cache/EvictableCache.java @@ -283,7 +283,7 @@ public CacheStats stats() @Override public ConcurrentMap asMap() { - return new ConcurrentMap() + return new ConcurrentMap<>() { private final ConcurrentMap, V> dataCacheMap = dataCache.asMap();