Make sure Stream.cancel() can be called multiple times.

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=72928049
This commit is contained in:
simonma 2014-08-08 14:51:01 -07:00 committed by Eric Anderson
parent f2c4c8555a
commit 1966d5b7f3
1 changed files with 5 additions and 1 deletions

View File

@ -14,6 +14,7 @@ import com.google.net.stubby.newtransport.ClientStream;
import com.google.net.stubby.newtransport.ClientTransport; import com.google.net.stubby.newtransport.ClientTransport;
import com.google.net.stubby.newtransport.InputStreamDeframer; import com.google.net.stubby.newtransport.InputStreamDeframer;
import com.google.net.stubby.newtransport.StreamListener; import com.google.net.stubby.newtransport.StreamListener;
import com.google.net.stubby.newtransport.StreamState;
import com.google.net.stubby.transport.Transport; import com.google.net.stubby.transport.Transport;
import com.google.net.stubby.transport.Transport.Code; import com.google.net.stubby.transport.Transport.Code;
@ -425,7 +426,10 @@ public class OkHttpClientTransport extends AbstractClientTransport {
@Override @Override
public void cancel() { public void cancel() {
Preconditions.checkState(streamId != 0, "streamId should be set"); if (streamId == 0) {
// This should only happens when the stream was failed in constructor.
Preconditions.checkState(state() == StreamState.CLOSED, "A unclosed stream has no id");
}
outboundPhase = Phase.STATUS; outboundPhase = Phase.STATUS;
if (finishStream(streamId, toGrpcStatus(ErrorCode.CANCEL))) { if (finishStream(streamId, toGrpcStatus(ErrorCode.CANCEL))) {
frameWriter.rstStream(streamId, ErrorCode.CANCEL); frameWriter.rstStream(streamId, ErrorCode.CANCEL);