Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 94fe402

Browse files
author
Gene Lee
authored
Fix ConnectionTimeoutTestWithThread test (#28618)
1 parent 7cd1637 commit 94fe402

File tree

2 files changed

+100
-96
lines changed

2 files changed

+100
-96
lines changed

src/System.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Collections.Generic;
65
using System.Data.Common;
7-
using System.Diagnostics;
86
using System.Reflection;
97
using System.Security;
10-
using System.Threading;
118
using Xunit;
129

1310
namespace System.Data.SqlClient.Tests
@@ -107,35 +104,6 @@ public void RetrieveWorkstationId(string workstation, bool withDispose, bool sho
107104
Assert.Equal(expected, conn.WorkstationId);
108105
}
109106

110-
[Fact]
111-
public void ConnectionTimeoutTestWithThread()
112-
{
113-
int timeoutSec = 5;
114-
string connStrNotAvailable = $"Server=tcp:fakeServer,1433;uid=fakeuser;pwd=fakepwd;Connection Timeout={timeoutSec}";
115-
116-
List<ConnectionWorker> list = new List<ConnectionWorker>();
117-
for (int i = 0; i < 10; ++i)
118-
{
119-
list.Add(new ConnectionWorker(connStrNotAvailable));
120-
}
121-
122-
ConnectionWorker.Start();
123-
ConnectionWorker.Stop();
124-
125-
double theMax = 0;
126-
foreach (ConnectionWorker w in list)
127-
{
128-
if (theMax < w.MaxTimeElapsed)
129-
{
130-
theMax = w.MaxTimeElapsed;
131-
}
132-
}
133-
134-
int threshold = (timeoutSec + 1) * 1000;
135-
136-
Console.WriteLine($"ConnectionTimeoutTestWithThread: Elapsed Time {theMax} and threshold {threshold}");
137-
}
138-
139107
[OuterLoop("Can take up to 4 seconds")]
140108
[Fact]
141109
public void ExceptionsWithMinPoolSizeCanBeHandled()
@@ -195,69 +163,5 @@ public void ConnectionTestValidCredentialCombination()
195163

196164
Assert.Equal(sqlCredential, conn.Credential);
197165
}
198-
199-
public class ConnectionWorker
200-
{
201-
private static ManualResetEventSlim startEvent = new ManualResetEventSlim(false);
202-
private static List<ConnectionWorker> workerList = new List<ConnectionWorker>();
203-
private ManualResetEventSlim doneEvent = new ManualResetEventSlim(false);
204-
private double maxTimeElapsed;
205-
private Thread thread;
206-
private string connectionString;
207-
208-
public ConnectionWorker(string connectionString)
209-
{
210-
workerList.Add(this);
211-
this.connectionString = connectionString;
212-
thread = new Thread(new ThreadStart(SqlConnectionOpen));
213-
thread.Start();
214-
}
215-
216-
public double MaxTimeElapsed
217-
{
218-
get
219-
{
220-
return maxTimeElapsed;
221-
}
222-
}
223-
224-
public static void Start()
225-
{
226-
startEvent.Set();
227-
}
228-
229-
public static void Stop()
230-
{
231-
foreach (ConnectionWorker w in workerList)
232-
{
233-
w.doneEvent.Wait();
234-
}
235-
}
236-
237-
public void SqlConnectionOpen()
238-
{
239-
startEvent.Wait();
240-
241-
Stopwatch sw = new Stopwatch();
242-
using (SqlConnection con = new SqlConnection(connectionString))
243-
{
244-
sw.Start();
245-
try
246-
{
247-
con.Open();
248-
}
249-
catch { }
250-
sw.Stop();
251-
}
252-
253-
double elapsed = sw.Elapsed.TotalMilliseconds;
254-
if (maxTimeElapsed < elapsed)
255-
{
256-
maxTimeElapsed = elapsed;
257-
}
258-
259-
doneEvent.Set();
260-
}
261-
}
262166
}
263167
}

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Threading;
58
using Xunit;
69

710
namespace System.Data.SqlClient.ManualTesting.Tests
@@ -45,5 +48,102 @@ public static void EnvironmentHostNameTest()
4548
}
4649
Assert.True(false, "No non-empty hostname found for the application");
4750
}
51+
52+
[CheckConnStrSetupFact]
53+
public static void ConnectionTimeoutTestWithThread()
54+
{
55+
const int timeoutSec = 5;
56+
const int numOfTry = 2;
57+
const int numOfThreads = 5;
58+
59+
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DataTestUtility.TcpConnStr);
60+
builder.DataSource = "invalidhost";
61+
builder.ConnectTimeout = timeoutSec;
62+
string connStrNotAvailable = builder.ConnectionString;
63+
64+
for (int i = 0; i < numOfThreads; ++i)
65+
{
66+
new ConnectionWorker(connStrNotAvailable, numOfTry);
67+
}
68+
69+
ConnectionWorker.Start();
70+
ConnectionWorker.Stop();
71+
72+
double timeTotal = 0;
73+
double timeElapsed = 0;
74+
75+
foreach (ConnectionWorker w in ConnectionWorker.WorkerList)
76+
{
77+
timeTotal += w.TimeElapsed;
78+
}
79+
timeElapsed = timeTotal / Convert.ToDouble(ConnectionWorker.WorkerList.Count);
80+
81+
int threshold = timeoutSec * numOfTry * 2 * 1000;
82+
83+
Assert.True(timeElapsed < threshold);
84+
}
85+
86+
public class ConnectionWorker
87+
{
88+
private static List<ConnectionWorker> workerList = new List<ConnectionWorker>();
89+
private ManualResetEventSlim _doneEvent = new ManualResetEventSlim(false);
90+
private double _timeElapsed;
91+
private Thread _thread;
92+
private string _connectionString;
93+
private int _numOfTry;
94+
95+
public ConnectionWorker(string connectionString, int numOfTry)
96+
{
97+
workerList.Add(this);
98+
_connectionString = connectionString;
99+
_numOfTry = numOfTry;
100+
_thread = new Thread(new ThreadStart(SqlConnectionOpen));
101+
}
102+
103+
public static List<ConnectionWorker> WorkerList => workerList;
104+
105+
public double TimeElapsed => _timeElapsed;
106+
107+
public static void Start()
108+
{
109+
foreach (ConnectionWorker w in workerList)
110+
{
111+
w._thread.Start();
112+
}
113+
}
114+
115+
public static void Stop()
116+
{
117+
foreach (ConnectionWorker w in workerList)
118+
{
119+
w._doneEvent.Wait();
120+
}
121+
}
122+
123+
public void SqlConnectionOpen()
124+
{
125+
Stopwatch sw = new Stopwatch();
126+
double totalTime = 0;
127+
for (int i = 0; i < _numOfTry; ++i)
128+
{
129+
using (SqlConnection con = new SqlConnection(_connectionString))
130+
{
131+
sw.Start();
132+
try
133+
{
134+
con.Open();
135+
}
136+
catch { }
137+
sw.Stop();
138+
}
139+
totalTime += sw.Elapsed.TotalMilliseconds;
140+
sw.Reset();
141+
}
142+
143+
_timeElapsed = totalTime / Convert.ToDouble(_numOfTry);
144+
145+
_doneEvent.Set();
146+
}
147+
}
48148
}
49149
}

0 commit comments

Comments
 (0)