72
72
73
73
@ RunWith (JUnit4 .class )
74
74
public class HttpJsonDirectServerStreamingCallableTest {
75
- private static final ApiMethodDescriptor <Color , Money > METHOD_SERVER_STREAMING_RECOGNIZE =
76
- ApiMethodDescriptor .<Color , Money >newBuilder ()
77
- .setFullMethodName ("google.cloud.v1.Fake/ServerStreamingRecognize" )
78
- .setHttpMethod ("POST" )
79
- .setRequestFormatter (
80
- ProtoMessageRequestFormatter .<Color >newBuilder ()
81
- .setPath (
82
- "/fake/v1/recognize/{blue}" ,
83
- request -> {
84
- Map <String , String > fields = new HashMap <>();
85
- ProtoRestSerializer <Field > serializer = ProtoRestSerializer .create ();
86
- serializer .putPathParam (fields , "blue" , request .getBlue ());
87
- return fields ;
88
- })
89
- .setQueryParamsExtractor (
90
- request -> {
91
- Map <String , List <String >> fields = new HashMap <>();
92
- ProtoRestSerializer <Field > serializer = ProtoRestSerializer .create ();
93
- serializer .putQueryParam (fields , "red" , request .getRed ());
94
- return fields ;
95
- })
96
- .setRequestBodyExtractor (
97
- request ->
98
- ProtoRestSerializer .create ()
99
- .toBody (
100
- "*" , request .toBuilder ().clearBlue ().clearRed ().build (), false ))
101
- .build ())
102
- .setResponseParser (
103
- ProtoMessageResponseParser .<Money >newBuilder ()
104
- .setDefaultInstance (Money .getDefaultInstance ())
105
- .build ())
106
- .setType (MethodType .SERVER_STREAMING )
107
- .build ();
108
-
109
- private MockHttpService mockService ;
110
-
111
75
private static final Color DEFAULT_REQUEST = Color .newBuilder ().setRed (0.5f ).build ();
112
76
private static final Color ASYNC_REQUEST = DEFAULT_REQUEST .toBuilder ().setGreen (1000 ).build ();
113
77
private static final Color ERROR_REQUEST = Color .newBuilder ().setRed (-1 ).build ();
114
78
private static final Money DEFAULT_RESPONSE =
115
79
Money .newBuilder ().setCurrencyCode ("USD" ).setUnits (127 ).build ();
116
80
private static final Money DEFAULTER_RESPONSE =
117
81
Money .newBuilder ().setCurrencyCode ("UAH" ).setUnits (255 ).build ();
118
- private static final int AWAIT_TERMINATION_SECONDS = 10 ;
119
82
120
83
private ServerStreamingCallSettings <Color , Money > streamingCallSettings ;
121
84
private ServerStreamingCallable <Color , Money > streamingCallable ;
122
85
123
86
private ManagedHttpJsonChannel channel ;
124
87
private ClientContext clientContext ;
125
88
private ExecutorService executorService ;
89
+ private MockHttpService mockService ;
90
+ ApiMethodDescriptor <Color , Money > methodServerStreamingRecognize ;
126
91
127
92
@ Before
128
93
public void initialize () throws IOException {
129
- mockService =
94
+ initialize (Duration .ofSeconds (30 ));
95
+ }
96
+
97
+ public void initialize (Duration timeout ) throws IOException {
98
+ this .methodServerStreamingRecognize =
99
+ ApiMethodDescriptor .<Color , Money >newBuilder ()
100
+ .setFullMethodName ("google.cloud.v1.Fake/ServerStreamingRecognize" )
101
+ .setHttpMethod ("POST" )
102
+ .setRequestFormatter (
103
+ ProtoMessageRequestFormatter .<Color >newBuilder ()
104
+ .setPath (
105
+ "/fake/v1/recognize/{blue}" ,
106
+ request -> {
107
+ Map <String , String > fields = new HashMap <>();
108
+ ProtoRestSerializer <Field > serializer = ProtoRestSerializer .create ();
109
+ serializer .putPathParam (fields , "blue" , request .getBlue ());
110
+ return fields ;
111
+ })
112
+ .setQueryParamsExtractor (
113
+ request -> {
114
+ Map <String , List <String >> fields = new HashMap <>();
115
+ ProtoRestSerializer <Field > serializer = ProtoRestSerializer .create ();
116
+ serializer .putQueryParam (fields , "red" , request .getRed ());
117
+ return fields ;
118
+ })
119
+ .setRequestBodyExtractor (
120
+ request ->
121
+ ProtoRestSerializer .create ()
122
+ .toBody (
123
+ "*" , request .toBuilder ().clearBlue ().clearRed ().build (), false ))
124
+ .build ())
125
+ .setResponseParser (
126
+ ProtoMessageResponseParser .<Money >newBuilder ()
127
+ .setDefaultInstance (Money .getDefaultInstance ())
128
+ .build ())
129
+ .setType (MethodType .SERVER_STREAMING )
130
+ .build ();
131
+ this .mockService =
130
132
new MockHttpService (
131
- Collections .singletonList (METHOD_SERVER_STREAMING_RECOGNIZE ), "google.com:443" );
133
+ Collections .singletonList (methodServerStreamingRecognize ), "google.com:443" );
132
134
executorService = Executors .newFixedThreadPool (2 );
133
135
channel =
134
136
new ManagedHttpJsonInterceptorChannel (
@@ -148,28 +150,22 @@ public void initialize() throws IOException {
148
150
.setTransportChannel (HttpJsonTransportChannel .create (channel ))
149
151
.setDefaultCallContext (
150
152
HttpJsonCallContext .of (channel , HttpJsonCallOptions .DEFAULT )
151
- .withTimeout (Duration . ofSeconds ( 3 ) )
153
+ .withTimeout (timeout )
152
154
.withEndpointContext (endpointContext ))
153
155
.build ();
154
156
155
157
streamingCallSettings = ServerStreamingCallSettings .<Color , Money >newBuilder ().build ();
156
158
streamingCallable =
157
159
HttpJsonCallableFactory .createServerStreamingCallable (
158
- HttpJsonCallSettings .create (METHOD_SERVER_STREAMING_RECOGNIZE ),
160
+ HttpJsonCallSettings .create (methodServerStreamingRecognize ),
159
161
streamingCallSettings ,
160
162
clientContext );
161
-
162
- mockService .reset ();
163
163
}
164
164
165
165
@ After
166
166
public void destroy () throws InterruptedException {
167
167
executorService .shutdown ();
168
168
channel .shutdown ();
169
-
170
- executorService .awaitTermination (AWAIT_TERMINATION_SECONDS , TimeUnit .SECONDS );
171
- channel .awaitTermination (AWAIT_TERMINATION_SECONDS , TimeUnit .SECONDS );
172
- mockService .reset ();
173
169
}
174
170
175
171
@ Test
@@ -178,7 +174,7 @@ public void testBadContext() {
178
174
// Create a local callable with a bad context
179
175
ServerStreamingCallable <Color , Money > streamingCallable =
180
176
HttpJsonCallableFactory .createServerStreamingCallable (
181
- HttpJsonCallSettings .create (METHOD_SERVER_STREAMING_RECOGNIZE ),
177
+ HttpJsonCallSettings .create (this . methodServerStreamingRecognize ),
182
178
streamingCallSettings ,
183
179
clientContext
184
180
.toBuilder ()
@@ -337,9 +333,12 @@ public void testBlockingServerStreaming() {
337
333
338
334
// This test ensures that the server-side streaming does not exceed the timeout value
339
335
@ Test
340
- public void testDeadlineExceededServerStreaming () throws InterruptedException {
336
+ public void testDeadlineExceededServerStreaming () throws InterruptedException , IOException {
337
+ // set a low timeout to trigger deadline-exceeded sooner
338
+ initialize (Duration .ofSeconds (1 ));
339
+
341
340
mockService .addResponse (
342
- new Money [] {DEFAULT_RESPONSE , DEFAULTER_RESPONSE }, java .time .Duration .ofSeconds (5 ));
341
+ new Money [] {DEFAULT_RESPONSE , DEFAULTER_RESPONSE }, java .time .Duration .ofSeconds (30 ));
343
342
Color request = Color .newBuilder ().setRed (0.5f ).build ();
344
343
CountDownLatch latch = new CountDownLatch (1 );
345
344
MoneyObserver moneyObserver = new MoneyObserver (false , latch );
@@ -349,7 +348,7 @@ public void testDeadlineExceededServerStreaming() throws InterruptedException {
349
348
moneyObserver .controller .request (2 );
350
349
// Set the latch's await time to above the context's timeout value to ensure that
351
350
// the latch has been released.
352
- Truth .assertThat (latch .await (5000 , TimeUnit .MILLISECONDS )).isTrue ();
351
+ Truth .assertThat (latch .await (30 , TimeUnit .SECONDS )).isTrue ();
353
352
354
353
Truth .assertThat (moneyObserver .error ).isInstanceOf (DeadlineExceededException .class );
355
354
Truth .assertThat (moneyObserver .error ).hasMessageThat ().isEqualTo ("Deadline exceeded" );
0 commit comments