Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import java.util.function.Supplier;
import lombok.AllArgsConstructor;
import lombok.Cleanup;
Expand Down Expand Up @@ -234,7 +233,6 @@ public void insertionDeletionWitGenericType(String provider, Supplier<String> ur
v.put("b", "2");
objCache.create(key1, v).join();

assertEqualsAndRetry(() -> objCache.getIfCached(key1), Optional.of(v), Optional.empty());
assertEquals(objCache.get(key1).join(), Optional.of(v));

objCache.delete(key1).join();
Expand Down Expand Up @@ -265,12 +263,11 @@ public void insertionDeletion(String provider, Supplier<String> urlSupplier) thr
assertEquals(e.getCause().getClass(), AlreadyExistsException.class);
}

assertEqualsAndRetry(() -> objCache.getIfCached(key1), Optional.of(value1), Optional.empty());
assertEquals(objCache.get(key1).join(), Optional.of(value1));

assertEquals(objCache.readModifyUpdateOrCreate(key1, __ -> value2).join(), value2);
assertEquals(objCache.get(key1).join(), Optional.of(value2));
assertEqualsAndRetry(() -> objCache.getIfCached(key1), Optional.of(value2), Optional.empty());
Awaitility.await().untilAsserted(() -> assertEquals(objCache.getIfCached(key1), Optional.of(value2)));

@Demogorgon314 Demogorgon314 Mar 1, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lhotari
Need ensure objCache.getIfCached(key1) don't return a wrong value.

If we use Awaitility.await().untilAsserted(() -> assertEquals(objCache.getIfCached(key1), Optional.of(value2)));, here has an example case test can't cover.

  1. objCache.getIfCached(key1) => Optional.of(value1) (We should fail here, but it will continue to run)
  2. objCache.getIfCached(key1) => Optional.of(value2)

The getIfCached method should always return Optional.empty() or correct value, we don't want to read dirty values from the cache, right?


objCache.delete(key1).join();

Expand Down Expand Up @@ -321,7 +318,7 @@ public void insertionOutsideCache(String provider, Supplier<String> urlSupplier)
store.put(key1, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(value1), Optional.of(-1L)).join();

assertEquals(objCache.get(key1).join(), Optional.of(value1));
assertEqualsAndRetry(() -> objCache.getIfCached(key1), Optional.of(value1), Optional.empty());
Awaitility.await().untilAsserted(() -> assertEquals(objCache.getIfCached(key1), Optional.of(value1)));
}

@Test(dataProvider = "impl")
Expand Down Expand Up @@ -587,35 +584,4 @@ public CustomClass deserialize(String path, byte[] content, Stat stat) throws IO
assertEquals(res.getValue().b, 2);
assertEquals(res.getValue().path, key1);
}

public static void assertEqualsAndRetry(Supplier<Object> actual,
Object expected,
Object expectedAndRetry) throws Exception {
assertEqualsAndRetry(actual, expected, expectedAndRetry, 5, 100);
}

public static void assertEqualsAndRetry(Supplier<Object> actual,
Object expected,
Object expectedAndRetry,
int retryCount,
long intSleepTimeInMillis) throws Exception {
assertTrue(retryStrategically((__) -> {
if (actual.get().equals(expectedAndRetry)) {
return false;
}
assertEquals(actual.get(), expected);
return true;
}, retryCount, intSleepTimeInMillis));
}

public static boolean retryStrategically(Predicate<Void> predicate, int retryCount, long intSleepTimeInMillis)
throws Exception {
for (int i = 0; i < retryCount; i++) {
if (predicate.test(null) || i == (retryCount - 1)) {
return true;
}
Thread.sleep(intSleepTimeInMillis + (intSleepTimeInMillis * i));
}
return false;
}
}