1- using System ;
2- using System . Collections . Generic ;
1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using System . Runtime . CompilerServices ;
34using System . Threading ;
45using System . Threading . Tasks ;
56using Npgsql ;
67using NUnit . Framework ;
8+ using PostgreSQLCopyHelper . Test . Extensions ;
79
810namespace PostgreSQLCopyHelper . Test . Issues
911{
@@ -26,40 +28,64 @@ protected override void OnSetupInTransaction()
2628 }
2729
2830 [ Test ]
29- public async Task Test_CanceledBulkInsertThrowsWhenCanceled ( )
31+ public Task Test_CanceledBulkInsertThrowsWhenCanceledBeforeStarting ( )
3032 {
3133 subject = new PostgreSQLCopyHelper < User > ( "sample" , "TestUsers" )
3234 . MapInteger ( "Id" , x => x . Id )
3335 . MapText ( "Name" , x => x . Name ) ;
3436
35- var cancellationTokenSource = new CancellationTokenSource ( ) ;
36-
37- // Try to work with the Bulk Inserter:
38- try
39- {
40- cancellationTokenSource . CancelAfter ( 15 ) ;
41- await subject . SaveAllAsync ( connection , FetchUserData ( ) , cancellationTokenSource . Token ) ;
42- Assert . Fail ( "Should Never Reach Here!" ) ;
43- }
44- catch ( TaskCanceledException )
37+ using ( var cts = new CancellationTokenSource ( 1 ) )
4538 {
39+ Assert . ThrowsAsync < TaskCanceledException > ( async ( ) =>
40+ await subject . SaveAllAsync ( connection , FetchUserData ( 5 ) , cts . Token ) , $ "Should Throw Exception of Type { nameof ( TaskCanceledException ) } !") ;
4641 }
47- catch ( Exception )
42+
43+ return Task . CompletedTask ;
44+ }
45+
46+ [ Test ]
47+ public Task Test_CanceledBulkInsertThrowsWhenCanceled ( )
48+ {
49+ subject = new PostgreSQLCopyHelper < User > ( "sample" , "TestUsers" )
50+ . MapInteger ( "Id" , x => x . Id )
51+ . MapText ( "Name" , x => x . Name ) ;
52+
53+ using ( var cts = new CancellationTokenSource ( 15 ) )
4854 {
49- Assert . Fail ( "Should Throw Exception of Type TaskCanceledException!" ) ;
55+ Assert . ThrowsAsync < TaskCanceledException > ( async ( ) =>
56+ await subject . SaveAllAsync ( connection , FetchUserData ( 10 ) , cts . Token ) , $ "Should Throw Exception of Type { nameof ( TaskCanceledException ) } !") ;
5057 }
51- finally
58+
59+ return Task . CompletedTask ;
60+ }
61+
62+ [ Test ]
63+ public async Task Test_CanceledBulkInsertDoesNotThrowWhenCancelledAfterCompletion ( )
64+ {
65+ subject = new PostgreSQLCopyHelper < User > ( "sample" , "TestUsers" )
66+ . MapInteger ( "Id" , x => x . Id )
67+ . MapText ( "Name" , x => x . Name ) ;
68+
69+ using ( var cts = new CancellationTokenSource ( 50 ) )
5270 {
53- cancellationTokenSource . Dispose ( ) ;
71+ var recordsSaved = await subject . SaveAllAsync ( connection , FetchUserData ( 10 ) , cts . Token ) ;
72+ var result = connection . GetAll ( "sample" , "\" TestUsers\" " )
73+ . Cast < User > ( )
74+ . OrderBy ( x => x . Id )
75+ . ToList ( ) ;
76+
77+ Assert . AreEqual ( 2 , recordsSaved ) ;
78+ Assert . AreEqual ( 1 , result . First ( ) . Id ) ;
79+ Assert . AreEqual ( 2 , result . Last ( ) . Id ) ;
5480 }
5581 }
5682
57- private static async IAsyncEnumerable < User > FetchUserData ( )
83+ private static async IAsyncEnumerable < User > FetchUserData ( int delayMillis , [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
5884 {
5985 for ( var i = 1 ; i <= 2 ; i ++ )
6086 {
6187 // Simulate waiting for data to come through.
62- await Task . Delay ( 10 ) ;
88+ await Task . Delay ( delayMillis , cancellationToken ) ;
6389 yield return new User
6490 {
6591 Id = i ,
0 commit comments