@@ -87,31 +87,39 @@ private StreamWriterV2 getTestStreamWriterV2() throws IOException {
8787 return StreamWriterV2 .newBuilder (TEST_STREAM , client ).build ();
8888 }
8989
90- private AppendRowsRequest createAppendRequest (String [] messages , long offset ) {
91- AppendRowsRequest .Builder requestBuilder = AppendRowsRequest .newBuilder ();
92- AppendRowsRequest .ProtoData .Builder dataBuilder = AppendRowsRequest .ProtoData .newBuilder ();
93- dataBuilder .setWriterSchema (
94- ProtoSchema .newBuilder ()
95- .setProtoDescriptor (
96- DescriptorProtos .DescriptorProto .newBuilder ()
97- .setName ("Message" )
98- .addField (
99- DescriptorProtos .FieldDescriptorProto .newBuilder ()
100- .setName ("foo" )
101- .setType (DescriptorProtos .FieldDescriptorProto .Type .TYPE_STRING )
102- .setNumber (1 )
103- .build ())
104- .build ()));
105- ProtoRows .Builder rows = ProtoRows .newBuilder ();
90+ private ProtoSchema createProtoSchema () {
91+ return ProtoSchema .newBuilder ()
92+ .setProtoDescriptor (
93+ DescriptorProtos .DescriptorProto .newBuilder ()
94+ .setName ("Message" )
95+ .addField (
96+ DescriptorProtos .FieldDescriptorProto .newBuilder ()
97+ .setName ("foo" )
98+ .setType (DescriptorProtos .FieldDescriptorProto .Type .TYPE_STRING )
99+ .setNumber (1 )
100+ .build ())
101+ .build ())
102+ .build ();
103+ }
104+
105+ private ProtoRows createProtoRows (String [] messages ) {
106+ ProtoRows .Builder rowsBuilder = ProtoRows .newBuilder ();
106107 for (String message : messages ) {
107108 FooType foo = FooType .newBuilder ().setFoo (message ).build ();
108- rows .addSerializedRows (foo .toByteString ());
109+ rowsBuilder .addSerializedRows (foo .toByteString ());
109110 }
111+ return rowsBuilder .build ();
112+ }
113+
114+ private AppendRowsRequest createAppendRequest (String [] messages , long offset ) {
115+ AppendRowsRequest .Builder requestBuilder = AppendRowsRequest .newBuilder ();
116+ AppendRowsRequest .ProtoData .Builder dataBuilder = AppendRowsRequest .ProtoData .newBuilder ();
117+ dataBuilder .setWriterSchema (createProtoSchema ());
110118 if (offset > 0 ) {
111119 requestBuilder .setOffset (Int64Value .of (offset ));
112120 }
113121 return requestBuilder
114- .setProtoRows (dataBuilder .setRows (rows . build ( )).build ())
122+ .setProtoRows (dataBuilder .setRows (createProtoRows ( messages )).build ())
115123 .setWriteStream (TEST_STREAM )
116124 .build ();
117125 }
@@ -166,6 +174,24 @@ public void run() {
166174 appendThread .interrupt ();
167175 }
168176
177+ private void verifyAppendRequests (long appendCount ) {
178+ assertEquals (appendCount , testBigQueryWrite .getAppendRequests ().size ());
179+ for (int i = 0 ; i < appendCount ; i ++) {
180+ AppendRowsRequest serverRequest = testBigQueryWrite .getAppendRequests ().get (i );
181+ assertTrue (serverRequest .getProtoRows ().getRows ().getSerializedRowsCount () > 0 );
182+ assertEquals (i , serverRequest .getOffset ().getValue ());
183+ if (i == 0 ) {
184+ // First request received by server should have schema and stream name.
185+ assertTrue (serverRequest .getProtoRows ().hasWriterSchema ());
186+ assertEquals (serverRequest .getWriteStream (), TEST_STREAM );
187+ } else {
188+ // Following request should not have schema and stream name.
189+ assertFalse (serverRequest .getProtoRows ().hasWriterSchema ());
190+ assertEquals (serverRequest .getWriteStream (), "" );
191+ }
192+ }
193+ }
194+
169195 @ Test
170196 public void testBuildBigQueryWriteClientInWriter () throws Exception {
171197 StreamWriterV2 writer =
@@ -181,40 +207,68 @@ public void testBuildBigQueryWriteClientInWriter() throws Exception {
181207 }
182208
183209 @ Test
184- public void testAppendSuccess () throws Exception {
185- StreamWriterV2 writer = getTestStreamWriterV2 ();
210+ public void testAppendWithRowsSuccess () throws Exception {
211+ StreamWriterV2 writer =
212+ StreamWriterV2 .newBuilder (TEST_STREAM , client ).setWriterSchema (createProtoSchema ()).build ();
186213
187- long appendCount = 1000 ;
214+ long appendCount = 100 ;
188215 for (int i = 0 ; i < appendCount ; i ++) {
189216 testBigQueryWrite .addResponse (createAppendResponse (i ));
190217 }
191218
192219 List <ApiFuture <AppendRowsResponse >> futures = new ArrayList <>();
193220 for (int i = 0 ; i < appendCount ; i ++) {
194- futures .add (sendTestMessage ( writer , new String [] {String .valueOf (i )}));
221+ futures .add (writer . append ( createProtoRows ( new String [] {String .valueOf (i )}), i ));
195222 }
196223
197224 for (int i = 0 ; i < appendCount ; i ++) {
198225 assertEquals (i , futures .get (i ).get ().getAppendResult ().getOffset ().getValue ());
199226 }
200- assertEquals (appendCount , testBigQueryWrite .getAppendRequests ().size ());
201227
228+ verifyAppendRequests (appendCount );
229+
230+ writer .close ();
231+ }
232+
233+ @ Test
234+ public void testAppendWithMessageSuccess () throws Exception {
235+ StreamWriterV2 writer = getTestStreamWriterV2 ();
236+
237+ long appendCount = 1000 ;
202238 for (int i = 0 ; i < appendCount ; i ++) {
203- AppendRowsRequest serverRequest = testBigQueryWrite .getAppendRequests ().get (i );
204- if (i == 0 ) {
205- // First request received by server should have schema and stream name.
206- assertTrue (serverRequest .getProtoRows ().hasWriterSchema ());
207- assertEquals (serverRequest .getWriteStream (), TEST_STREAM );
208- } else {
209- // Following request should not have schema and stream name.
210- assertFalse (serverRequest .getProtoRows ().hasWriterSchema ());
211- assertEquals (serverRequest .getWriteStream (), "" );
212- }
239+ testBigQueryWrite .addResponse (createAppendResponse (i ));
213240 }
214241
242+ List <ApiFuture <AppendRowsResponse >> futures = new ArrayList <>();
243+ for (int i = 0 ; i < appendCount ; i ++) {
244+ futures .add (writer .append (createAppendRequest (new String [] {String .valueOf (i )}, i )));
245+ }
246+
247+ for (int i = 0 ; i < appendCount ; i ++) {
248+ assertEquals (i , futures .get (i ).get ().getAppendResult ().getOffset ().getValue ());
249+ }
250+
251+ verifyAppendRequests (appendCount );
252+
215253 writer .close ();
216254 }
217255
256+ @ Test
257+ public void testAppendWithRowsNoSchema () throws Exception {
258+ final StreamWriterV2 writer = getTestStreamWriterV2 ();
259+ StatusRuntimeException ex =
260+ assertThrows (
261+ StatusRuntimeException .class ,
262+ new ThrowingRunnable () {
263+ @ Override
264+ public void run () throws Throwable {
265+ writer .append (createProtoRows (new String [] {"A" }), -1 );
266+ }
267+ });
268+ assertEquals (ex .getStatus ().getCode (), Status .INVALID_ARGUMENT .getCode ());
269+ assertTrue (ex .getStatus ().getDescription ().contains ("Writer schema must be provided" ));
270+ }
271+
218272 @ Test
219273 public void testAppendSuccessAndConnectionError () throws Exception {
220274 StreamWriterV2 writer = getTestStreamWriterV2 ();
0 commit comments