66
77using System ;
88using System . Collections . Generic ;
9+ using System . Data . Common ;
910using System . Linq ;
11+ using System . Threading ;
1012using Xtensive . Orm . Providers ;
1113using Xtensive . Sql ;
1214using Xtensive . Sql . Model ;
@@ -17,15 +19,13 @@ public static class StorageTestHelper
1719 {
1820 public static bool IsFetched ( Session session , Key key )
1921 {
20- EntityState dummy ;
21- return session . EntityStateCache . TryGetItem ( key , false , out dummy ) ;
22+ return session . EntityStateCache . TryGetItem ( key , false , out var _ ) ;
2223 }
2324
24- public static object GetNativeTransaction ( )
25+ public static object GetNativeTransaction ( Session session )
2526 {
26- var handler = Session . Demand ( ) . Handler ;
27- var sqlHandler = handler as SqlSessionHandler ;
28- if ( sqlHandler != null )
27+ var handler = session . Handler ;
28+ if ( handler is SqlSessionHandler sqlHandler )
2929 return sqlHandler . Connection . ActiveTransaction ;
3030 throw new NotSupportedException ( ) ;
3131 }
@@ -66,6 +66,39 @@ public static void DemandSchemas(ConnectionInfo connectionInfo, params string[]
6666 }
6767 }
6868
69+ /// <summary>
70+ /// Waits for full-text indexes of MS SQL to be populated.
71+ /// Every now and then it gets state of them from database or waits timeout to be reached.
72+ /// </summary>
73+ /// <param name="domain"></param>
74+ public static void WaitFullTextIndexesPopulated ( Domain domain , TimeSpan timeout )
75+ {
76+ if ( StorageProviderInfo . Instance . Provider == StorageProvider . SqlServer ) {
77+ var driver = TestSqlDriver . Create ( domain . Configuration . ConnectionInfo ) ;
78+ using ( var connection = driver . CreateConnection ( ) ) {
79+
80+ var date = DateTime . UtcNow . Add ( timeout ) ;
81+ while ( ! CheckFtIndexesPopulated ( connection ) && DateTime . UtcNow < date ) {
82+ Thread . Sleep ( TimeSpan . FromSeconds ( 2 ) ) ;
83+ }
84+ }
85+ }
86+
87+ static bool CheckFtIndexesPopulated ( SqlConnection connection )
88+ {
89+ connection . Open ( ) ;
90+ try {
91+ using ( var command = connection . CreateCommand ( ) ) {
92+ command . CommandText = $ "SELECT COUNT(*) FROM sys.fulltext_indexes WHERE has_crawl_completed = 0";
93+ return ( ( int ) command . ExecuteScalar ( ) ) == 0 ;
94+ }
95+ }
96+ finally {
97+ connection . Close ( ) ;
98+ }
99+ }
100+ }
101+
69102 private static void CreateUsers ( SqlConnection connection , IEnumerable < string > schemasToCreate )
70103 {
71104 var translator = connection . Driver . Translator ;
@@ -86,14 +119,14 @@ private static void CreateSchemas(SqlConnection connection, Catalog catalog, IEn
86119
87120 private static void ExecuteQuery ( SqlConnection connection , ISqlCompileUnit query )
88121 {
89- using ( var command = connection . CreateCommand ( query ) )
90- command . ExecuteNonQuery ( ) ;
122+ using var command = connection . CreateCommand ( query ) ;
123+ _ = command . ExecuteNonQuery ( ) ;
91124 }
92125
93126 private static void ExecuteQuery ( SqlConnection connection , string query )
94127 {
95- using ( var command = connection . CreateCommand ( query ) )
96- command . ExecuteNonQuery ( ) ;
128+ using var command = connection . CreateCommand ( query ) ;
129+ _ = command . ExecuteNonQuery ( ) ;
97130 }
98131 }
99132}
0 commit comments