Skip to content

Commit ec7f00a

Browse files
core,testing: make MethodDescriptor final and add Test helper
1 parent ce3a94b commit ec7f00a

File tree

5 files changed

+119
-27
lines changed

5 files changed

+119
-27
lines changed

core/src/main/java/io/grpc/MethodDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* <p>Can be constructed manually but will often be generated by stub code generators.
5151
*/
5252
@Immutable
53-
public class MethodDescriptor<ReqT, RespT> {
53+
public final class MethodDescriptor<ReqT, RespT> {
5454

5555
private final MethodType type;
5656
private final String fullMethodName;

core/src/test/java/io/grpc/ClientInterceptorsTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import io.grpc.ClientInterceptors.CheckedForwardingClientCall;
5252
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
5353
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
54+
import io.grpc.testing.TestMethodDescriptors;
5455

5556
import org.junit.Before;
5657
import org.junit.Test;
@@ -74,8 +75,7 @@ public class ClientInterceptorsTest {
7475

7576
private BaseClientCall call = new BaseClientCall();
7677

77-
@Mock
78-
private MethodDescriptor<String, Integer> method;
78+
private final MethodDescriptor<Void, Void> method = TestMethodDescriptors.noopMethod();
7979

8080
/**
8181
* Sets up mocks.
@@ -270,8 +270,8 @@ public void start(ClientCall.Listener<RespT> responseListener, Metadata headers)
270270
};
271271
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
272272
@SuppressWarnings("unchecked")
273-
ClientCall.Listener<Integer> listener = mock(ClientCall.Listener.class);
274-
ClientCall<String, Integer> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
273+
ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
274+
ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
275275
// start() on the intercepted call will eventually reach the call created by the real channel
276276
interceptedCall.start(listener, new Metadata());
277277
// The headers passed to the real channel call will contain the information inserted by the
@@ -306,8 +306,8 @@ public void onHeaders(Metadata headers) {
306306
};
307307
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
308308
@SuppressWarnings("unchecked")
309-
ClientCall.Listener<Integer> listener = mock(ClientCall.Listener.class);
310-
ClientCall<String, Integer> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
309+
ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
310+
ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
311311
interceptedCall.start(listener, new Metadata());
312312
// Capture the underlying call listener that will receive headers from the transport.
313313

@@ -330,16 +330,16 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
330330
}
331331
};
332332
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
333-
ClientCall<String, Integer> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
333+
ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
334334
assertNotSame(call, interceptedCall);
335335
@SuppressWarnings("unchecked")
336-
ClientCall.Listener<Integer> listener = mock(ClientCall.Listener.class);
336+
ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
337337
Metadata headers = new Metadata();
338338
interceptedCall.start(listener, headers);
339339
assertSame(listener, call.listener);
340340
assertSame(headers, call.headers);
341-
interceptedCall.sendMessage("request");
342-
assertThat(call.messages).containsExactly("request");
341+
interceptedCall.sendMessage(null /*request*/);
342+
assertThat(call.messages).containsExactly((Void) null /*request*/);
343343
interceptedCall.halfClose();
344344
assertTrue(call.halfClosed);
345345
interceptedCall.request(1);
@@ -368,11 +368,11 @@ protected void checkedStart(ClientCall.Listener<RespT> responseListener, Metadat
368368
};
369369
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);
370370
@SuppressWarnings("unchecked")
371-
ClientCall.Listener<Integer> listener = mock(ClientCall.Listener.class);
372-
ClientCall<String, Integer> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
371+
ClientCall.Listener<Void> listener = mock(ClientCall.Listener.class);
372+
ClientCall<Void, Void> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);
373373
assertNotSame(call, interceptedCall);
374374
interceptedCall.start(listener, new Metadata());
375-
interceptedCall.sendMessage("request");
375+
interceptedCall.sendMessage(null /*request*/);
376376
interceptedCall.halfClose();
377377
interceptedCall.request(1);
378378
call.done = true;

