Skip to content

Commit 63275ae

Browse files
committed
Add Test for LeakAwareDataBufferFactory
1 parent 3203d39 commit 63275ae

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ class LeakAwareDataBuffer implements PooledDataBuffer {
4444
Assert.notNull(dataBufferFactory, "DataBufferFactory must not be null");
4545
this.delegate = delegate;
4646
this.dataBufferFactory = dataBufferFactory;
47-
this.leakError = createLeakError();
47+
this.leakError = createLeakError(delegate);
4848
}
4949

50-
private static AssertionError createLeakError() {
51-
AssertionError result = new AssertionError("Leak detected in test case");
50+
private static AssertionError createLeakError(DataBuffer delegate) {
51+
String message = String.format("DataBuffer leak detected: {%s} has not been released.%n" +
52+
"Stack trace of buffer allocation statement follows:",
53+
delegate);
54+
AssertionError result = new AssertionError(message);
5255
// remove first four irrelevant stack trace elements
5356
StackTraceElement[] oldTrace = result.getStackTrace();
5457
StackTraceElement[] newTrace = new StackTraceElement[oldTrace.length - 4];

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23+
import org.jetbrains.annotations.NotNull;
2324
import org.junit.After;
2425

2526
import org.springframework.util.Assert;
@@ -74,17 +75,18 @@ public void checkForLeaks() {
7475
}
7576

7677
@Override
77-
public LeakAwareDataBuffer allocateBuffer() {
78-
LeakAwareDataBuffer dataBuffer =
79-
new LeakAwareDataBuffer(this.delegate.allocateBuffer(), this);
80-
this.created.add(dataBuffer);
81-
return dataBuffer;
78+
public DataBuffer allocateBuffer() {
79+
return allocateBufferInternal(this.delegate.allocateBuffer());
8280
}
8381

8482
@Override
85-
public LeakAwareDataBuffer allocateBuffer(int initialCapacity) {
86-
LeakAwareDataBuffer dataBuffer =
87-
new LeakAwareDataBuffer(this.delegate.allocateBuffer(initialCapacity), this);
83+
public DataBuffer allocateBuffer(int initialCapacity) {
84+
return allocateBufferInternal(this.delegate.allocateBuffer(initialCapacity));
85+
}
86+
87+
@NotNull
88+
private DataBuffer allocateBufferInternal(DataBuffer delegateBuffer) {
89+
LeakAwareDataBuffer dataBuffer = new LeakAwareDataBuffer(delegateBuffer, this);
8890
this.created.add(dataBuffer);
8991
return dataBuffer;
9092
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.io.buffer;
18+
19+
import org.junit.Test;
20+
21+
import static org.junit.Assert.*;
22+
import static org.springframework.core.io.buffer.DataBufferUtils.release;
23+
24+
/**
25+
* @author Arjen Poutsma
26+
*/
27+
public class LeakAwareDataBufferFactoryTests {
28+
29+
private final LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
30+
31+
32+
@Test
33+
public void leak() {
34+
DataBuffer dataBuffer = this.bufferFactory.allocateBuffer();
35+
try {
36+
this.bufferFactory.checkForLeaks();
37+
fail("AssertionError expected");
38+
}
39+
catch (AssertionError expected) {
40+
// ignore
41+
}
42+
finally {
43+
release(dataBuffer);
44+
}
45+
}
46+
47+
@Test
48+
public void noLeak() {
49+
DataBuffer dataBuffer = this.bufferFactory.allocateBuffer();
50+
release(dataBuffer);
51+
this.bufferFactory.checkForLeaks();
52+
}
53+
54+
}

0 commit comments

Comments
 (0)