Skip to content

Commit

Permalink
"reset()" ReusableByteBuffers when they are returned to the pool (#667)
Browse files Browse the repository at this point in the history
ReusableByteBufefrs must be properly reset when returned to the pool.
  • Loading branch information
brenuart authored Sep 29, 2021
1 parent c87a732 commit f9ce2e3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ private ReusableByteBufferPool(int initialCapacity) {
*
* @param buffer the buffer to return to the pool.
*/
protected void releaseInstance(ReusableByteBuffer buffer) {
@Override
protected boolean recycleInstance(ReusableByteBuffer buffer) {
buffer.reset();
super.release(buffer);
return true;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2013-2021 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 net.logstash.logback.util;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import org.junit.jupiter.api.Test;

/**
* @author brenuart
*
*/
public class ReusableByteBufferPoolTest {

/*
* Assert ReusableByteBuffer are properly recycled when returned to the pool
*/
@Test
public void testBufferRecycled() throws IOException {
ReusableByteBufferPool pool = ReusableByteBufferPool.create(1024);

// Acquire a buffer from the pool and write some content to it
ReusableByteBuffer buffer = pool.acquire();
buffer.write("hello".getBytes());
assertThat(buffer.size()).isNotZero();

// Release the buffer
pool.release(buffer);

// Ask again for a buffer - the previous should be returned and have a size()==0
ReusableByteBuffer secondBuffer = pool.acquire();
assertThat(secondBuffer).isSameAs(buffer);
assertThat(secondBuffer.size()).isZero();
}
}

0 comments on commit f9ce2e3

Please sign in to comment.