Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce automated test crashes #2968

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,8 @@ public IEnumerator<object[]> GetEnumerator()
{
foreach (string connStrAE in DataTestUtility.AEConnStrings)
{
yield return new object[] { connStrAE, @"ExecuteReader", 1 };
yield return new object[] { connStrAE, @"ExecuteReader", 3 };
yield return new object[] { connStrAE, @"ExecuteNonQuery", 1 };
yield return new object[] { connStrAE, @"ExecuteNonQuery", 3 };
yield return new object[] { connStrAE, @"ExecuteReader" };
edwardneal marked this conversation as resolved.
Show resolved Hide resolved
yield return new object[] { connStrAE, @"ExecuteNonQuery" };
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public class ConnectionWorker : IDisposable
private static List<ConnectionWorker> s_workerList = new();
private ManualResetEventSlim _doneEvent = new(false);
private double _timeElapsed;
private Thread _thread;
private Task _task;
private string _connectionString;
private int _numOfTry;

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

public static List<ConnectionWorker> WorkerList => s_workerList;
Expand All @@ -191,7 +191,7 @@ public static void Start()
{
foreach (ConnectionWorker w in s_workerList)
{
w._thread.Start();
w._task.Start();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

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

TestWorker worker = new TestWorker(builder.ConnectionString);
Thread childThread = new Thread(() => worker.TestMultipleConnection());
childThread.Start();
Task childTask = Task.Factory.StartNew(() => worker.TestMultipleConnection(), TaskCreationOptions.LongRunning);
benrr101 marked this conversation as resolved.
Show resolved Hide resolved

if (workerCompletedEvent.WaitOne(10000))
{
childThread.Join();
childTask.Wait();
}
else
{
// currently Thread.Abort() throws PlatformNotSupportedException in CoreFx.
childThread.Interrupt();
throw new Exception("SqlConnection could not open and close successfully in timely manner. Possibly connection hangs.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Data;
using System.Data.SqlTypes;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
#if !NETFRAMEWORK
using Microsoft.SqlServer.Types;
Expand Down Expand Up @@ -886,30 +887,19 @@ private static void EnableOptimizedParameterBinding_ReturnSucceeds()
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ClosedConnection_SqlParameterValueTest()
{
var threads = new List<Thread>();
for (int i = 0; i < 100; i++)
var tasks = new Task[100];
for (int i = 0; i < tasks.Length; i++)
{
var t = new Thread(() =>
var t = Task.Factory.StartNew(() =>
{
for (int j = 0; j < 1000; j++)
{
try
{
RunParameterTest();
}
catch (Exception e)
{
Assert.Fail($"Unexpected exception occurred: {e.Message}");
}
RunParameterTest();
}
});
t.Start();
threads.Add(t);
}
for (int i = 0; i < threads.Count; i++)
{
threads[i].Join();
}, TaskCreationOptions.LongRunning);
tasks[i] = t;
}
Task.WaitAll(tasks);
}

private static void RunParameterTest()
Expand All @@ -927,7 +917,7 @@ private static void RunParameterTest()
cm.Parameters.Add(new SqlParameter("@id2", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output });
try
{
System.Threading.Tasks.Task<int> task = cm.ExecuteNonQueryAsync(cancellationToken.Token);
Task<int> task = cm.ExecuteNonQueryAsync(cancellationToken.Token);
task.Wait();
}
catch (Exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
Expand Down Expand Up @@ -70,8 +71,7 @@ public void TestMain()
{
for (int tcount = 0; tcount < ThreadCountDefault; tcount++)
{
Thread t = new Thread(TestThread);
t.Start();
_ = Task.Factory.StartNew(TestThread, TaskCreationOptions.LongRunning);
}
}

Expand Down
Loading