@@ -114,14 +114,62 @@ public async Task CanCancelStatement()
114114 }
115115
116116 [ Fact ]
117- public async Task CanCancelStreamFromStatement ( )
117+ public async Task CanCancelStreamAndDisposeStatement ( )
118118 {
119119 foreach ( BigQueryTestEnvironment environment in _environments )
120120 {
121- AdbcConnection adbcConnection = GetAdbcConnection ( environment . Name ) ;
121+ using AdbcConnection adbcConnection = GetAdbcConnection ( environment . Name ) ;
122122
123123 AdbcStatement statement = adbcConnection . CreateStatement ( ) ;
124124
125+ // Execute the query/cancel multiple times to validate consistent behavior
126+ const int iterations = 3 ;
127+ QueryResult [ ] results = new QueryResult [ iterations ] ;
128+ for ( int i = 0 ; i < iterations ; i ++ )
129+ {
130+ _outputHelper ? . WriteLine ( $ "Iteration { i + 1 } of { iterations } ") ;
131+ // Generate unique column names so query will not be served from cache
132+ string columnName1 = Guid . NewGuid ( ) . ToString ( "N" ) ;
133+ string columnName2 = Guid . NewGuid ( ) . ToString ( "N" ) ;
134+ statement . SqlQuery = $ "SELECT `{ columnName2 } ` AS `{ columnName1 } ` FROM UNNEST(GENERATE_ARRAY(1, 100)) AS `{ columnName2 } `";
135+ _outputHelper ? . WriteLine ( $ "Query: { statement . SqlQuery } ") ;
136+
137+ // Expect this to take about 10 seconds without cancellation
138+ results [ i ] = statement . ExecuteQuery ( ) ;
139+ }
140+ statement . Cancel ( ) ;
141+ statement . Dispose ( ) ;
142+ for ( int index = 0 ; index < iterations ; index ++ )
143+ {
144+ try
145+ {
146+ QueryResult queryResult = results [ index ] ;
147+ using IArrowArrayStream ? stream = queryResult . Stream ;
148+ Assert . NotNull ( stream ) ;
149+ RecordBatch batch = await stream . ReadNextRecordBatchAsync ( ) ;
150+
151+ Assert . Fail ( "Expecting OperationCanceledException to be thrown." ) ;
152+ }
153+ catch ( Exception ex ) when ( BigQueryUtils . ContainsException ( ex , out OperationCanceledException ? _ ) )
154+ {
155+ _outputHelper ? . WriteLine ( $ "Received expected OperationCanceledException: { ex . Message } ") ;
156+ }
157+ catch ( Exception ex ) when ( ex is not FailException )
158+ {
159+ Assert . Fail ( $ "Expecting OperationCanceledException to be thrown. Instead, received { ex . GetType ( ) . Name } : { ex . Message } ") ;
160+ }
161+ }
162+ }
163+ }
164+
165+ [ Fact ]
166+ public async Task CanCancelStreamFromStatement ( )
167+ {
168+ foreach ( BigQueryTestEnvironment environment in _environments )
169+ {
170+ using AdbcConnection adbcConnection = GetAdbcConnection ( environment . Name ) ;
171+ using AdbcStatement statement = adbcConnection . CreateStatement ( ) ;
172+
125173 // Execute the query/cancel multiple times to validate consistent behavior
126174 const int iterations = 3 ;
127175 QueryResult [ ] results = new QueryResult [ iterations ] ;
@@ -143,7 +191,7 @@ public async Task CanCancelStreamFromStatement()
143191 try
144192 {
145193 QueryResult queryResult = results [ index ] ;
146- IArrowArrayStream ? stream = queryResult . Stream ;
194+ using IArrowArrayStream ? stream = queryResult . Stream ;
147195 Assert . NotNull ( stream ) ;
148196 RecordBatch batch = await stream . ReadNextRecordBatchAsync ( ) ;
149197
0 commit comments