File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed
Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 22// Licensed under the MIT License. See License.txt in the project root for license information.
33
44using System ;
5+ using System . Collections . Generic ;
56using System . Data ;
67using System . Diagnostics ;
78using System . Threading ;
@@ -117,5 +118,36 @@ public static object ExecuteScalar(
117118
118119 return null ;
119120 }
121+
122+ /// <summary>
123+ /// Retries the specified action, waiting for the specified duration in between each attempt
124+ /// </summary>
125+ /// <param name="action">The action to run</param>
126+ /// <param name="retryCount">The max number of retries to attempt</param>
127+ /// <param name="waitDurationMs">The duration in milliseconds between each attempt</param>
128+ /// <exception cref="AggregateException">Aggregate of all exceptions thrown if all retries failed</exception>
129+ public static void Retry ( Action action , int retryCount = 3 , int waitDurationMs = 10000 )
130+ {
131+ var exceptions = new List < Exception > ( ) ;
132+ while ( true )
133+ {
134+ try
135+ {
136+ action ( ) ;
137+ break ;
138+ }
139+ catch ( Exception ex )
140+ {
141+ exceptions . Add ( ex ) ;
142+ retryCount -- ;
143+ if ( retryCount == 0 )
144+ {
145+ throw new AggregateException ( $ "Action failed all retries", exceptions ) ;
146+ }
147+ Console . WriteLine ( $ "Error running action, retrying after { waitDurationMs } ms. { retryCount } retries left. { ex } ") ;
148+ Thread . Sleep ( waitDurationMs ) ;
149+ }
150+ }
151+ }
120152 }
121153}
Original file line number Diff line number Diff line change @@ -105,12 +105,14 @@ private void SetupDatabase()
105105 this . MasterConnectionString = connectionStringBuilder . ToString ( ) ;
106106
107107 // Create database
108+ // Retry this in case the server isn't fully initialized yet
108109 this . DatabaseName = TestUtils . GetUniqueDBName ( "SqlBindingsTest" ) ;
109- using ( var masterConnection = new SqlConnection ( this . MasterConnectionString ) )
110+ TestUtils . Retry ( ( ) =>
110111 {
112+ using var masterConnection = new SqlConnection ( this . MasterConnectionString ) ;
111113 masterConnection . Open ( ) ;
112114 TestUtils . ExecuteNonQuery ( masterConnection , $ "CREATE DATABASE [{ this . DatabaseName } ]") ;
113- }
115+ } ) ;
114116
115117 // Setup connection
116118 connectionStringBuilder . InitialCatalog = this . DatabaseName ;
You can’t perform that action at this time.
0 commit comments