1616//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
1717
1818using System . Security . Cryptography ;
19+ using System . Threading . Tasks ;
1920using FirebirdSql . Data . TestsBase ;
2021using NUnit . Framework ;
2122
@@ -25,104 +26,78 @@ namespace FirebirdSql.Data.FirebirdClient.Tests
2526 [ TestFixtureSource ( typeof ( FbServerTypeTestFixtureSource ) , nameof ( FbServerTypeTestFixtureSource . Embedded ) ) ]
2627 public class FbBlobTests : FbTestsBase
2728 {
28- #region Constructors
29-
3029 public FbBlobTests ( FbServerType serverType , bool compression , FbWireCrypt wireCrypt )
3130 : base ( serverType , compression , wireCrypt )
3231 { }
3332
34- #endregion
35-
36- #region Unit Tests
37-
3833 [ Test ]
39- public void BinaryBlobTest ( )
34+ public async Task BinaryBlobTest ( )
4035 {
4136 var id_value = GetId ( ) ;
42-
43- var selectText = "SELECT blob_field FROM TEST WHERE int_field = " + id_value . ToString ( ) ;
44- var insertText = "INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)" ;
45-
46- // Generate an array of temp data
4737 var insert_values = new byte [ 100000 * 4 ] ;
48- var rng = new RNGCryptoServiceProvider ( ) ;
49- rng . GetBytes ( insert_values ) ;
50-
51- // Execute insert command
52- var transaction = Connection . BeginTransaction ( ) ;
53-
54- var insert = new FbCommand ( insertText , Connection , transaction ) ;
55- insert . Parameters . Add ( "@int_field" , FbDbType . Integer ) . Value = id_value ;
56- insert . Parameters . Add ( "@blob_field" , FbDbType . Binary ) . Value = insert_values ;
57- insert . ExecuteNonQuery ( ) ;
58-
59- transaction . Commit ( ) ;
60-
61- // Check that inserted values are correct
62- var select = new FbCommand ( selectText , Connection ) ;
63- var select_values = ( byte [ ] ) select . ExecuteScalar ( ) ;
38+ using ( var rng = new RNGCryptoServiceProvider ( ) )
39+ {
40+ rng . GetBytes ( insert_values ) ;
41+ }
6442
65- for ( var i = 0 ; i < insert_values . Length ; i ++ )
43+ await using ( var transaction = await Connection . BeginTransactionAsync ( ) )
6644 {
67- if ( insert_values [ i ] != select_values [ i ] )
45+ await using ( var insert = new FbCommand ( "INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)" , Connection , transaction ) )
6846 {
69- Assert . Fail ( "differences at index " + i . ToString ( ) ) ;
47+ insert . Parameters . Add ( "@int_field" , FbDbType . Integer ) . Value = id_value ;
48+ insert . Parameters . Add ( "@blob_field" , FbDbType . Binary ) . Value = insert_values ;
49+ await insert . ExecuteNonQueryAsync ( ) ;
7050 }
51+ await transaction . CommitAsync ( ) ;
52+ }
53+
54+ await using ( var select = new FbCommand ( $ "SELECT blob_field FROM TEST WHERE int_field = { id_value } ", Connection ) )
55+ {
56+ var select_values = ( byte [ ] ) await select . ExecuteScalarAsync ( ) ;
57+ CollectionAssert . AreEqual ( insert_values , select_values ) ;
7158 }
7259 }
7360
7461 [ Test ]
75- public void ReaderGetBytes ( )
62+ public async Task ReaderGetBytes ( )
7663 {
7764 var id_value = GetId ( ) ;
78-
79- var selectText = "SELECT blob_field FROM TEST WHERE int_field = " + id_value . ToString ( ) ;
80- var insertText = "INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)" ;
81-
82- // Generate an array of temp data
8365 var insert_values = new byte [ 100000 * 4 ] ;
84- var rng = new RNGCryptoServiceProvider ( ) ;
85- rng . GetBytes ( insert_values ) ;
86-
87- // Execute insert command
88- var transaction = Connection . BeginTransaction ( ) ;
89-
90- var insert = new FbCommand ( insertText , Connection , transaction ) ;
91- insert . Parameters . Add ( "@int_field" , FbDbType . Integer ) . Value = id_value ;
92- insert . Parameters . Add ( "@blob_field" , FbDbType . Binary ) . Value = insert_values ;
93- insert . ExecuteNonQuery ( ) ;
94-
95- transaction . Commit ( ) ;
96-
97- // Check that inserted values are correct
98- var select = new FbCommand ( selectText , Connection ) ;
99-
100- var select_values = new byte [ 100000 * 4 ] ;
66+ using ( var rng = new RNGCryptoServiceProvider ( ) )
67+ {
68+ rng . GetBytes ( insert_values ) ;
69+ }
10170
102- using ( var reader = select . ExecuteReader ( ) )
71+ await using ( var transaction = await Connection . BeginTransactionAsync ( ) )
10372 {
104- var index = 0 ;
105- var segmentSize = 1000 ;
106- while ( reader . Read ( ) )
73+ await using ( var insert = new FbCommand ( "INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)" , Connection , transaction ) )
10774 {
108- while ( index < 400000 )
109- {
110- reader . GetBytes ( 0 , index , select_values , index , segmentSize ) ;
111-
112- index += segmentSize ;
113- }
75+ insert . Parameters . Add ( "@int_field" , FbDbType . Integer ) . Value = id_value ;
76+ insert . Parameters . Add ( "@blob_field" , FbDbType . Binary ) . Value = insert_values ;
77+ await insert . ExecuteNonQueryAsync ( ) ;
11478 }
79+ await transaction . CommitAsync ( ) ;
11580 }
11681
117- for ( var i = 0 ; i < insert_values . Length ; i ++ )
82+ await using ( var select = new FbCommand ( $ "SELECT blob_field FROM TEST WHERE int_field = { id_value } " , Connection ) )
11883 {
119- if ( insert_values [ i ] != select_values [ i ] )
84+ var select_values = new byte [ 100000 * 4 ] ;
85+ using ( var reader = await select . ExecuteReaderAsync ( ) )
12086 {
121- Assert . Fail ( "differences at index " + i . ToString ( ) ) ;
87+ var index = 0 ;
88+ var segmentSize = 1000 ;
89+ while ( await reader . ReadAsync ( ) )
90+ {
91+ while ( index < 400000 )
92+ {
93+ reader . GetBytes ( 0 , index , select_values , index , segmentSize ) ;
94+
95+ index += segmentSize ;
96+ }
97+ }
12298 }
99+ CollectionAssert . AreEqual ( insert_values , select_values ) ;
123100 }
124101 }
125-
126- #endregion
127102 }
128103}
0 commit comments