@@ -204,6 +204,122 @@ public async Task ExecuteScalar_WhenSelectNoRows_ReturnNull()
204204 . ExecuteScalarAsync ( ) ) ;
205205 }
206206
207+ [ Fact ]
208+ public async Task ImplicitSession_SimpleScalar_Works ( )
209+ {
210+ await using var connection = CreateConnection ( ) ;
211+ connection . ConnectionString += ";EnableImplicitSession=true" ;
212+ await connection . OpenAsync ( ) ;
213+
214+ var cmd = connection . CreateCommand ( ) ;
215+ cmd . CommandText = "SELECT 40 + 2;" ;
216+ var scalar = await cmd . ExecuteScalarAsync ( ) ;
217+ Assert . Equal ( 42 , Convert . ToInt32 ( scalar ) ) ;
218+ }
219+
220+ [ Fact ]
221+ public async Task ImplicitSession_RepeatedScalars_WorksManyTimes ( )
222+ {
223+ await using var connection = CreateConnection ( ) ;
224+ connection . ConnectionString += ";EnableImplicitSession=true" ;
225+ await connection . OpenAsync ( ) ;
226+
227+ for ( var i = 0 ; i < 30 ; i ++ )
228+ {
229+ var cmd = connection . CreateCommand ( ) ;
230+ cmd . CommandText = $ "SELECT { i } ;";
231+ var scalar = await cmd . ExecuteScalarAsync ( ) ;
232+ Assert . Equal ( i , Convert . ToInt32 ( scalar ) ) ;
233+ }
234+ }
235+
236+ [ Fact ]
237+ public void ImplicitSession_ConcurrentCommand_IsStillBlockedByBusyCheck ( )
238+ {
239+ using var connection = CreateConnection ( ) ;
240+ connection . ConnectionString += ";EnableImplicitSession=true" ;
241+ connection . Open ( ) ;
242+
243+ var cmd = connection . CreateCommand ( ) ;
244+ cmd . CommandText = "SELECT 1; SELECT 1;" ;
245+ using var reader = cmd . ExecuteReader ( ) ;
246+
247+ var ex = Assert . Throws < YdbOperationInProgressException > ( ( ) => cmd . ExecuteReader ( ) ) ;
248+ Assert . Equal ( "A command is already in progress: SELECT 1; SELECT 1;" , ex . Message ) ;
249+ }
250+
251+ [ Fact ]
252+ public async Task ImplicitSession_WithExplicitTransaction_UsesExplicitSessionAndCommits ( )
253+ {
254+ var table = $ "Implicit_{ Guid . NewGuid ( ) : N} ";
255+
256+ await using var connection = CreateConnection ( ) ;
257+ connection . ConnectionString += ";EnableImplicitSession=true" ;
258+ await connection . OpenAsync ( ) ;
259+
260+ try
261+ {
262+ await using ( var create = connection . CreateCommand ( ) )
263+ {
264+ create . CommandText = $ """
265+ CREATE TABLE { table } (
266+ Id Int32,
267+ Name Text,
268+ PRIMARY KEY (Id)
269+ )
270+ """ ;
271+ await create . ExecuteNonQueryAsync ( ) ;
272+ }
273+
274+ var tx = connection . BeginTransaction ( ) ;
275+ await using ( var insert = connection . CreateCommand ( ) )
276+ {
277+ insert . Transaction = tx ;
278+ insert . CommandText = $ "INSERT INTO { table } (Id, Name) VALUES (1, 'A');";
279+ await insert . ExecuteNonQueryAsync ( ) ;
280+ insert . CommandText = $ "INSERT INTO { table } (Id, Name) VALUES (2, 'B');";
281+ await insert . ExecuteNonQueryAsync ( ) ;
282+ }
283+
284+ await tx . CommitAsync ( ) ;
285+
286+ await using ( var check = connection . CreateCommand ( ) )
287+ {
288+ check . CommandText = $ "SELECT COUNT(*) FROM { table } ;";
289+ var count = Convert . ToInt32 ( await check . ExecuteScalarAsync ( ) ) ;
290+ Assert . Equal ( 2 , count ) ;
291+ }
292+ }
293+ finally
294+ {
295+ await using var drop = connection . CreateCommand ( ) ;
296+ drop . CommandText = $ "DROP TABLE { table } ";
297+ await drop . ExecuteNonQueryAsync ( ) ;
298+ }
299+ }
300+
301+ [ Fact ]
302+ public async Task ImplicitSession_Cancellation_AfterFirstResult_StillReturnsFirst ( )
303+ {
304+ await using var connection = CreateConnection ( ) ;
305+ connection . ConnectionString += ";EnableImplicitSession=true" ;
306+ await connection . OpenAsync ( ) ;
307+
308+ var cmd = new YdbCommand ( connection ) { CommandText = "SELECT 1; SELECT 1;" } ;
309+ using var cts = new CancellationTokenSource ( ) ;
310+
311+ var reader = await cmd . ExecuteReaderAsync ( cts . Token ) ;
312+
313+ await reader . ReadAsync ( cts . Token ) ;
314+ Assert . Equal ( 1 , reader . GetValue ( 0 ) ) ;
315+ Assert . True ( await reader . NextResultAsync ( cts . Token ) ) ;
316+
317+ await cts . CancelAsync ( ) ;
318+
319+ await reader . ReadAsync ( cts . Token ) ;
320+ Assert . Equal ( 1 , reader . GetValue ( 0 ) ) ;
321+ Assert . False ( await reader . NextResultAsync ( ) ) ;
322+ }
207323
208324 public class Data < T > ( DbType dbType , T expected , bool isNullable = false )
209325 {
0 commit comments