22using System . Collections . Generic ;
33using System . Linq ;
44using System . Net ;
5- using System . Net . Http ;
5+ using System . Threading ;
66using System . Threading . Tasks ;
77using InfluxDB3 . Client . Config ;
88using InfluxDB3 . Client . Write ;
99using WireMock . Matchers ;
1010using WireMock . RequestBuilders ;
1111using WireMock . ResponseBuilders ;
12+ using WriteOptions = InfluxDB3 . Client . Config . WriteOptions ;
1213
1314namespace InfluxDB3 . Client . Test ;
1415
@@ -493,55 +494,113 @@ public void WriteNoSyncTrueNotSupported()
493494 }
494495
495496 [ Test ]
496- public async Task TestSetHttpClient ( )
497+ public void TimeoutExceededByTimeout ( )
497498 {
498499 MockServer
499500 . Given ( Request . Create ( ) . WithPath ( "/api/v2/write" ) . UsingPost ( ) )
500- . RespondWith ( Response . Create ( ) . WithStatusCode ( HttpStatusCode . OK ) ) ;
501-
502- var httpClient = new HttpClient ( ) ;
503- httpClient . DefaultRequestHeaders . UserAgent . ParseAdd ( "my-user-agent" ) ;
504- httpClient . DefaultRequestHeaders . Add ( "X-Client-ID" , "123" ) ;
501+ . RespondWith ( Response . Create ( ) . WithStatusCode ( 204 ) . WithDelay ( TimeSpan . FromSeconds ( 2 ) ) ) ;
505502
506503 _client = new InfluxDBClient ( new ClientConfig
507504 {
508505 Host = MockServerUrl ,
509506 Token = "my-token" ,
510507 Database = "my-database" ,
511- HttpClient = httpClient
508+ Timeout = TimeSpan . FromTicks ( 1 )
512509 } ) ;
510+ TestWriteRecordAsync ( _client ) ;
511+ TestWriteRecordsAsync ( _client ) ;
512+ TestWritePointAsync ( _client ) ;
513+ TestWritePointsAsync ( _client ) ;
514+ }
513515
514- await _client . WriteRecordAsync ( "mem,tag=a field=1" ) ;
515- var requests = MockServer . LogEntries . ToList ( ) ;
516- using ( Assert . EnterMultipleScope ( ) )
516+ [ Test ]
517+ public void TimeoutExceededByWriteTimeout ( )
518+ {
519+ MockServer
520+ . Given ( Request . Create ( ) . WithPath ( "/api/v2/write" ) . UsingPost ( ) )
521+ . RespondWith ( Response . Create ( ) . WithStatusCode ( 204 ) . WithDelay ( TimeSpan . FromSeconds ( 2 ) ) ) ;
522+
523+ _client = new InfluxDBClient ( new ClientConfig
517524 {
518- Assert . That ( requests [ 0 ] . RequestMessage . Headers ? [ "User-Agent" ] . First ( ) , Is . EqualTo ( "my-user-agent" ) ) ;
519- Assert . That ( requests [ 0 ] . RequestMessage . Headers [ "X-Client-ID" ] . First ( ) , Is . EqualTo ( "123" ) ) ;
520- }
521- Assert . Pass ( ) ;
525+ Host = MockServerUrl ,
526+ Token = "my-token" ,
527+ Database = "my-database" ,
528+ QueryTimeout = TimeSpan . FromSeconds ( 11 ) ,
529+ Timeout = TimeSpan . FromSeconds ( 11 ) ,
530+ WriteTimeout = TimeSpan . FromTicks ( 1 ) // WriteTimeout has a higher priority than Timeout
531+ } ) ;
532+ TestWriteRecordAsync ( _client ) ;
533+ TestWriteRecordsAsync ( _client ) ;
534+ TestWritePointAsync ( _client ) ;
535+ TestWritePointsAsync ( _client ) ;
536+
522537 }
523538
524539 [ Test ]
525- public void TestCheckHttpClientStillOpen ( )
540+ public void TimeoutExceededByToken ( )
526541 {
527542 MockServer
528- . Given ( Request . Create ( ) . WithPath ( "/test" ) . UsingGet ( ) )
529- . RespondWith (
530- Response . Create ( )
531- . WithStatusCode ( HttpStatusCode . OK )
532- . WithBody ( "Still ok" ) ) ;
543+ . Given ( Request . Create ( ) . WithPath ( "/api/v2/write" ) . UsingPost ( ) )
544+ . RespondWith ( Response . Create ( ) . WithStatusCode ( 204 ) . WithDelay ( TimeSpan . FromSeconds ( 2 ) ) ) ;
533545
534- var httpClient = new HttpClient ( new HttpClientHandler ( ) ) ;
535546 _client = new InfluxDBClient ( new ClientConfig
536547 {
537548 Host = MockServerUrl ,
538549 Token = "my-token" ,
539550 Database = "my-database" ,
540- HttpClient = httpClient
551+ QueryTimeout = TimeSpan . FromSeconds ( 11 ) ,
552+ Timeout = TimeSpan . FromSeconds ( 11 ) ,
553+ WriteTimeout = TimeSpan . FromSeconds ( 11 )
541554 } ) ;
542- _client . Dispose ( ) ;
555+ var cancellationToken = new CancellationTokenSource ( TimeSpan . FromTicks ( 1 ) ) . Token ;
556+ TestWriteRecordAsync ( _client , cancellationToken ) ;
557+ TestWriteRecordsAsync ( _client , cancellationToken ) ;
558+ TestWritePointAsync ( _client , cancellationToken ) ;
559+ TestWritePointsAsync ( _client , cancellationToken ) ;
560+ }
543561
544- var httpResponseMessage = httpClient . Send ( new HttpRequestMessage ( HttpMethod . Get , "test" ) ) ;
545- Assert . That ( httpResponseMessage . Content . ReadAsStringAsync ( ) . Result , Is . EqualTo ( "Still ok" ) ) ;
562+ private static void TestWriteRecordAsync ( InfluxDBClient client , CancellationToken ? cancellationToken = null )
563+ {
564+ Assert . ThrowsAsync < TaskCanceledException > ( async ( ) =>
565+ {
566+ await client . WriteRecordAsync ( "mem,tag=a field=1" , cancellationToken : cancellationToken ?? CancellationToken . None ) ;
567+ } ) ;
568+ }
569+
570+ private static void TestWriteRecordsAsync ( InfluxDBClient client , CancellationToken ? cancellationToken = null )
571+ {
572+ Assert . ThrowsAsync < TaskCanceledException > ( async ( ) =>
573+ {
574+ await client . WriteRecordsAsync (
575+ records : new [ ] { "stat,unit=temperature value=24.5" , "stat,unit=temperature value=25.5" } ,
576+ cancellationToken : cancellationToken ?? CancellationToken . None
577+ ) ;
578+ } ) ;
579+ }
580+
581+ private static void TestWritePointAsync ( InfluxDBClient client , CancellationToken ? cancellationToken = null )
582+ {
583+ Assert . ThrowsAsync < TaskCanceledException > ( async ( ) =>
584+ {
585+ await client . WritePointAsync (
586+ PointData . Measurement ( "h2o" ) . SetTag ( "location" , "europe" ) . SetField ( "level" , 2 ) ,
587+ cancellationToken : cancellationToken ?? CancellationToken . None
588+ ) ;
589+ } ) ;
590+ }
591+
592+ private static void TestWritePointsAsync ( InfluxDBClient client , CancellationToken ? cancellationToken = null )
593+ {
594+ Assert . ThrowsAsync < TaskCanceledException > ( async ( ) =>
595+ {
596+ await client . WritePointsAsync (
597+ points : new [ ]
598+ {
599+ PointData . Measurement ( "h2o" ) . SetTag ( "location" , "europe" ) . SetField ( "level" , 2 ) ,
600+ PointData . Measurement ( "h2o" ) . SetTag ( "location" , "us-west" ) . SetField ( "level" , 4 ) ,
601+ } ,
602+ cancellationToken : cancellationToken ?? CancellationToken . None
603+ ) ;
604+ } ) ;
546605 }
547606}
0 commit comments