Skip to content

Commit

Permalink
Add tests covering new release() call.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosen committed Feb 13, 2016
1 parent 6134989 commit c9726c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import io.netty.channel.FileRegion;
import io.netty.util.AbstractReferenceCounted;
import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.*;

import org.apache.spark.network.TestManagedBuffer;
import org.apache.spark.network.buffer.ManagedBuffer;
import org.apache.spark.network.buffer.NettyManagedBuffer;
import org.apache.spark.network.util.ByteArrayWritableChannel;
Expand All @@ -48,14 +50,25 @@ public void testShortWrite() throws Exception {
@Test
public void testByteBufBody() throws Exception {
ByteBuf header = Unpooled.copyLong(42);
ByteBuf body = Unpooled.copyLong(84);
ByteBuf body = Unpooled.copyLong(84).retain();
ManagedBuffer managedBuf = new NettyManagedBuffer(body);
MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, body.readableBytes());

ByteBuf result = doWrite(msg, 1);
assertEquals(msg.count(), result.readableBytes());
assertEquals(42, result.readLong());
assertEquals(84, result.readLong());
msg.deallocate();
}

@Test
public void testDeallocateReleasesManagedBuffer() throws Exception {
ByteBuf header = Unpooled.copyLong(42);
ManagedBuffer managedBuf = Mockito.spy(new TestManagedBuffer(84));
ByteBuf body = ((ByteBuf) managedBuf.convertToNetty()).retain();
MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, body.readableBytes());
msg.deallocate();
Mockito.verify(managedBuf, Mockito.times(1)).release();
}

private void testFileRegionBody(int totalWrites, int writesPerCall) throws Exception {
Expand All @@ -70,6 +83,7 @@ private void testFileRegionBody(int totalWrites, int writesPerCall) throws Excep
for (long i = 0; i < 8; i++) {
assertEquals(i, result.readLong());
}
msg.deallocate();
}

private ByteBuf doWrite(MessageWithHeader msg, int minExpectedWrites) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.netty.channel.Channel;
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.Mockito.*;

import org.apache.spark.network.TestManagedBuffer;
import org.apache.spark.network.buffer.ManagedBuffer;
Expand All @@ -40,12 +39,12 @@ public void managedBuffersAreFeedWhenConnectionIsClosed() throws Exception {
buffers.add(buffer2);
long streamId = manager.registerStream("appId", buffers.iterator());

Channel dummyChannel = Mockito.mock(Channel.class);
Channel dummyChannel = Mockito.mock(Channel.class, Mockito.RETURNS_SMART_NULLS);
manager.registerChannel(dummyChannel, streamId);

manager.connectionTerminated(dummyChannel);

Mockito.verify(buffer1, times(1)).release();
Mockito.verify(buffer2, times(1)).release();
Mockito.verify(buffer1, Mockito.times(1)).release();
Mockito.verify(buffer2, Mockito.times(1)).release();
}
}

0 comments on commit c9726c2

Please sign in to comment.