/// <summary> /// Tests if killing the connection using the InternalConnectionWrapper is working /// </summary> /// <param name="connectionString"></param> private static void KillConnectionTest(string connectionString) { InternalConnectionWrapper wrapper = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); wrapper = new InternalConnectionWrapper(connection); using (SqlCommand command = new SqlCommand("SELECT 5;", connection)) { CompareScalarResults(5, command.ExecuteScalar()); } wrapper.KillConnection(); } using (SqlConnection connection2 = new SqlConnection(connectionString)) { connection2.Open(); Assert.False(wrapper.IsInternalConnectionOf(connection2), "New connection has internal connection that was just killed"); using (SqlCommand command = new SqlCommand("SELECT 5;", connection2)) { CompareScalarResults(5, command.ExecuteScalar()); } } }
/// <summary> /// Tests if killing the connection using the InternalConnectionWrapper is working /// </summary> /// <param name="connectionString"></param> private static void KillConnectionTest(string connectionString) { #if DEBUG InternalConnectionWrapper wrapper = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); wrapper = new InternalConnectionWrapper(connection); using (SqlCommand command = new SqlCommand("SELECT 5;", connection)) { DataTestUtility.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result."); } wrapper.KillConnection(); } using (SqlConnection connection2 = new SqlConnection(connectionString)) { connection2.Open(); Assert.False(wrapper.IsInternalConnectionOf(connection2), "New connection has internal connection that was just killed"); using (SqlCommand command = new SqlCommand("SELECT 5;", connection2)) { DataTestUtility.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result."); } } #endif }
private static void ReplacementConnectionUsesSemaphoreTest(string connectionString) { string newConnectionString = (new SqlConnectionStringBuilder(connectionString) { MaxPoolSize = 2, ConnectTimeout = 5 }).ConnectionString; SqlConnection.ClearAllPools(); SqlConnection liveConnection = new SqlConnection(newConnectionString); SqlConnection deadConnection = new SqlConnection(newConnectionString); liveConnection.Open(); deadConnection.Open(); InternalConnectionWrapper deadConnectionInternal = new InternalConnectionWrapper(deadConnection); InternalConnectionWrapper liveConnectionInternal = new InternalConnectionWrapper(liveConnection); deadConnectionInternal.KillConnection(); deadConnection.Close(); liveConnection.Close(); Task <InternalConnectionWrapper>[] tasks = new Task <InternalConnectionWrapper> [3]; Barrier syncBarrier = new Barrier(tasks.Length); Func <InternalConnectionWrapper> taskFunction = (() => ReplacementConnectionUsesSemaphoreTask(newConnectionString, syncBarrier)); for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Factory.StartNew <InternalConnectionWrapper>(taskFunction); } bool taskWithLiveConnection = false; bool taskWithNewConnection = false; bool taskWithCorrectException = false; Task waitAllTask = Task.Factory.ContinueWhenAll(tasks, (completedTasks) => { foreach (var item in completedTasks) { if (item.Status == TaskStatus.Faulted) { // One task should have a timeout exception if ((!taskWithCorrectException) && (item.Exception.InnerException is InvalidOperationException) && (item.Exception.InnerException.Message.StartsWith(SystemDataResourceManager.Instance.ADP_PooledOpenTimeout))) { taskWithCorrectException = true; } else if (!taskWithCorrectException) { // Rethrow the unknown exception ExceptionDispatchInfo exceptionInfo = ExceptionDispatchInfo.Capture(item.Exception); exceptionInfo.Throw(); } } else if (item.Status == TaskStatus.RanToCompletion) { // One task should get the live connection if (item.Result.Equals(liveConnectionInternal)) { if (!taskWithLiveConnection) { taskWithLiveConnection = true; } } else if (!item.Result.Equals(deadConnectionInternal) && !taskWithNewConnection) { taskWithNewConnection = true; } } else { Console.WriteLine("ERROR: Task in unknown state: {0}", item.Status); } } }); waitAllTask.Wait(); Assert.True(taskWithLiveConnection && taskWithNewConnection && taskWithCorrectException, string.Format("Tasks didn't finish as expected.\nTask with live connection: {0}\nTask with new connection: {1}\nTask with correct exception: {2}\n", taskWithLiveConnection, taskWithNewConnection, taskWithCorrectException)); }
private static void ReplacementConnectionUsesSemaphoreTest(string connectionString) { string newConnectionString = (new SqlConnectionStringBuilder(connectionString) { MaxPoolSize = 2, ConnectTimeout = 5 }).ConnectionString; SqlConnection.ClearAllPools(); SqlConnection liveConnection = new SqlConnection(newConnectionString); SqlConnection deadConnection = new SqlConnection(newConnectionString); liveConnection.Open(); deadConnection.Open(); InternalConnectionWrapper deadConnectionInternal = new InternalConnectionWrapper(deadConnection); InternalConnectionWrapper liveConnectionInternal = new InternalConnectionWrapper(liveConnection); deadConnectionInternal.KillConnection(); deadConnection.Close(); liveConnection.Close(); Task<InternalConnectionWrapper>[] tasks = new Task<InternalConnectionWrapper>[3]; Barrier syncBarrier = new Barrier(tasks.Length); Func<InternalConnectionWrapper> taskFunction = (() => ReplacementConnectionUsesSemaphoreTask(newConnectionString, syncBarrier)); for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Factory.StartNew<InternalConnectionWrapper>(taskFunction); } bool taskWithLiveConnection = false; bool taskWithNewConnection = false; bool taskWithCorrectException = false; Task waitAllTask = Task.Factory.ContinueWhenAll(tasks, (completedTasks) => { foreach (var item in completedTasks) { if (item.Status == TaskStatus.Faulted) { // One task should have a timeout exception if ((!taskWithCorrectException) && (item.Exception.InnerException is InvalidOperationException) && (item.Exception.InnerException.Message.StartsWith(SystemDataResourceManager.Instance.ADP_PooledOpenTimeout))) taskWithCorrectException = true; else if (!taskWithCorrectException) { // Rethrow the unknown exception ExceptionDispatchInfo exceptionInfo = ExceptionDispatchInfo.Capture(item.Exception); exceptionInfo.Throw(); } } else if (item.Status == TaskStatus.RanToCompletion) { // One task should get the live connection if (item.Result.Equals(liveConnectionInternal)) { if (!taskWithLiveConnection) taskWithLiveConnection = true; } else if (!item.Result.Equals(deadConnectionInternal) && !taskWithNewConnection) taskWithNewConnection = true; } else Console.WriteLine("ERROR: Task in unknown state: {0}", item.Status); } }); waitAllTask.Wait(); Assert.True(taskWithLiveConnection && taskWithNewConnection && taskWithCorrectException, string.Format("Tasks didn't finish as expected.\nTask with live connection: {0}\nTask with new connection: {1}\nTask with correct exception: {2}\n", taskWithLiveConnection, taskWithNewConnection, taskWithCorrectException)); }
/// <summary> /// Tests if killing the connection using the InternalConnectionWrapper is working /// </summary> /// <param name="connectionString"></param> private static void KillConnectionTest(string connectionString) { InternalConnectionWrapper wrapper = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); wrapper = new InternalConnectionWrapper(connection); using (SqlCommand command = new SqlCommand("SELECT 5;", connection)) { DataTestClass.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result."); } wrapper.KillConnection(); } using (SqlConnection connection2 = new SqlConnection(connectionString)) { connection2.Open(); Assert.False(wrapper.IsInternalConnectionOf(connection2), "New connection has internal connection that was just killed"); using (SqlCommand command = new SqlCommand("SELECT 5;", connection2)) { DataTestClass.AssertEqualsWithDescription(5, command.ExecuteScalar(), "Incorrect scalar result."); } } }
/// <summary> /// Tests if killing the connection using the InternalConnectionWrapper is working /// </summary> /// <param name="connectionString"></param> private static void KillConnectionTest(string connectionString) { InternalConnectionWrapper wrapper = null; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); wrapper = new InternalConnectionWrapper(connection); using (SqlCommand command = new SqlCommand("SELECT 5;", connection)) { CompareScalarResults(5, command.ExecuteScalar()); } wrapper.KillConnection(); } using (SqlConnection connection2 = new SqlConnection(connectionString)) { connection2.Open(); Assert.False(wrapper.IsInternalConnectionOf(connection2), "New connection has internal connection that was just killed"); using (SqlCommand command = new SqlCommand("SELECT 5;", connection2)) { CompareScalarResults(5, command.ExecuteScalar()); } } }