Skip to content

Commit

Permalink
413155 simplify HttpTransportOverSPDY a bit more. Throw exceptions if…
Browse files Browse the repository at this point in the history
… send is called with no content, lastContent=false and no responseInfo
  • Loading branch information
Thomas Becker committed Jul 17, 2013
1 parent 205ef85 commit d65b511
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void send(ByteBuffer responseBodyContent, boolean lastContent, Callback c
}

@Override
public void send(HttpGenerator.ResponseInfo info, ByteBuffer content, boolean lastContent, Callback callback)
public void send(HttpGenerator.ResponseInfo info, ByteBuffer content, boolean lastContent, final Callback callback)
{
if (LOG.isDebugEnabled())
LOG.debug("Sending {} {} {} {} last={}", this, stream, info, BufferUtil.toDetailString(content), lastContent);
Expand Down Expand Up @@ -133,29 +133,36 @@ public void send(HttpGenerator.ResponseInfo info, ByteBuffer content, boolean la
LOG.warn("Committed response twice.", exception);
return;
}
sendReply(info, lastContent && !hasContent ? callback : new Callback.Adapter(), close);
sendReply(info, !hasContent ? callback : new Callback.Adapter()
{
@Override
public void failed(Throwable x)
{
callback.failed(x);
}
}, close);
}

// Do we have some content to send as well
if (hasContent)
{
// send the data and let it call the callback
LOG.debug("Send content: {} on stream: {} lastContent={}", BufferUtil.toDetailString(content), stream,
lastContent);

// send the data and let it call the callback
stream.data(new ByteBufferDataInfo(endPoint.getIdleTimeout(), TimeUnit.MILLISECONDS, content, lastContent
), callback);
}
// else do we need to close
else if (lastContent && info == null)
{
LOG.debug("No content and lastContent=true. Sending empty ByteBuffer to close stream: {}", stream);
// send empty data to close and let the send call the callback
LOG.debug("No content and lastContent=true. Sending empty ByteBuffer to close stream: {}", stream);
stream.data(new ByteBufferDataInfo(endPoint.getIdleTimeout(), TimeUnit.MILLISECONDS,
BufferUtil.EMPTY_BUFFER, lastContent), callback);
}
else if(!lastContent)
callback.succeeded();
else if (!lastContent && !hasContent && info == null)
throw new IllegalStateException("not lastContent, no content and no responseInfo!");

}

private void sendReply(HttpGenerator.ResponseInfo info, Callback callback, boolean close)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,12 @@ public void testSendWithResponseInfoNullAndEmptyContentAndLastContentTrue() thro
assertThat("ByteBuffer is empty", dataInfoCaptor.getValue().length(), is(0));
}

@Test
public void testSendWithResponseInfoNullAndContentNullAndLastContentFalse() throws Exception
{
ByteBuffer content = null;
boolean lastContent = false;

httpTransportOverSPDY.send(null, content, lastContent, callback);
verify(callback, times(1)).succeeded();
verify(stream, times(0)).data(any(ByteBufferDataInfo.class), any(Callback.class));
}

@Test
public void testSendWithResponseInfoNullAndContentAndLastContentFalse() throws Exception
{
ByteBuffer content = createRandomByteBuffer();
boolean lastContent = false;

httpTransportOverSPDY.send(null, content, lastContent, callback);
ArgumentCaptor<ByteBufferDataInfo> dataInfoCaptor = ArgumentCaptor.forClass(ByteBufferDataInfo.class);
ArgumentCaptor<Callback> callbackCaptor = ArgumentCaptor.forClass(Callback.class);
Expand All @@ -166,13 +155,35 @@ public void testSendWithResponseInfoNullAndContentAndLastContentFalse() throws E
assertThat("ByteBuffer is empty", dataInfoCaptor.getValue().length(), is(4096));
}

@Test
@Test(expected = IllegalStateException.class)
public void testSendWithResponseInfoNullAndContentNullAndLastContentFalse() throws Exception
{
ByteBuffer content = null;
boolean lastContent = false;
httpTransportOverSPDY.send(null, content, lastContent, callback);
}

@Test(expected = IllegalStateException.class)
public void testSendWithResponseInfoNullAndEmptyContentAndLastContentFalse() throws Exception
{
ByteBuffer content = BufferUtil.EMPTY_BUFFER;
boolean lastContent = false;

httpTransportOverSPDY.send(null, content, lastContent, callback);
}

@Test
public void testSendWithResponseInfoAndContentNullAndLastContentFalse() throws Exception
{
ByteBuffer content = null;
boolean lastContent = false;

httpTransportOverSPDY.send(responseInfo, content, lastContent, callback);
ArgumentCaptor<ReplyInfo> replyInfoCaptor = ArgumentCaptor.forClass(ReplyInfo.class);
ArgumentCaptor<Callback> callbackCaptor = ArgumentCaptor.forClass(Callback.class);
verify(stream, times(1)).reply(replyInfoCaptor.capture(), callbackCaptor.capture());
callbackCaptor.getValue().succeeded();
assertThat("ReplyInfo close is true", replyInfoCaptor.getValue().isClose(), is(false));

verify(stream, times(0)).data(any(ByteBufferDataInfo.class), any(Callback.class));
verify(callback, times(1)).succeeded();
}
Expand All @@ -182,7 +193,7 @@ public void testSendWithResponseInfoAndContentNullAndLastContentTrue() throws Ex
{
ByteBuffer content = null;
boolean lastContent = true;

// when stream.isClosed() is called a 2nd time, the reply has closed the stream already
when(stream.isClosed()).thenReturn(false).thenReturn(true);

Expand Down Expand Up @@ -216,23 +227,6 @@ public void testSendWithResponseInfoAndContentAndLastContentTrue() throws Except
verify(callback, times(1)).succeeded();
}

@Test
public void testSendWithResponseInfoAndContentNullAndLastContentFalse() throws Exception
{
ByteBuffer content = null;
boolean lastContent = false;

httpTransportOverSPDY.send(responseInfo, content, lastContent, callback);
ArgumentCaptor<ReplyInfo> replyInfoCaptor = ArgumentCaptor.forClass(ReplyInfo.class);
ArgumentCaptor<Callback> callbackCaptor = ArgumentCaptor.forClass(Callback.class);
verify(stream, times(1)).reply(replyInfoCaptor.capture(), callbackCaptor.capture());
callbackCaptor.getValue().succeeded();
assertThat("ReplyInfo close is true", replyInfoCaptor.getValue().isClose(), is(false));

verify(stream, times(0)).data(any(ByteBufferDataInfo.class), any(Callback.class));
verify(callback, times(1)).succeeded();
}

@Test
public void testSendWithResponseInfoAndContentAndLastContentFalse() throws Exception
{
Expand Down

0 comments on commit d65b511

Please sign in to comment.