Skip to content

Commit

Permalink
refine assertion for broken equality log message (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Apr 7, 2023
1 parent 1eb57ac commit 0ef0f31
Showing 1 changed file with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2359,10 +2359,8 @@ public void brokenEquality_eviction(BoundedLocalCache<Object, Int> cache,
assertThat(cache.estimatedSize()).isEqualTo(1);

var event = Iterables.getOnlyElement(TestLoggerFactory.getLoggingEvents());
assertThat(event.getFormattedMessage()).containsMatch(
"An invalid state was detected.*\\(key: \\d+, key type: MutableInt.*\\)");
assertThat(event.getThrowable().orElseThrow())
.isInstanceOf(IllegalStateException.class);
assertThat(event.getThrowable().orElseThrow()).isInstanceOf(IllegalStateException.class);
checkBrokenEqualityMessage(cache, key, event.getFormattedMessage());
assertThat(event.getLevel()).isEqualTo(ERROR);

cache.data.clear();
Expand Down Expand Up @@ -2390,10 +2388,8 @@ public void brokenEquality_expiration(
assertThat(cache.estimatedSize()).isEqualTo(1);

var event = Iterables.getOnlyElement(TestLoggerFactory.getLoggingEvents());
assertThat(event.getFormattedMessage()).containsMatch(
"An invalid state was detected.*\\(key: \\d+, key type: MutableInt.*\\)");
assertThat(event.getThrowable().orElseThrow())
.isInstanceOf(IllegalStateException.class);
assertThat(event.getThrowable().orElseThrow()).isInstanceOf(IllegalStateException.class);
checkBrokenEqualityMessage(cache, key, event.getFormattedMessage());
assertThat(event.getLevel()).isEqualTo(ERROR);

cache.data.clear();
Expand All @@ -2414,10 +2410,8 @@ public void brokenEquality_clear(BoundedLocalCache<Object, Int> cache, CacheCont
assertThat(cache.estimatedSize()).isEqualTo(1);

var event = Iterables.getOnlyElement(TestLoggerFactory.getLoggingEvents());
assertThat(event.getFormattedMessage()).containsMatch(
"An invalid state was detected.*\\(key: \\d+, key type: MutableInt.*\\)");
assertThat(event.getThrowable().orElseThrow())
.isInstanceOf(IllegalStateException.class);
assertThat(event.getThrowable().orElseThrow()).isInstanceOf(IllegalStateException.class);
checkBrokenEqualityMessage(cache, key, event.getFormattedMessage());
assertThat(event.getLevel()).isEqualTo(ERROR);

cache.data.clear();
Expand Down Expand Up @@ -2499,12 +2493,20 @@ private static void testForBrokenEquality(BoundedLocalCache<MutableInt, Int> cac
key.decrement();

var e = assertThrows(IllegalStateException.class, () -> task.accept(key));
assertThat(e).hasMessageThat().containsMatch(
"An invalid state was detected.*\\(key: \\d+, key type: MutableInt.*\\)");
checkBrokenEqualityMessage(cache, key, e.getMessage());

cache.data.clear();
}

private static void checkBrokenEqualityMessage(
BoundedLocalCache<?, ?> cache, Object key, String msg) {
assertThat(msg).contains("An invalid state was detected");
assertThat(msg).contains("cache type: " + cache.getClass().getSimpleName());
assertThat(msg).contains("node type: " + cache.nodeFactory.getClass().getSimpleName());
assertThat(msg).contains("key type: " + key.getClass().getSimpleName());
assertThat(msg).contains("key: " + key);
}

/* --------------- Miscellaneous --------------- */

@Test
Expand Down

0 comments on commit 0ef0f31

Please sign in to comment.