@@ -38,7 +38,7 @@ impl Write {
3838 }
3939
4040 pub ( crate ) async fn new ( builder : ClientBuilder ) -> BuilderResult < Self > {
41- let inner = Arc :: new ( Transport :: new ( builder. config ) . await ?) ;
41+ let inner = Arc :: new ( Transport :: new ( builder. config , builder . request_options ) . await ?) ;
4242 let pools = Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ;
4343 Ok ( Self {
4444 inner,
@@ -171,9 +171,81 @@ mod tests {
171171 use super :: super :: error:: AppendError ;
172172 use super :: * ;
173173 use crate :: model:: { ArrowRecordBatch , ArrowSchema , ProtoRows , ProtoSchema } ;
174+ use crate :: write:: retry_policy:: RetryableErrors ;
175+ use crate :: write:: test:: {
176+ NoBackoff , USER_AGENT , check_user_agent, convert, rows, schema, test_response,
177+ } ;
178+ use bigquery_grpc_mock:: google:: cloud:: bigquery:: storage:: v1;
174179 use bigquery_grpc_mock:: { MockBigQueryWrite , start} ;
175- use gaxi:: grpc:: tonic:: Status as TonicStatus ;
180+ use gaxi:: grpc:: tonic:: { Response as TonicResponse , Status as TonicStatus } ;
176181 use google_cloud_auth:: credentials:: anonymous:: Builder as Anonymous ;
182+ use google_cloud_gax:: retry_policy:: RetryPolicyExt ;
183+ use test_case:: test_case;
184+ use tokio:: sync:: mpsc;
185+
186+ #[ test_case( false ; "standalone" ) ]
187+ #[ test_case( true ; "multiplexed" ) ]
188+ #[ tokio:: test]
189+ async fn default_stream_user_agent ( multiplexing : bool ) -> anyhow:: Result < ( ) > {
190+ let ( response_tx, response_rx) = mpsc:: channel ( 1 ) ;
191+ let mut mock = MockBigQueryWrite :: new ( ) ;
192+ if multiplexing {
193+ mock. expect_get_write_stream ( )
194+ . times ( 2 )
195+ . returning ( move |req| {
196+ check_user_agent ( req. metadata ( ) ) ;
197+ Ok ( TonicResponse :: new ( v1:: WriteStream {
198+ name : req. into_inner ( ) . name ,
199+ location : "us" . to_string ( ) ,
200+ ..Default :: default ( )
201+ } ) )
202+ } ) ;
203+ }
204+ let mut sequence = mockall:: Sequence :: new ( ) ;
205+ mock. expect_append_rows ( )
206+ . times ( 1 )
207+ . in_sequence ( & mut sequence)
208+ . return_once ( move |req| {
209+ check_user_agent ( req. metadata ( ) ) ;
210+ Err ( TonicStatus :: unavailable ( "retry opening the stream" ) )
211+ } ) ;
212+ mock. expect_append_rows ( )
213+ . times ( 1 )
214+ . in_sequence ( & mut sequence)
215+ . return_once ( move |req| {
216+ check_user_agent ( req. metadata ( ) ) ;
217+ Ok ( TonicResponse :: from ( response_rx) )
218+ } ) ;
219+ let ( endpoint, _server) = start ( "0.0.0.0:0" , mock) . await ?;
220+ let client = Write :: builder ( )
221+ . with_endpoint ( endpoint)
222+ . with_credentials ( Anonymous :: new ( ) . build ( ) )
223+ . with_user_agent ( USER_AGENT )
224+ . build ( )
225+ . await ?;
226+ let writer = client
227+ . open_default_stream ( "projects/p/datasets/d/tables/t" )
228+ . with_multiplexing ( multiplexing)
229+ . with_retry_policy ( RetryableErrors . with_attempt_limit ( 2 ) )
230+ . with_backoff_policy ( NoBackoff )
231+ . with_attempt_timeout ( std:: time:: Duration :: from_secs ( 5 ) )
232+ . build_arrow ( schema ( ) )
233+ . await ?;
234+ response_tx. send ( Ok ( convert ( & test_response ( 1 ) ) ) ) . await ?;
235+ assert_eq ! ( writer. append( rows( 1 ) ) . send( ) . await ?. offset, Some ( 1 ) ) ;
236+
237+ if multiplexing {
238+ let other = client
239+ . open_default_stream ( "projects/p/datasets/d/tables/other" )
240+ . with_multiplexing ( true )
241+ . build_arrow ( schema ( ) )
242+ . await ?;
243+ assert ! ( Arc :: ptr_eq( & writer. inner. pool, & other. inner. pool) ) ;
244+ response_tx. send ( Ok ( convert ( & test_response ( 2 ) ) ) ) . await ?;
245+ assert_eq ! ( other. append( rows( 2 ) ) . send( ) . await ?. offset, Some ( 2 ) ) ;
246+ }
247+ Ok ( ( ) )
248+ }
177249
178250 #[ tokio:: test]
179251 async fn arrow ( ) -> anyhow:: Result < ( ) > {
0 commit comments