From 3d89acf9ea0bce8c41d2084b55e7eca15d481be4 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 24 Oct 2022 10:51:54 +0100 Subject: [PATCH] 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. --- .../core/codec/ResourceRegionEncoderTests.java | 2 +- .../io/buffer/LeakAwareDataBufferFactoryTests.java | 5 ++--- .../io/buffer/AbstractLeakCheckingTests.java | 4 +++- .../io/buffer/LeakAwareDataBufferFactory.java | 10 +++++++++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/core/codec/ResourceRegionEncoderTests.java b/spring-core/src/test/java/org/springframework/core/codec/ResourceRegionEncoderTests.java index 465780eb38..6412af9339 100644 --- a/spring-core/src/test/java/org/springframework/core/codec/ResourceRegionEncoderTests.java +++ b/spring-core/src/test/java/org/springframework/core/codec/ResourceRegionEncoderTests.java @@ -63,7 +63,7 @@ class ResourceRegionEncoderTests extends AbstractLeakCheckingTests { } @Test - void shouldEncodeResourceRegionFileResource() throws Exception { + void shouldEncodeResourceRegionFileResource() { ResourceRegion region = new ResourceRegion( new ClassPathResource("ResourceRegionEncoderTests.txt", getClass()), 0, 6); Flux result = this.encoder.encode(Mono.just(region), this.bufferFactory, diff --git a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactoryTests.java b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactoryTests.java index 64531c0865..9d482f6cc7 100644 --- a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactoryTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,8 +35,7 @@ class LeakAwareDataBufferFactoryTests { void leak() { DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(); try { - assertThatExceptionOfType(AssertionError.class).isThrownBy( - this.bufferFactory::checkForLeaks); + assertThatExceptionOfType(AssertionError.class).isThrownBy(this.bufferFactory::checkForLeaks); } finally { release(dataBuffer); diff --git a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/AbstractLeakCheckingTests.java b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/AbstractLeakCheckingTests.java index 061cad5f8e..348f5dbbec 100644 --- a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/AbstractLeakCheckingTests.java +++ b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/AbstractLeakCheckingTests.java @@ -16,6 +16,8 @@ package org.springframework.core.testfixture.io.buffer; +import java.time.Duration; + import org.junit.jupiter.api.AfterEach; import org.springframework.core.io.buffer.DataBufferFactory; @@ -42,7 +44,7 @@ public abstract class AbstractLeakCheckingTests { */ @AfterEach final void checkForLeaks() { - this.bufferFactory.checkForLeaks(); + this.bufferFactory.checkForLeaks(Duration.ofSeconds(1)); } } diff --git a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/LeakAwareDataBufferFactory.java b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/LeakAwareDataBufferFactory.java index 129821ccb5..9686aa95b0 100644 --- a/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/LeakAwareDataBufferFactory.java +++ b/spring-core/src/testFixtures/java/org/springframework/core/testfixture/io/buffer/LeakAwareDataBufferFactory.java @@ -81,13 +81,21 @@ public class LeakAwareDataBufferFactory implements DataBufferFactory { * method. */ public void checkForLeaks() { + checkForLeaks(Duration.ofSeconds(0)); + } + + /** + * Variant of {@link #checkForLeaks()} with the option to wait for buffer release. + * @param timeout how long to wait for buffers to be released; 0 for no waiting + */ + public void checkForLeaks(Duration timeout) { this.trackCreated.set(false); Instant start = Instant.now(); while (true) { if (this.created.stream().noneMatch(LeakAwareDataBuffer::isAllocated)) { return; } - if (Instant.now().isBefore(start.plus(Duration.ofSeconds(5)))) { + if (Instant.now().isBefore(start.plus(timeout))) { try { Thread.sleep(50); }