@@ -494,4 +494,46 @@ public void ConnectionStringSetThreadsAndAccessModeOption(string accessMode, int
494494 value . Should ( ) . Be ( threads ) ;
495495 }
496496 }
497+
498+ [ Fact ]
499+ public void ConnectionStateHandlerIsCalledOnOpen ( )
500+ {
501+ using var dbInfo = DisposableFile . GenerateInTemp ( "db" ) ;
502+ using var connection = new DuckDBConnection ( dbInfo . ConnectionString ) ;
503+ var handlerCalled = false ;
504+ connection . StateChange += Assert ;
505+ connection . Open ( ) ;
506+ connection . StateChange -= Assert ; // otherwise the dispose/close will trigger the assert
507+
508+ handlerCalled . Should ( ) . BeTrue ( ) ;
509+ return ;
510+
511+ void Assert ( object sender , StateChangeEventArgs args )
512+ {
513+ args . OriginalState . Should ( ) . Be ( ConnectionState . Closed ) ;
514+ args . CurrentState . Should ( ) . Be ( ConnectionState . Open ) ;
515+ handlerCalled = true ;
516+ }
517+ }
518+
519+ [ Fact ]
520+ public async Task ConnectionStateHandlerIsCalledOnClose ( )
521+ {
522+ using var dbInfo = DisposableFile . GenerateInTemp ( "db" ) ;
523+ await using var connection = new DuckDBConnection ( dbInfo . ConnectionString ) ;
524+ var handlerCalled = false ;
525+ await connection . OpenAsync ( ) ;
526+ connection . StateChange += Assert ;
527+ await connection . CloseAsync ( ) ;
528+
529+ handlerCalled . Should ( ) . BeTrue ( ) ;
530+ return ;
531+
532+ void Assert ( object sender , StateChangeEventArgs args )
533+ {
534+ args . OriginalState . Should ( ) . Be ( ConnectionState . Open ) ;
535+ args . CurrentState . Should ( ) . Be ( ConnectionState . Closed ) ;
536+ handlerCalled = true ;
537+ }
538+ }
497539}
0 commit comments