Add Test for LeakAwareDataBufferFactory
This commit is contained in:
parent
3203d39821
commit
63275ae2b7
|
|
@ -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];
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue