Forbid calling next more than once in interceptors.

Will throw IllegalStateException.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=79717262
This commit is contained in:
zhangkun 2014-11-11 18:03:27 -08:00 committed by Eric Anderson
parent 4073a8a1a7
commit aa0b7cc9af
4 changed files with 38 additions and 2 deletions

View File

@ -53,7 +53,7 @@ public class ClientInterceptors {
private static class ProcessInterceptorChannel implements Channel {
private final Channel channel;
private final Iterator<ClientInterceptor> interceptors;
private Iterator<ClientInterceptor> interceptors;
private ProcessInterceptorChannel(Channel channel, Iterable<ClientInterceptor> interceptors) {
this.channel = channel;
@ -62,9 +62,13 @@ public class ClientInterceptors {
@Override
public <ReqT, RespT> Call<ReqT, RespT> newCall(MethodDescriptor<ReqT, RespT> method) {
if (interceptors.hasNext()) {
if (interceptors != null && interceptors.hasNext()) {
return interceptors.next().interceptCall(method, this);
} else {
Preconditions.checkState(interceptors != null,
"The channel has already been called. "
+ "Some interceptor must have called on \"next\" twice.");
interceptors = null;
return channel.newCall(method);
}
}

View File

@ -93,6 +93,9 @@ public class ServerInterceptors {
if (interceptors != null && interceptors.hasNext()) {
return interceptors.next().interceptCall(method, call, headers, this);
} else {
Preconditions.checkState(interceptors != null,
"The call handler has already been called. "
+ "Some interceptor must have called on \"next\" twice.");
interceptors = null;
return callHandler.startCall(method, call, headers);
}

View File

@ -81,6 +81,20 @@ public class ClientInterceptorsTest {
verifyNoMoreInteractions(channel, interceptor);
}
@Test(expected = IllegalStateException.class)
public void callNextTwice() {
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> Call<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
Channel next) {
next.newCall(method);
return next.newCall(method);
}
};
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
intercepted.newCall(method);
}
@Test
public void ordered() {
final List<String> order = new ArrayList<String>();

View File

@ -116,6 +116,21 @@ public class ServerInterceptorsTest {
verifyNoMoreInteractions(handler2);
}
@Test(expected = IllegalStateException.class)
public void callNextTwice() {
ServerInterceptor interceptor = new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(String method,
ServerCall<RespT> call, Headers headers, ServerCallHandler<ReqT, RespT> next) {
next.startCall(method, call, headers);
return next.startCall(method, call, headers);
}
};
ServerServiceDefinition intercepted = ServerInterceptors.intercept(serviceDefinition,
interceptor);
getSoleMethod(intercepted).getServerCallHandler().startCall(methodName, call, headers);
}
@Test
public void ordered() {
final List<String> order = new ArrayList<String>();