Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mitali Jha
committed
Jan 13, 2016
1 parent
99e4818
commit ffdbe28
Showing
13 changed files
with
1,148 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...est/java/org/asynchttpclient/handler/resumable/ResumableRandomAccessFileListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
package org.asynchttpclient.handler.resumable; | |||
|
|||
import static org.mockito.Mockito.*; | |||
|
|||
import java.io.IOException; | |||
import java.io.RandomAccessFile; | |||
import java.nio.ByteBuffer; | |||
|
|||
import org.powermock.api.mockito.PowerMockito; | |||
import org.testng.annotations.Test; | |||
|
|||
public class ResumableRandomAccessFileListenerTest { | |||
|
|||
@Test | |||
public void testOnBytesReceivedBufferHasArray() throws IOException { | |||
RandomAccessFile file = PowerMockito.mock(RandomAccessFile.class); | |||
ResumableRandomAccessFileListener listener = new ResumableRandomAccessFileListener(file); | |||
byte[] array = new byte[] { 1, 2, 23, 33 }; | |||
ByteBuffer buf = ByteBuffer.wrap(array); | |||
listener.onBytesReceived(buf); | |||
verify(file).write(array, 0, 4); | |||
} | |||
|
|||
@Test | |||
public void testOnBytesReceivedBufferHasNoArray() throws IOException { | |||
RandomAccessFile file = PowerMockito.mock(RandomAccessFile.class); | |||
ResumableRandomAccessFileListener listener = new ResumableRandomAccessFileListener(file); | |||
|
|||
byte[] byteArray = new byte[] { 1, 2, 23, 33 }; | |||
ByteBuffer buf = ByteBuffer.allocateDirect(4); | |||
buf.put(byteArray); | |||
buf.flip(); | |||
listener.onBytesReceived(buf); | |||
verify(file).write(byteArray); | |||
} | |||
|
|||
} |
73 changes: 73 additions & 0 deletions
73
client/src/test/java/org/asynchttpclient/netty/NettyResponseFutureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,73 @@ | |||
package org.asynchttpclient.netty; | |||
|
|||
import static org.testng.Assert.*; | |||
|
|||
import java.util.concurrent.CancellationException; | |||
import java.util.concurrent.ExecutionException; | |||
|
|||
import static org.mockito.Mockito.*; | |||
|
|||
import org.asynchttpclient.AsyncHandler; | |||
import org.testng.annotations.Test; | |||
|
|||
public class NettyResponseFutureTest { | |||
|
|||
@Test | |||
public void testCancel() { | |||
AsyncHandler asyncHandler = mock(AsyncHandler.class); | |||
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null); | |||
boolean result = nettyResponseFuture.cancel(false); | |||
verify(asyncHandler).onThrowable(anyObject()); | |||
assertTrue(result, "Cancel should return true if the Future was cancelled successfully"); | |||
assertTrue(nettyResponseFuture.isCancelled(), "isCancelled should return true for a cancelled Future"); | |||
} | |||
|
|||
@Test | |||
public void testCancelOnAlreadyCancelled() { | |||
AsyncHandler asyncHandler = mock(AsyncHandler.class); | |||
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null); | |||
nettyResponseFuture.cancel(false); | |||
boolean result = nettyResponseFuture.cancel(false); | |||
assertFalse(result, "cancel should return false for an already cancelled Future"); | |||
assertTrue(nettyResponseFuture.isCancelled(), "isCancelled should return true for a cancelled Future"); | |||
} | |||
|
|||
@Test(expectedExceptions = CancellationException.class) | |||
public void testGetContentThrowsCancellationExceptionIfCancelled() throws InterruptedException, ExecutionException { | |||
AsyncHandler asyncHandler = mock(AsyncHandler.class); | |||
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null); | |||
nettyResponseFuture.cancel(false); | |||
nettyResponseFuture.get(); | |||
fail("A CancellationException must have occurred by now as 'cancel' was called before 'get'"); | |||
} | |||
|
|||
@Test | |||
public void testGet() throws Exception { | |||
AsyncHandler asyncHandler = mock(AsyncHandler.class); | |||
Object value = new Object(); | |||
when(asyncHandler.onCompleted()).thenReturn(value); | |||
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null); | |||
nettyResponseFuture.done(); | |||
Object result = nettyResponseFuture.get(); | |||
assertEquals(result, value, "The Future should return the value given by asyncHandler#onCompleted"); | |||
} | |||
|
|||
@Test(expectedExceptions = ExecutionException.class) | |||
public void testGetThrowsExceptionThrownByAsyncHandler() throws Exception { | |||
AsyncHandler asyncHandler = mock(AsyncHandler.class); | |||
when(asyncHandler.onCompleted()).thenThrow(new RuntimeException()); | |||
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null); | |||
nettyResponseFuture.done(); | |||
nettyResponseFuture.get(); | |||
fail("An ExecutionException must have occurred by now as asyncHandler threw an exception in 'onCompleted'"); | |||
} | |||
|
|||
@Test(expectedExceptions = ExecutionException.class) | |||
public void testGetThrowsExceptionOnAbort() throws InterruptedException, ExecutionException { | |||
AsyncHandler asyncHandler = mock(AsyncHandler.class); | |||
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null); | |||
nettyResponseFuture.abort(new RuntimeException()); | |||
nettyResponseFuture.get(); | |||
fail("An ExecutionException must have occurred by now as 'abort' was called before 'get'"); | |||
} | |||
} |
Oops, something went wrong.