2020import static com .google .cloud .bigtable .data .v2 .stub .sql .SqlProtoFactory .stringType ;
2121import static com .google .cloud .bigtable .data .v2 .stub .sql .SqlProtoFactory .stringValue ;
2222import static com .google .common .truth .Truth .assertThat ;
23+ import static org .junit .Assert .assertThrows ;
2324
25+ import com .google .api .gax .rpc .UnavailableException ;
26+ import com .google .bigtable .v2 .BigtableGrpc ;
27+ import com .google .bigtable .v2 .ExecuteQueryRequest ;
28+ import com .google .bigtable .v2 .ExecuteQueryResponse ;
29+ import com .google .cloud .bigtable .data .v2 .BigtableDataSettings ;
30+ import com .google .cloud .bigtable .data .v2 .FakeServiceBuilder ;
2431import com .google .cloud .bigtable .data .v2 .internal .ProtoResultSetMetadata ;
2532import com .google .cloud .bigtable .data .v2 .internal .ProtoSqlRow ;
2633import com .google .cloud .bigtable .data .v2 .internal .RequestContext ;
2734import com .google .cloud .bigtable .data .v2 .internal .SqlRow ;
2835import com .google .cloud .bigtable .data .v2 .models .sql .Statement ;
36+ import com .google .cloud .bigtable .data .v2 .stub .EnhancedBigtableStub ;
2937import com .google .cloud .bigtable .gaxx .testing .FakeStreamingApi .ServerStreamingStashCallable ;
38+ import io .grpc .Server ;
39+ import io .grpc .Status ;
40+ import io .grpc .StatusRuntimeException ;
41+ import io .grpc .stub .StreamObserver ;
42+ import java .io .IOException ;
3043import java .util .Collections ;
3144import java .util .Iterator ;
45+ import org .junit .After ;
46+ import org .junit .Before ;
3247import org .junit .Test ;
3348import org .junit .runner .RunWith ;
3449import org .junit .runners .JUnit4 ;
@@ -39,6 +54,29 @@ public class ExecuteQueryCallableTest {
3954 private static final RequestContext REQUEST_CONTEXT =
4055 RequestContext .create ("fake-project" , "fake-instance" , "fake-profile" );
4156
57+ private Server server ;
58+ private FakeService fakeService = new FakeService ();
59+ private EnhancedBigtableStub stub ;
60+
61+ @ Before
62+ public void setup () throws IOException {
63+ server = FakeServiceBuilder .create (fakeService ).start ();
64+
65+ BigtableDataSettings settings =
66+ BigtableDataSettings .newBuilderForEmulator (server .getPort ())
67+ .setProjectId ("fake-project" )
68+ .setInstanceId ("fake-instance" )
69+ .build ();
70+
71+ stub = EnhancedBigtableStub .create (settings .getStubSettings ());
72+ }
73+
74+ @ After
75+ public void tearDown () {
76+ stub .close ();
77+ server .shutdown ();
78+ }
79+
4280 @ Test
4381 public void testCallContextAndServerStreamSetup () {
4482 SqlRow row =
@@ -57,4 +95,29 @@ public void testCallContextAndServerStreamSetup() {
5795 assertThat (responseIterator .next ()).isEqualTo (row );
5896 assertThat (responseIterator .hasNext ()).isFalse ();
5997 }
98+
99+ @ Test
100+ public void testExecuteQueryRequestsAreNotRetried () {
101+ // TODO: retries for execute query is currently disabled. This test should be
102+ // updated once resumption token is in place.
103+ SqlServerStream stream = stub .executeQueryCallable ().call (Statement .of ("SELECT * FROM table" ));
104+
105+ Iterator <SqlRow > iterator = stream .rows ().iterator ();
106+
107+ assertThrows (UnavailableException .class , iterator ::next ).getCause ();
108+ assertThat (fakeService .attempts ).isEqualTo (1 );
109+ }
110+
111+ private static class FakeService extends BigtableGrpc .BigtableImplBase {
112+
113+ private int attempts = 0 ;
114+
115+ @ Override
116+ public void executeQuery (
117+ ExecuteQueryRequest request , StreamObserver <ExecuteQueryResponse > responseObserver ) {
118+ attempts ++;
119+ responseObserver .onNext (metadata (columnMetadata ("test" , stringType ())));
120+ responseObserver .onError (new StatusRuntimeException (Status .UNAVAILABLE ));
121+ }
122+ }
60123}
0 commit comments