|
56 | 56 | import com.google.protobuf.Descriptors; |
57 | 57 | import com.google.protobuf.Descriptors.DescriptorValidationException; |
58 | 58 | import com.google.protobuf.Int64Value; |
| 59 | +import com.google.protobuf.Timestamp; |
59 | 60 | import io.grpc.Status; |
60 | 61 | import io.grpc.StatusRuntimeException; |
61 | 62 | import io.opentelemetry.api.common.Attributes; |
@@ -2551,4 +2552,66 @@ public void testGetDefaultStreamName() { |
2551 | 2552 | assertEquals( |
2552 | 2553 | "projects/projectId/datasets/datasetId/tables/tableId/_default", actualDefaultName); |
2553 | 2554 | } |
| 2555 | + |
| 2556 | + @Test |
| 2557 | + public void testLocationCacheIsHit() throws Exception { |
| 2558 | + WriteStream expectedResponse = |
| 2559 | + WriteStream.newBuilder() |
| 2560 | + .setName(WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString()) |
| 2561 | + .setCreateTime(Timestamp.newBuilder().build()) |
| 2562 | + .setCommitTime(Timestamp.newBuilder().build()) |
| 2563 | + .setTableSchema(TableSchema.newBuilder().build()) |
| 2564 | + .setLocation("oklahoma") |
| 2565 | + .build(); |
| 2566 | + testBigQueryWrite.addResponse(expectedResponse); |
| 2567 | + |
| 2568 | + // first stream will result in call to getWriteStream for location lookup |
| 2569 | + StreamWriter writer1 = |
| 2570 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 2571 | + .setWriterSchema(createProtoSchema()) |
| 2572 | + .setEnableConnectionPool(true) |
| 2573 | + .build(); |
| 2574 | + |
| 2575 | + // second stream will hit the location cache |
| 2576 | + StreamWriter writer2 = |
| 2577 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 2578 | + .setWriterSchema(createProtoSchema()) |
| 2579 | + .setEnableConnectionPool(true) |
| 2580 | + .build(); |
| 2581 | + assertEquals(1, testBigQueryWrite.getWriteStreamRequests().size()); |
| 2582 | + } |
| 2583 | + |
| 2584 | + @Test |
| 2585 | + public void testLocationCacheExpires() throws Exception { |
| 2586 | + // force cache to expire in 1000 millis |
| 2587 | + StreamWriter.recreateProjectLocationCache(1000); |
| 2588 | + WriteStream expectedResponse = |
| 2589 | + WriteStream.newBuilder() |
| 2590 | + .setName(WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString()) |
| 2591 | + .setCreateTime(Timestamp.newBuilder().build()) |
| 2592 | + .setCommitTime(Timestamp.newBuilder().build()) |
| 2593 | + .setTableSchema(TableSchema.newBuilder().build()) |
| 2594 | + .setLocation("oklahoma") |
| 2595 | + .build(); |
| 2596 | + testBigQueryWrite.addResponse(expectedResponse); |
| 2597 | + testBigQueryWrite.addResponse(expectedResponse); |
| 2598 | + |
| 2599 | + // first stream will result in call to getWriteStream for location lookup |
| 2600 | + StreamWriter writer1 = |
| 2601 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 2602 | + .setWriterSchema(createProtoSchema()) |
| 2603 | + .setEnableConnectionPool(true) |
| 2604 | + .build(); |
| 2605 | + |
| 2606 | + // force cache to expire |
| 2607 | + TimeUnit.SECONDS.sleep(2); |
| 2608 | + |
| 2609 | + // second stream will result in call to getWriteStream for location lookup |
| 2610 | + StreamWriter writer2 = |
| 2611 | + StreamWriter.newBuilder(TEST_STREAM_1, client) |
| 2612 | + .setWriterSchema(createProtoSchema()) |
| 2613 | + .setEnableConnectionPool(true) |
| 2614 | + .build(); |
| 2615 | + assertEquals(2, testBigQueryWrite.getWriteStreamRequests().size()); |
| 2616 | + } |
2554 | 2617 | } |
0 commit comments