|
39 | 39 | import java.util.List; |
40 | 40 | import java.util.UUID; |
41 | 41 | import java.util.concurrent.ExecutionException; |
| 42 | +import java.util.logging.Logger; |
42 | 43 | import org.junit.Before; |
43 | 44 | import org.junit.Test; |
44 | 45 | import org.junit.runner.RunWith; |
45 | 46 | import org.junit.runners.JUnit4; |
46 | 47 |
|
47 | 48 | @RunWith(JUnit4.class) |
48 | 49 | public class ConnectionWorkerTest { |
| 50 | + private static final Logger log = Logger.getLogger(StreamWriter.class.getName()); |
49 | 51 | private static final String TEST_STREAM_1 = "projects/p1/datasets/d1/tables/t1/streams/s1"; |
50 | 52 | private static final String TEST_STREAM_2 = "projects/p2/datasets/d2/tables/t2/streams/s2"; |
51 | 53 | private static final String TEST_TRACE_ID = "DATAFLOW:job_id"; |
@@ -84,10 +86,12 @@ public void testMultiplexedAppendSuccess() throws Exception { |
84 | 86 | StreamWriter sw1 = |
85 | 87 | StreamWriter.newBuilder(TEST_STREAM_1, client) |
86 | 88 | .setWriterSchema(createProtoSchema("foo")) |
| 89 | + .setLocation("us") |
87 | 90 | .build(); |
88 | 91 | StreamWriter sw2 = |
89 | 92 | StreamWriter.newBuilder(TEST_STREAM_2, client) |
90 | 93 | .setWriterSchema(createProtoSchema("complicate")) |
| 94 | + .setLocation("us") |
91 | 95 | .build(); |
92 | 96 | // We do a pattern of: |
93 | 97 | // send to stream1, string1 |
@@ -205,11 +209,20 @@ public void testAppendInSameStream_switchSchema() throws Exception { |
205 | 209 | // send to stream1, schema1 |
206 | 210 | // ... |
207 | 211 | StreamWriter sw1 = |
208 | | - StreamWriter.newBuilder(TEST_STREAM_1, client).setWriterSchema(schema1).build(); |
| 212 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 213 | + .setLocation("us") |
| 214 | + .setWriterSchema(schema1) |
| 215 | + .build(); |
209 | 216 | StreamWriter sw2 = |
210 | | - StreamWriter.newBuilder(TEST_STREAM_1, client).setWriterSchema(schema2).build(); |
| 217 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 218 | + .setLocation("us") |
| 219 | + .setWriterSchema(schema2) |
| 220 | + .build(); |
211 | 221 | StreamWriter sw3 = |
212 | | - StreamWriter.newBuilder(TEST_STREAM_1, client).setWriterSchema(schema3).build(); |
| 222 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 223 | + .setLocation("us") |
| 224 | + .setWriterSchema(schema3) |
| 225 | + .build(); |
213 | 226 | for (long i = 0; i < appendCount; i++) { |
214 | 227 | switch ((int) i % 4) { |
215 | 228 | case 0: |
@@ -305,10 +318,14 @@ public void testAppendInSameStream_switchSchema() throws Exception { |
305 | 318 | public void testAppendButInflightQueueFull() throws Exception { |
306 | 319 | ProtoSchema schema1 = createProtoSchema("foo"); |
307 | 320 | StreamWriter sw1 = |
308 | | - StreamWriter.newBuilder(TEST_STREAM_1, client).setWriterSchema(schema1).build(); |
| 321 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 322 | + .setLocation("us") |
| 323 | + .setWriterSchema(schema1) |
| 324 | + .build(); |
309 | 325 | ConnectionWorker connectionWorker = |
310 | 326 | new ConnectionWorker( |
311 | 327 | TEST_STREAM_1, |
| 328 | + "us", |
312 | 329 | createProtoSchema("foo"), |
313 | 330 | 6, |
314 | 331 | 100000, |
@@ -356,10 +373,14 @@ public void testAppendButInflightQueueFull() throws Exception { |
356 | 373 | public void testThrowExceptionWhileWithinAppendLoop() throws Exception { |
357 | 374 | ProtoSchema schema1 = createProtoSchema("foo"); |
358 | 375 | StreamWriter sw1 = |
359 | | - StreamWriter.newBuilder(TEST_STREAM_1, client).setWriterSchema(schema1).build(); |
| 376 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 377 | + .setLocation("us") |
| 378 | + .setWriterSchema(schema1) |
| 379 | + .build(); |
360 | 380 | ConnectionWorker connectionWorker = |
361 | 381 | new ConnectionWorker( |
362 | 382 | TEST_STREAM_1, |
| 383 | + "us", |
363 | 384 | createProtoSchema("foo"), |
364 | 385 | 100000, |
365 | 386 | 100000, |
@@ -411,6 +432,69 @@ public void testThrowExceptionWhileWithinAppendLoop() throws Exception { |
411 | 432 | assertThat(ex.getCause()).hasMessageThat().contains("Any exception can happen."); |
412 | 433 | } |
413 | 434 |
|
| 435 | + @Test |
| 436 | + public void testLocationMismatch() throws Exception { |
| 437 | + ProtoSchema schema1 = createProtoSchema("foo"); |
| 438 | + StreamWriter sw1 = |
| 439 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 440 | + .setWriterSchema(schema1) |
| 441 | + .setLocation("eu") |
| 442 | + .build(); |
| 443 | + ConnectionWorker connectionWorker = |
| 444 | + new ConnectionWorker( |
| 445 | + TEST_STREAM_1, |
| 446 | + "us", |
| 447 | + createProtoSchema("foo"), |
| 448 | + 100000, |
| 449 | + 100000, |
| 450 | + Duration.ofSeconds(100), |
| 451 | + FlowController.LimitExceededBehavior.Block, |
| 452 | + TEST_TRACE_ID, |
| 453 | + client.getSettings()); |
| 454 | + StatusRuntimeException ex = |
| 455 | + assertThrows( |
| 456 | + StatusRuntimeException.class, |
| 457 | + () -> |
| 458 | + sendTestMessage( |
| 459 | + connectionWorker, |
| 460 | + sw1, |
| 461 | + createFooProtoRows(new String[] {String.valueOf(0)}), |
| 462 | + 0)); |
| 463 | + assertEquals( |
| 464 | + "INVALID_ARGUMENT: StreamWriter with location eu is scheduled to use a connection with location us", |
| 465 | + ex.getMessage()); |
| 466 | + } |
| 467 | + |
| 468 | + @Test |
| 469 | + public void testStreamNameMismatch() throws Exception { |
| 470 | + ProtoSchema schema1 = createProtoSchema("foo"); |
| 471 | + StreamWriter sw1 = |
| 472 | + StreamWriter.newBuilder(TEST_STREAM_1, client).setWriterSchema(schema1).build(); |
| 473 | + ConnectionWorker connectionWorker = |
| 474 | + new ConnectionWorker( |
| 475 | + TEST_STREAM_2, |
| 476 | + null, |
| 477 | + createProtoSchema("foo"), |
| 478 | + 100000, |
| 479 | + 100000, |
| 480 | + Duration.ofSeconds(100), |
| 481 | + FlowController.LimitExceededBehavior.Block, |
| 482 | + TEST_TRACE_ID, |
| 483 | + client.getSettings()); |
| 484 | + StatusRuntimeException ex = |
| 485 | + assertThrows( |
| 486 | + StatusRuntimeException.class, |
| 487 | + () -> |
| 488 | + sendTestMessage( |
| 489 | + connectionWorker, |
| 490 | + sw1, |
| 491 | + createFooProtoRows(new String[] {String.valueOf(0)}), |
| 492 | + 0)); |
| 493 | + assertEquals( |
| 494 | + "INVALID_ARGUMENT: StreamWriter with stream name projects/p1/datasets/d1/tables/t1/streams/s1 is scheduled to use a connection with stream name projects/p2/datasets/d2/tables/t2/streams/s2", |
| 495 | + ex.getMessage()); |
| 496 | + } |
| 497 | + |
414 | 498 | @Test |
415 | 499 | public void testExponentialBackoff() throws Exception { |
416 | 500 | assertThat(ConnectionWorker.calculateSleepTimeMilli(0)).isEqualTo(1); |
@@ -440,6 +524,7 @@ private ConnectionWorker createConnectionWorker( |
440 | 524 | throws IOException { |
441 | 525 | return new ConnectionWorker( |
442 | 526 | streamName, |
| 527 | + "us", |
443 | 528 | createProtoSchema("foo"), |
444 | 529 | maxRequests, |
445 | 530 | maxBytes, |
|
0 commit comments