Skip to content

Commit 3d89acf

Browse files
committed
Adjust checkForLeaks timeout settings
LeakAwareDataBufferFactory#checkForLeaks automatically waits up to 5 sec for buffers to be released, which could be used as a way of awaiting on some async logic to complete, and as long as buffers are released, it shouldn't be long. However, the leak test in LeakAwareDataBufferFactoryTests actually expects to find a leak, and always ends up waiting the full 5 seconds. This change, makes the wait configurable, with the no-arg method using 0 (no waiting). AbstractLeakCheckingTests uses 1 second by default since ResourceRegionEncoderTests did fail locally. If more tests need this, we can adjust the settings.
1 parent 9410998 commit 3d89acf

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

spring-core/src/test/java/org/springframework/core/codec/ResourceRegionEncoderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void canEncode() {
6363
}
6464

6565
@Test
66-
void shouldEncodeResourceRegionFileResource() throws Exception {
66+
void shouldEncodeResourceRegionFileResource() {
6767
ResourceRegion region = new ResourceRegion(
6868
new ClassPathResource("ResourceRegionEncoderTests.txt", getClass()), 0, 6);
6969
Flux<DataBuffer> result = this.encoder.encode(Mono.just(region), this.bufferFactory,

spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactoryTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,8 +35,7 @@ class LeakAwareDataBufferFactoryTests {
3535
void leak() {
3636
DataBuffer dataBuffer = this.bufferFactory.allocateBuffer();
3737
try {
38-
assertThatExceptionOfType(AssertionError.class).isThrownBy(
39-
this.bufferFactory::checkForLeaks);
38+
assertThatExceptionOfType(AssertionError.class).isThrownBy(this.bufferFactory::checkForLeaks);
4039
}
4140
finally {
4241
release(dataBuffer);

spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/AbstractLeakCheckingTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.core.testfixture.io.buffer;
1818

19+
import java.time.Duration;
20+
1921
import org.junit.jupiter.api.AfterEach;
2022

2123
import org.springframework.core.io.buffer.DataBufferFactory;
@@ -42,7 +44,7 @@ public abstract class AbstractLeakCheckingTests {
4244
*/
4345
@AfterEach
4446
final void checkForLeaks() {
45-
this.bufferFactory.checkForLeaks();
47+
this.bufferFactory.checkForLeaks(Duration.ofSeconds(1));
4648
}
4749

4850
}

spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/LeakAwareDataBufferFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,21 @@ public LeakAwareDataBufferFactory(DataBufferFactory delegate) {
8181
* method.
8282
*/
8383
public void checkForLeaks() {
84+
checkForLeaks(Duration.ofSeconds(0));
85+
}
86+
87+
/**
88+
* Variant of {@link #checkForLeaks()} with the option to wait for buffer release.
89+
* @param timeout how long to wait for buffers to be released; 0 for no waiting
90+
*/
91+
public void checkForLeaks(Duration timeout) {
8492
this.trackCreated.set(false);
8593
Instant start = Instant.now();
8694
while (true) {
8795
if (this.created.stream().noneMatch(LeakAwareDataBuffer::isAllocated)) {
8896
return;
8997
}
90-
if (Instant.now().isBefore(start.plus(Duration.ofSeconds(5)))) {
98+
if (Instant.now().isBefore(start.plus(timeout))) {
9199
try {
92100
Thread.sleep(50);
93101
}

0 commit comments

Comments
 (0)