62
62
import java .util .concurrent .Executors ;
63
63
import java .util .concurrent .TimeUnit ;
64
64
import org .junit .After ;
65
- import org .junit .AfterClass ;
66
65
import org .junit .Assert ;
67
- import org .junit .BeforeClass ;
66
+ import org .junit .Before ;
68
67
import org .junit .Test ;
69
68
import org .junit .runner .RunWith ;
70
69
import org .junit .runners .JUnit4 ;
@@ -107,9 +106,7 @@ public class HttpJsonDirectServerStreamingCallableTest {
107
106
.setType (MethodType .SERVER_STREAMING )
108
107
.build ();
109
108
110
- private static final MockHttpService MOCK_SERVICE =
111
- new MockHttpService (
112
- Collections .singletonList (METHOD_SERVER_STREAMING_RECOGNIZE ), "google.com:443" );
109
+ private MockHttpService mockService ;
113
110
114
111
private static final Color DEFAULT_REQUEST = Color .newBuilder ().setRed (0.5f ).build ();
115
112
private static final Color ASYNC_REQUEST = DEFAULT_REQUEST .toBuilder ().setGreen (1000 ).build ();
@@ -120,22 +117,25 @@ public class HttpJsonDirectServerStreamingCallableTest {
120
117
Money .newBuilder ().setCurrencyCode ("UAH" ).setUnits (255 ).build ();
121
118
private static final int AWAIT_TERMINATION_SECONDS = 10 ;
122
119
123
- private static ServerStreamingCallSettings <Color , Money > streamingCallSettings ;
124
- private static ServerStreamingCallable <Color , Money > streamingCallable ;
120
+ private ServerStreamingCallSettings <Color , Money > streamingCallSettings ;
121
+ private ServerStreamingCallable <Color , Money > streamingCallable ;
125
122
126
- private static ManagedHttpJsonChannel channel ;
127
- private static ClientContext clientContext ;
128
- private static ExecutorService executorService ;
123
+ private ManagedHttpJsonChannel channel ;
124
+ private ClientContext clientContext ;
125
+ private ExecutorService executorService ;
129
126
130
- @ BeforeClass
131
- public static void initialize () throws IOException {
127
+ @ Before
128
+ public void initialize () throws IOException {
129
+ mockService =
130
+ new MockHttpService (
131
+ Collections .singletonList (METHOD_SERVER_STREAMING_RECOGNIZE ), "google.com:443" );
132
132
executorService = Executors .newFixedThreadPool (2 );
133
133
channel =
134
134
new ManagedHttpJsonInterceptorChannel (
135
135
ManagedHttpJsonChannel .newBuilder ()
136
136
.setEndpoint ("google.com:443" )
137
137
.setExecutor (executorService )
138
- .setHttpTransport (MOCK_SERVICE )
138
+ .setHttpTransport (mockService )
139
139
.build (),
140
140
new HttpJsonHeaderInterceptor (Collections .singletonMap ("header-key" , "headerValue" )));
141
141
EndpointContext endpointContext = Mockito .mock (EndpointContext .class );
@@ -158,25 +158,23 @@ public static void initialize() throws IOException {
158
158
HttpJsonCallSettings .create (METHOD_SERVER_STREAMING_RECOGNIZE ),
159
159
streamingCallSettings ,
160
160
clientContext );
161
+
162
+ mockService .reset ();
161
163
}
162
164
163
- @ AfterClass
164
- public static void destroy () throws InterruptedException {
165
+ @ After
166
+ public void destroy () throws InterruptedException {
165
167
executorService .shutdown ();
166
168
channel .shutdown ();
167
169
168
170
executorService .awaitTermination (AWAIT_TERMINATION_SECONDS , TimeUnit .SECONDS );
169
171
channel .awaitTermination (AWAIT_TERMINATION_SECONDS , TimeUnit .SECONDS );
170
- }
171
-
172
- @ After
173
- public void tearDown () throws InterruptedException {
174
- MOCK_SERVICE .reset ();
172
+ mockService .reset ();
175
173
}
176
174
177
175
@ Test
178
176
public void testBadContext () {
179
- MOCK_SERVICE .addResponse (new Money [] {DEFAULT_RESPONSE });
177
+ mockService .addResponse (new Money [] {DEFAULT_RESPONSE });
180
178
// Create a local callable with a bad context
181
179
ServerStreamingCallable <Color , Money > streamingCallable =
182
180
HttpJsonCallableFactory .createServerStreamingCallable (
@@ -202,7 +200,7 @@ public void testBadContext() {
202
200
203
201
@ Test
204
202
public void testServerStreamingStart () throws InterruptedException {
205
- MOCK_SERVICE .addResponse (new Money [] {DEFAULT_RESPONSE });
203
+ mockService .addResponse (new Money [] {DEFAULT_RESPONSE });
206
204
CountDownLatch latch = new CountDownLatch (1 );
207
205
MoneyObserver moneyObserver = new MoneyObserver (true , latch );
208
206
@@ -217,7 +215,7 @@ public void testServerStreamingStart() throws InterruptedException {
217
215
218
216
@ Test
219
217
public void testServerStreaming () throws InterruptedException {
220
- MOCK_SERVICE .addResponse (new Money [] {DEFAULT_RESPONSE , DEFAULTER_RESPONSE });
218
+ mockService .addResponse (new Money [] {DEFAULT_RESPONSE , DEFAULTER_RESPONSE });
221
219
CountDownLatch latch = new CountDownLatch (3 );
222
220
MoneyObserver moneyObserver = new MoneyObserver (true , latch );
223
221
@@ -231,7 +229,7 @@ public void testServerStreaming() throws InterruptedException {
231
229
232
230
@ Test
233
231
public void testManualFlowControl () throws Exception {
234
- MOCK_SERVICE .addResponse (new Money [] {DEFAULT_RESPONSE });
232
+ mockService .addResponse (new Money [] {DEFAULT_RESPONSE });
235
233
CountDownLatch latch = new CountDownLatch (2 );
236
234
MoneyObserver moneyObserver = new MoneyObserver (false , latch );
237
235
@@ -251,7 +249,7 @@ public void testManualFlowControl() throws Exception {
251
249
252
250
@ Test
253
251
public void testCancelClientCall () throws Exception {
254
- MOCK_SERVICE .addResponse (new Money [] {DEFAULT_RESPONSE });
252
+ mockService .addResponse (new Money [] {DEFAULT_RESPONSE });
255
253
CountDownLatch latch = new CountDownLatch (1 );
256
254
MoneyObserver moneyObserver = new MoneyObserver (false , latch );
257
255
@@ -267,7 +265,7 @@ public void testCancelClientCall() throws Exception {
267
265
268
266
@ Test
269
267
public void testOnResponseError () throws Throwable {
270
- MOCK_SERVICE .addException (404 , new RuntimeException ("some error" ));
268
+ mockService .addException (404 , new RuntimeException ("some error" ));
271
269
272
270
CountDownLatch latch = new CountDownLatch (1 );
273
271
MoneyObserver moneyObserver = new MoneyObserver (true , latch );
@@ -292,7 +290,7 @@ public void testOnResponseError() throws Throwable {
292
290
293
291
@ Test
294
292
public void testObserverErrorCancelsCall () throws Throwable {
295
- MOCK_SERVICE .addResponse (new Money [] {DEFAULT_RESPONSE });
293
+ mockService .addResponse (new Money [] {DEFAULT_RESPONSE });
296
294
final RuntimeException expectedCause = new RuntimeException ("some error" );
297
295
final SettableApiFuture <Throwable > actualErrorF = SettableApiFuture .create ();
298
296
@@ -332,7 +330,7 @@ protected void onCompleteImpl() {
332
330
333
331
@ Test
334
332
public void testBlockingServerStreaming () {
335
- MOCK_SERVICE .addResponse (new Money [] {DEFAULT_RESPONSE });
333
+ mockService .addResponse (new Money [] {DEFAULT_RESPONSE });
336
334
Color request = Color .newBuilder ().setRed (0.5f ).build ();
337
335
ServerStream <Money > response = streamingCallable .call (request );
338
336
List <Money > responseData = Lists .newArrayList (response );
@@ -344,7 +342,7 @@ public void testBlockingServerStreaming() {
344
342
// This test ensures that the server-side streaming does not exceed the timeout value
345
343
@ Test
346
344
public void testDeadlineExceededServerStreaming () throws InterruptedException {
347
- MOCK_SERVICE .addResponse (
345
+ mockService .addResponse (
348
346
new Money [] {DEFAULT_RESPONSE , DEFAULTER_RESPONSE }, java .time .Duration .ofSeconds (5 ));
349
347
Color request = Color .newBuilder ().setRed (0.5f ).build ();
350
348
CountDownLatch latch = new CountDownLatch (1 );
0 commit comments