From 63275ae2b750af8d5ea78cc1f0395b536c8b0a55 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 19 Nov 2018 16:55:50 +0100 Subject: [PATCH] Add Test for LeakAwareDataBufferFactory --- .../core/io/buffer/LeakAwareDataBuffer.java | 9 ++-- .../io/buffer/LeakAwareDataBufferFactory.java | 18 ++++--- .../LeakAwareDataBufferFactoryTests.java | 54 +++++++++++++++++++ 3 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactoryTests.java diff --git a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java index b8f01e13a41..94c194a1c1d 100644 --- a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBuffer.java @@ -44,11 +44,14 @@ class LeakAwareDataBuffer implements PooledDataBuffer { Assert.notNull(dataBufferFactory, "DataBufferFactory must not be null"); this.delegate = delegate; this.dataBufferFactory = dataBufferFactory; - this.leakError = createLeakError(); + this.leakError = createLeakError(delegate); } - private static AssertionError createLeakError() { - AssertionError result = new AssertionError("Leak detected in test case"); + private static AssertionError createLeakError(DataBuffer delegate) { + String message = String.format("DataBuffer leak detected: {%s} has not been released.%n" + + "Stack trace of buffer allocation statement follows:", + delegate); + AssertionError result = new AssertionError(message); // remove first four irrelevant stack trace elements StackTraceElement[] oldTrace = result.getStackTrace(); StackTraceElement[] newTrace = new StackTraceElement[oldTrace.length - 4]; diff --git a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactory.java b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactory.java index 2a127cec2b0..16bebbaf38f 100644 --- a/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactory.java +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactory.java @@ -20,6 +20,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; +import org.jetbrains.annotations.NotNull; import org.junit.After; import org.springframework.util.Assert; @@ -74,17 +75,18 @@ public class LeakAwareDataBufferFactory implements DataBufferFactory { } @Override - public LeakAwareDataBuffer allocateBuffer() { - LeakAwareDataBuffer dataBuffer = - new LeakAwareDataBuffer(this.delegate.allocateBuffer(), this); - this.created.add(dataBuffer); - return dataBuffer; + public DataBuffer allocateBuffer() { + return allocateBufferInternal(this.delegate.allocateBuffer()); } @Override - public LeakAwareDataBuffer allocateBuffer(int initialCapacity) { - LeakAwareDataBuffer dataBuffer = - new LeakAwareDataBuffer(this.delegate.allocateBuffer(initialCapacity), this); + public DataBuffer allocateBuffer(int initialCapacity) { + return allocateBufferInternal(this.delegate.allocateBuffer(initialCapacity)); + } + + @NotNull + private DataBuffer allocateBufferInternal(DataBuffer delegateBuffer) { + LeakAwareDataBuffer dataBuffer = new LeakAwareDataBuffer(delegateBuffer, this); this.created.add(dataBuffer); return dataBuffer; } 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 new file mode 100644 index 00000000000..4dfb02dbad1 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/LeakAwareDataBufferFactoryTests.java @@ -0,0 +1,54 @@ +/* + * Copyright 2002-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.io.buffer; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.springframework.core.io.buffer.DataBufferUtils.release; + +/** + * @author Arjen Poutsma + */ +public class LeakAwareDataBufferFactoryTests { + + private final LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory(); + + + @Test + public void leak() { + DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(); + try { + this.bufferFactory.checkForLeaks(); + fail("AssertionError expected"); + } + catch (AssertionError expected) { + // ignore + } + finally { + release(dataBuffer); + } + } + + @Test + public void noLeak() { + DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(); + release(dataBuffer); + this.bufferFactory.checkForLeaks(); + } + +} \ No newline at end of file