|
16 | 16 | package com.google.cloud.bigtable.gaxx.reframing; |
17 | 17 |
|
18 | 18 | import com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable; |
| 19 | +import com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable.StreamControllerStash; |
19 | 20 | import com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver; |
20 | 21 | import com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockServerStreamingCall; |
21 | 22 | import com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockServerStreamingCallable; |
|
34 | 35 | import java.util.concurrent.Executors; |
35 | 36 | import java.util.concurrent.Future; |
36 | 37 | import java.util.concurrent.TimeUnit; |
| 38 | +import java.util.concurrent.atomic.AtomicInteger; |
37 | 39 | import org.junit.After; |
38 | 40 | import org.junit.Before; |
39 | 41 | import org.junit.Test; |
@@ -311,6 +313,67 @@ public void run() { |
311 | 313 | Truth.assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue(); |
312 | 314 | } |
313 | 315 |
|
| 316 | + @Test |
| 317 | + public void testReframerPushError() throws Exception { |
| 318 | + MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true); |
| 319 | + Reframer<String, String> reframer = |
| 320 | + new DasherizingReframer(1) { |
| 321 | + @Override |
| 322 | + public void push(String response) { |
| 323 | + if (response.equals("boom")) { |
| 324 | + throw new IllegalStateException("fake error"); |
| 325 | + } |
| 326 | + super.push(response); |
| 327 | + } |
| 328 | + }; |
| 329 | + |
| 330 | + ReframingResponseObserver<String, String> middleware = |
| 331 | + new ReframingResponseObserver<>(outerObserver, reframer); |
| 332 | + ServerStreamingStashCallable<String, String> innerCallable = |
| 333 | + new ServerStreamingStashCallable<>(ImmutableList.of("a", "boom", "c")); |
| 334 | + |
| 335 | + innerCallable.call("request", middleware); |
| 336 | + |
| 337 | + Truth.assertThat(outerObserver.getFinalError()).isInstanceOf(IllegalStateException.class); |
| 338 | + Truth.assertThat(outerObserver.getFinalError()).hasMessage("fake error"); |
| 339 | + Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a"); |
| 340 | + Truth.assertThat(outerObserver.popNextResponse()).isNull(); |
| 341 | + } |
| 342 | + |
| 343 | + @Test |
| 344 | + public void testReframerPopError() { |
| 345 | + final AtomicInteger popCount = new AtomicInteger(); |
| 346 | + |
| 347 | + MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true); |
| 348 | + Reframer<String, String> reframer = |
| 349 | + new DasherizingReframer(1) { |
| 350 | + @Override |
| 351 | + public String pop() { |
| 352 | + if (popCount.incrementAndGet() == 2) { |
| 353 | + throw new IllegalStateException("fake error"); |
| 354 | + } |
| 355 | + return super.pop(); |
| 356 | + } |
| 357 | + }; |
| 358 | + |
| 359 | + ReframingResponseObserver<String, String> middleware = |
| 360 | + new ReframingResponseObserver<>(outerObserver, reframer); |
| 361 | + ServerStreamingStashCallable<String, String> innerCallable = |
| 362 | + new ServerStreamingStashCallable<>(ImmutableList.of("a", "boom", "c")); |
| 363 | + |
| 364 | + innerCallable.call("request", middleware); |
| 365 | + StreamControllerStash<String> lastCall = innerCallable.popLastCall(); |
| 366 | + |
| 367 | + Truth.assertThat(outerObserver.getFinalError()).isInstanceOf(IllegalStateException.class); |
| 368 | + Truth.assertThat(outerObserver.getFinalError()).hasMessage("fake error"); |
| 369 | + Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a"); |
| 370 | + Truth.assertThat(outerObserver.popNextResponse()).isNull(); |
| 371 | + Truth.assertThat(popCount.get()).isEqualTo(2); |
| 372 | + |
| 373 | + Truth.assertThat(lastCall.getError()).isInstanceOf(CancellationException.class); |
| 374 | + Truth.assertThat(lastCall.getNumDelivered()).isEqualTo(2); |
| 375 | + } |
| 376 | + |
314 | 377 | /** |
315 | 378 | * A simple implementation of a {@link Reframer}. The input string is split by dash, and the |
316 | 379 | * output is concatenated by dashes. The test can verify M:N behavior by adjusting the |
|
0 commit comments