Skip to content

Commit

Permalink
#12082 - fix DynamicCapacity.release()
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed Jul 24, 2024
1 parent d43da52 commit 76eaff4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.jetty.http2.tests;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
Expand Down Expand Up @@ -572,7 +573,7 @@ public void onPreface()
}
catch (HpackException x)
{
x.printStackTrace();
throw new RuntimeException(x);
}
}

Expand All @@ -589,7 +590,7 @@ public void onHeaders(HeadersFrame request)
}
catch (HpackException x)
{
x.printStackTrace();
throw new RuntimeException(x);
}
}

Expand All @@ -601,9 +602,9 @@ private void writeFrames()
accumulator.writeTo(Content.Sink.from(output), false);
accumulator.release();
}
catch (Throwable x)
catch (IOException x)
{
x.printStackTrace();
throw new RuntimeException(x);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ public DynamicCapacity(ByteBufferPool pool, boolean direct, long maxSize, int ag

private DynamicCapacity(List<RetainableByteBuffer> buffers, ByteBufferPool pool, boolean direct, long maxSize, int aggregationSize, int minRetainSize)
{
super();
super(new ReferenceCounter()); // Make sure the wrapped retainable is a ReferenceCounter.
_pool = pool == null ? ByteBufferPool.NON_POOLING : pool;
_direct = direct;
_maxSize = maxSize < 0 ? Long.MAX_VALUE : maxSize;
Expand Down Expand Up @@ -1916,6 +1916,8 @@ public boolean release()
buffer.release();
_buffers.clear();
_aggregate = null;
// Re-acquire the ref counter to make sure this instance is reusable.
((ReferenceCounter)getWrapped()).acquire();
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.io;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DynamicCapacityTest
{
private ArrayByteBufferPool.Tracking _pool;

@BeforeEach
public void beforeAll()
{
_pool = new ArrayByteBufferPool.Tracking();
}

@AfterEach
public void afterAll()
{
assertThat("Leaks: " + _pool.dumpLeaks(), _pool.getLeaks().size(), is(0));
}

@Test
public void testReuseAfterRelease()
{
RetainableByteBuffer.Mutable accumulator = new RetainableByteBuffer.DynamicCapacity(_pool);

accumulator.putInt(1);
assertTrue(accumulator.hasRemaining());
assertTrue(accumulator.release());
assertFalse(accumulator.hasRemaining());

accumulator.putInt(1);
assertTrue(accumulator.hasRemaining());
assertTrue(accumulator.release());
assertFalse(accumulator.hasRemaining());
}
}

0 comments on commit 76eaff4

Please sign in to comment.