Skip to content

Commit d70875f

Browse files
authored
Fix flaky tests (#667)
Signed-off-by: Valentin Delaye <jonesbusy@users.noreply.github.com>
1 parent d07d3c2 commit d70875f

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<mockito.version>5.23.0</mockito.version>
7272
<system.stubs.version>2.1.8</system.stubs.version>
7373
<classgraph.version>4.8.184</classgraph.version>
74+
<awaitability.version>4.3.0</awaitability.version>
7475

7576
<!-- Plugin version -->
7677
<palantir.java.format.version>2.74.0</palantir.java.format.version>
@@ -149,6 +150,11 @@
149150
<artifactId>commons-compress</artifactId>
150151
<version>${apache.common-compress.version}</version>
151152
</dependency>
153+
<dependency>
154+
<groupId>org.awaitility</groupId>
155+
<artifactId>awaitility</artifactId>
156+
<version>${awaitability.version}</version>
157+
</dependency>
152158
<dependency>
153159
<groupId>org.bouncycastle</groupId>
154160
<artifactId>bcprov-jdk18on</artifactId>
@@ -251,6 +257,11 @@
251257
<artifactId>classgraph</artifactId>
252258
<scope>test</scope>
253259
</dependency>
260+
<dependency>
261+
<groupId>org.awaitility</groupId>
262+
<artifactId>awaitility</artifactId>
263+
<scope>test</scope>
264+
</dependency>
254265
<dependency>
255266
<groupId>org.junit.jupiter</groupId>
256267
<artifactId>junit-jupiter-api</artifactId>

src/test/java/land/oras/RegistryTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ void shouldCheckIfCanMount() throws IOException {
284284
}
285285

286286
@Test
287+
@Execution(ExecutionMode.SAME_THREAD)
287288
void shouldMountWithInsecureRegistry(@TempDir Path homeDir) throws Exception {
288289

289290
// language=toml

src/test/java/land/oras/auth/TokenCacheTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020

2121
package land.oras.auth;
2222

23+
import static org.awaitility.Awaitility.await;
2324
import static org.junit.jupiter.api.Assertions.assertEquals;
24-
import static org.junit.jupiter.api.Assertions.assertNull;
2525
import static org.junit.jupiter.api.Assertions.assertTrue;
2626

2727
import io.micrometer.core.instrument.FunctionCounter;
2828
import io.micrometer.core.instrument.MeterRegistry;
2929
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
30+
import java.util.concurrent.TimeUnit;
3031
import land.oras.ContainerRef;
3132
import land.oras.TestUtils;
3233
import org.junit.jupiter.api.BeforeAll;
@@ -80,16 +81,13 @@ void shouldAddAndRetrieveTokenThenExpiredIt() throws InterruptedException {
8081
Scopes scopes = Scopes.of(containerRef, Scope.PULL).withService("dockerhub"); // Pull only
8182
TokenCache.put(scopes, tokenResponse);
8283
assertEquals(tokenResponse, TokenCache.get(scopes), "Should retrieve the token before expiration");
83-
Thread.sleep(3000); // Wait for the token to expire
84-
assertNull(TokenCache.get(scopes), "Should return null after token expiration");
84+
await().atMost(10, TimeUnit.SECONDS)
85+
.until(() -> TokenCache.get(scopes) == null
86+
&& meterRegistry.find("cache.evictions").functionCounters().stream()
87+
.mapToDouble(FunctionCounter::count)
88+
.sum()
89+
>= 1);
8590
TestUtils.dumpMetrics(meterRegistry);
86-
// At least one eviction
87-
assertTrue(
88-
meterRegistry.find("cache.evictions").functionCounters().stream()
89-
.mapToDouble(FunctionCounter::count)
90-
.sum()
91-
>= 1,
92-
"Should have at least one eviction");
9391
}
9492

9593
@Test

0 commit comments

Comments
 (0)