Skip to content

Commit 06f7eca

Browse files
authored
Reduce automated test crashes (#2968)
* Converted Threads to long-running Tasks The key advantage is that exceptions propagate properly. If a thread throws an exception (as a result of a failed test assertion, or otherwise) then the test host crashes and must be restarted. * Corrected the instantiation of the cancellation task - missing state parameter. * Changes to TestSqlCommandCancel, eliminating timing-specific cancellation behaviour testing. This should also allow the test to run on both netcore and netfx. * Responding to code review. * Removed two unnecessary iterations from DatabaseHelper. * Added explanatory comments to ApiShould. * Switched to using Task.WaitAll rather than waiting for each Task in sequence. * Improve cancellation detection Cancellation can trigger one of several different errors, resulting in a flakier test. Also ensure that the query always takes more than 150ms, ensuring that a quick query execution doesn't cause the test to fail. Finally, make sure that we try to read everything from the SqlDataReader. * Correcting previous merge
1 parent 9e5eb97 commit 06f7eca

File tree

6 files changed

+168
-128
lines changed

6 files changed

+168
-128
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/ApiShould.cs

Lines changed: 149 additions & 95 deletions
Large diffs are not rendered by default.

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/DatabaseHelper.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,8 @@ public IEnumerator<object[]> GetEnumerator()
290290
{
291291
foreach (string connStrAE in DataTestUtility.AEConnStrings)
292292
{
293-
yield return new object[] { connStrAE, @"ExecuteReader", 1 };
294-
yield return new object[] { connStrAE, @"ExecuteReader", 3 };
295-
yield return new object[] { connStrAE, @"ExecuteNonQuery", 1 };
296-
yield return new object[] { connStrAE, @"ExecuteNonQuery", 3 };
293+
yield return new object[] { connStrAE, @"ExecuteReader" };
294+
yield return new object[] { connStrAE, @"ExecuteNonQuery" };
297295
}
298296
}
299297
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public class ConnectionWorker : IDisposable
171171
private static List<ConnectionWorker> s_workerList = new();
172172
private ManualResetEventSlim _doneEvent = new(false);
173173
private double _timeElapsed;
174-
private Thread _thread;
174+
private Task _task;
175175
private string _connectionString;
176176
private int _numOfTry;
177177

@@ -180,7 +180,7 @@ public ConnectionWorker(string connectionString, int numOfTry)
180180
s_workerList.Add(this);
181181
_connectionString = connectionString;
182182
_numOfTry = numOfTry;
183-
_thread = new Thread(new ThreadStart(SqlConnectionOpen));
183+
_task = new Task(SqlConnectionOpen, TaskCreationOptions.LongRunning);
184184
}
185185

186186
public static List<ConnectionWorker> WorkerList => s_workerList;
@@ -191,7 +191,7 @@ public static void Start()
191191
{
192192
foreach (ConnectionWorker w in s_workerList)
193193
{
194-
w._thread.Start();
194+
w._task.Start();
195195
}
196196
}
197197

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/MirroringTest/ConnectionOnMirroringTest.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Data;
88
using System.Threading;
9+
using System.Threading.Tasks;
910
using Xunit;
1011

1112
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
@@ -28,17 +29,14 @@ public static void TestMultipleConnectionToMirroredServer()
2829
builder.ConnectTimeout = 0;
2930

3031
TestWorker worker = new TestWorker(builder.ConnectionString);
31-
Thread childThread = new Thread(() => worker.TestMultipleConnection());
32-
childThread.Start();
32+
Task childTask = Task.Factory.StartNew(() => worker.TestMultipleConnection(), TaskCreationOptions.LongRunning);
3333

3434
if (workerCompletedEvent.WaitOne(10000))
3535
{
36-
childThread.Join();
36+
childTask.Wait();
3737
}
3838
else
3939
{
40-
// currently Thread.Abort() throws PlatformNotSupportedException in CoreFx.
41-
childThread.Interrupt();
4240
throw new Exception("SqlConnection could not open and close successfully in timely manner. Possibly connection hangs.");
4341
}
4442
}

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/ParametersTest.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Data;
99
using System.Data.SqlTypes;
1010
using System.Threading;
11+
using System.Threading.Tasks;
1112
using Xunit;
1213
using System.Globalization;
1314

@@ -957,30 +958,19 @@ private static void EnableOptimizedParameterBinding_ReturnSucceeds()
957958
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
958959
public static void ClosedConnection_SqlParameterValueTest()
959960
{
960-
var threads = new List<Thread>();
961-
for (int i = 0; i < 100; i++)
961+
var tasks = new Task[100];
962+
for (int i = 0; i < tasks.Length; i++)
962963
{
963-
var t = new Thread(() =>
964+
var t = Task.Factory.StartNew(() =>
964965
{
965966
for (int j = 0; j < 1000; j++)
966967
{
967-
try
968-
{
969-
RunParameterTest();
970-
}
971-
catch (Exception e)
972-
{
973-
Assert.Fail($"Unexpected exception occurred: {e.Message}");
974-
}
968+
RunParameterTest();
975969
}
976-
});
977-
t.Start();
978-
threads.Add(t);
979-
}
980-
for (int i = 0; i < threads.Count; i++)
981-
{
982-
threads[i].Join();
970+
}, TaskCreationOptions.LongRunning);
971+
tasks[i] = t;
983972
}
973+
Task.WaitAll(tasks);
984974
}
985975

986976
private static void RunParameterTest()
@@ -998,7 +988,7 @@ private static void RunParameterTest()
998988
cm.Parameters.Add(new SqlParameter("@id2", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output });
999989
try
1000990
{
1001-
System.Threading.Tasks.Task<int> task = cm.ExecuteNonQueryAsync(cancellationToken.Token);
991+
Task<int> task = cm.ExecuteNonQueryAsync(cancellationToken.Token);
1002992
task.Wait();
1003993
}
1004994
catch (Exception)

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/RandomStressTest/RandomStressTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Diagnostics;
99
using System.Text;
1010
using System.Threading;
11+
using System.Threading.Tasks;
1112
using Xunit;
1213

1314
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
@@ -70,8 +71,7 @@ public void TestMain()
7071
{
7172
for (int tcount = 0; tcount < ThreadCountDefault; tcount++)
7273
{
73-
Thread t = new Thread(TestThread);
74-
t.Start();
74+
_ = Task.Factory.StartNew(TestThread, TaskCreationOptions.LongRunning);
7575
}
7676
}
7777

0 commit comments

Comments
 (0)