core/src/test/java/io/grpc/ContextsTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import static org.junit.Assert.assertThat;
4343
import static org.junit.Assert.assertTrue;
4444
import static org.junit.Assert.fail;
45-
import static org.mockito.Mockito.mock;
4645

4746
import io.grpc.internal.FakeClock;
4847
import io.grpc.testing.NoopServerCall;
@@ -66,8 +65,6 @@ public class ContextsTest {
6665
/** For use in comparing context by reference. */
6766
private Context uniqueContext = Context.ROOT.withValue(contextKey, new Object());
6867
@SuppressWarnings("unchecked")
69-
private MethodDescriptor<Object, Object> method = mock(MethodDescriptor.class);
70-
@SuppressWarnings("unchecked")
7168
private ServerCall<Object, Object> call = new NoopServerCall<Object, Object>();
7269
private Metadata headers = new Metadata();
7370

@@ -108,7 +105,6 @@ public void interceptCall_basic() {
108105
@Override
109106
public ServerCall.Listener<Object> startCall(
110107
ServerCall<Object, Object> call, Metadata headers) {
111-
assertSame(ContextsTest.this.method, method);
112108
assertSame(ContextsTest.this.call, call);
113109
assertSame(ContextsTest.this.headers, headers);
114110
assertSame(uniqueContext, Context.current());

okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import io.grpc.okhttp.internal.framed.Header;
8585
import io.grpc.okhttp.internal.framed.HeadersMode;
8686
import io.grpc.okhttp.internal.framed.Settings;
87+
import io.grpc.testing.TestMethodDescriptors;
8788

8889
import okio.Buffer;
8990

@@ -133,8 +134,9 @@ public class OkHttpClientTransportTest {
133134

134135
@Mock
135136
private FrameWriter frameWriter;
136-
@Mock
137-
MethodDescriptor<?, ?> method;
137+
138+
private MethodDescriptor<Void, Void> method = TestMethodDescriptors.noopMethod();
139+
138140
@Mock
139141
private ManagedClientTransport.Listener transportListener;
140142
private OkHttpClientTransport clientTransport;
@@ -149,8 +151,6 @@ public class OkHttpClientTransportTest {
149151
public void setUp() {
150152
MockitoAnnotations.initMocks(this);
151153
executor = Executors.newCachedThreadPool();
152-
when(method.getFullMethodName()).thenReturn("fakemethod");
153-
when(method.getType()).thenReturn(MethodType.UNARY);
154154
when(frameWriter.maxDataLength()).thenReturn(Integer.MAX_VALUE);
155155
frameReader = new MockFrameReader();
156156
}
@@ -441,7 +441,7 @@ public void addDefaultUserAgent() throws Exception {
441441
GrpcUtil.getGrpcUserAgent("okhttp", null));
442442
List<Header> expectedHeaders = Arrays.asList(SCHEME_HEADER, METHOD_HEADER,
443443
new Header(Header.TARGET_AUTHORITY, "notarealauthority:80"),
444-
new Header(Header.TARGET_PATH, "/fakemethod"),
444+
new Header(Header.TARGET_PATH, "/" + method.getFullMethodName()),
445445
userAgentHeader, CONTENT_TYPE_HEADER, TE_HEADER);
446446
verify(frameWriter, timeout(TIME_OUT_MS))
447447
.synStream(eq(false), eq(false), eq(3), eq(0), eq(expectedHeaders));
@@ -457,7 +457,7 @@ public void overrideDefaultUserAgent() throws Exception {
457457
stream.start(listener);
458458
List<Header> expectedHeaders = Arrays.asList(SCHEME_HEADER, METHOD_HEADER,
459459
new Header(Header.TARGET_AUTHORITY, "notarealauthority:80"),
460-
new Header(Header.TARGET_PATH, "/fakemethod"),
460+
new Header(Header.TARGET_PATH, "/" + method.getFullMethodName()),
461461
new Header(GrpcUtil.USER_AGENT_KEY.name(),
462462
GrpcUtil.getGrpcUserAgent("okhttp", "fakeUserAgent")),
463463
CONTENT_TYPE_HEADER, TE_HEADER);
@@ -967,21 +967,33 @@ public void unaryHeadersShouldNotBeFlushed() throws Exception {
967967

968968
@Test
969969
public void serverStreamingHeadersShouldNotBeFlushed() throws Exception {
970-
when(method.getType()).thenReturn(MethodType.SERVER_STREAMING);
970+
method = MethodDescriptor.create(
971+
MethodType.SERVER_STREAMING,
972+
method.getFullMethodName(),
973+
TestMethodDescriptors.noopMarshaller(),
974+
TestMethodDescriptors.noopMarshaller());
971975
shouldHeadersBeFlushed(false);
972976
shutdownAndVerify();
973977
}
974978

975979
@Test
976980
public void clientStreamingHeadersShouldBeFlushed() throws Exception {
977-
when(method.getType()).thenReturn(MethodType.CLIENT_STREAMING);
981+
method = MethodDescriptor.create(
982+
MethodType.CLIENT_STREAMING,
983+
method.getFullMethodName(),
984+
TestMethodDescriptors.noopMarshaller(),
985+
TestMethodDescriptors.noopMarshaller());
978986
shouldHeadersBeFlushed(true);
979987
shutdownAndVerify();
980988
}
981989

982990
@Test
983991
public void duplexStreamingHeadersShouldNotBeFlushed() throws Exception {
984-
when(method.getType()).thenReturn(MethodType.BIDI_STREAMING);
992+
method = MethodDescriptor.create(
993+
MethodType.BIDI_STREAMING,
994+
method.getFullMethodName(),
995+
TestMethodDescriptors.noopMarshaller(),
996+
TestMethodDescriptors.noopMarshaller());
985997
shouldHeadersBeFlushed(true);
986998
shutdownAndVerify();
987999
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2017, Google Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* * Neither the name of Google Inc. nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package io.grpc.testing;
33+
34+
import io.grpc.ExperimentalApi;
35+
import io.grpc.MethodDescriptor;
36+
import io.grpc.MethodDescriptor.MethodType;
37+
38+
import java.io.ByteArrayInputStream;
39+
import java.io.InputStream;
40+
41+
/**
42+
* A collection of method descriptor constructors useful for tests. These are useful if you need
43+
* a descriptor, but don't really care how it works.
44+
*/
45+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2600")
46+
public final class TestMethodDescriptors {
47+
private TestMethodDescriptors() {}
48+
49+
/**
50+
* Creates a new method descriptor that always creates zero length messages, and always parses to
51+
* null objects.
52+
*/
53+
public static MethodDescriptor<Void, Void> noopMethod() {
54+
return noopMethod("service_foo", "method_bar");
55+
}
56+
57+
private static MethodDescriptor<Void, Void> noopMethod(
58+
String serviceName, String methodName) {
59+
return MethodDescriptor.create(
60+
MethodType.UNARY,
61+
MethodDescriptor.generateFullMethodName(serviceName, methodName),
62+
noopMarshaller(),
63+
noopMarshaller());
64+
}
65+
66+
/**
67+
* Creates a new marshaller that does nothing.
68+
*/
69+
public static MethodDescriptor.Marshaller<Void> noopMarshaller() {
70+
return new NoopMarshaller();
71+
}
72+
73+
private static final class NoopMarshaller implements MethodDescriptor.Marshaller<Void> {
74+
@Override
75+
public InputStream stream(Void value) {
76+
return new ByteArrayInputStream(new byte[]{});
77+
}
78+
79+
@Override
80+
public Void parse(InputStream stream) {
81+
return null;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)