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

Commit cf191eb

Browse files
author
Gene Lee
authored
SqlConnection timeout test is ported (#26306)
1 parent fe00858 commit cf191eb

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

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

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
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-
5+
using System.Collections.Generic;
66
using System.Data.Common;
7+
using System.Diagnostics;
78
using System.Reflection;
9+
using System.Threading;
810
using Xunit;
911

1012
namespace System.Data.SqlClient.Tests
@@ -79,5 +81,97 @@ public void SqlConnectionInvalidParameters()
7981
Assert.Throws<ArgumentException>(() => new SqlConnection("Timeout=1a;"));
8082
Assert.Throws<ArgumentException>(() => new SqlConnection("Integrated Security=truee"));
8183
}
84+
85+
[Fact]
86+
public void ConnectionTimeoutTestWithThread()
87+
{
88+
int timeoutSec = 5;
89+
string connStrNotAvailable = $"Server=tcp:fakeServer,1433;uid=fakeuser;pwd=fakepwd;Connection Timeout={timeoutSec}";
90+
91+
List<ConnectionWorker> list = new List<ConnectionWorker>();
92+
for (int i = 0; i < 10; ++i)
93+
{
94+
list.Add(new ConnectionWorker(connStrNotAvailable));
95+
}
96+
97+
ConnectionWorker.Start();
98+
ConnectionWorker.Stop();
99+
100+
double theMax = 0;
101+
foreach (ConnectionWorker w in list)
102+
{
103+
if (theMax < w.MaxTimeElapsed)
104+
{
105+
theMax = w.MaxTimeElapsed;
106+
}
107+
}
108+
109+
int threshold = (timeoutSec + 1) * 1000;
110+
Assert.True(theMax < threshold);
111+
}
112+
113+
public class ConnectionWorker
114+
{
115+
private static ManualResetEventSlim startEvent = new ManualResetEventSlim(false);
116+
private static List<ConnectionWorker> workerList = new List<ConnectionWorker>();
117+
private ManualResetEventSlim doneEvent = new ManualResetEventSlim(false);
118+
private double maxTimeElapsed;
119+
private Thread thread;
120+
private string connectionString;
121+
122+
public ConnectionWorker(string connectionString)
123+
{
124+
workerList.Add(this);
125+
this.connectionString = connectionString;
126+
thread = new Thread(new ThreadStart(SqlConnectionOpen));
127+
thread.Start();
128+
}
129+
130+
public double MaxTimeElapsed
131+
{
132+
get
133+
{
134+
return maxTimeElapsed;
135+
}
136+
}
137+
138+
public static void Start()
139+
{
140+
startEvent.Set();
141+
}
142+
143+
public static void Stop()
144+
{
145+
foreach (ConnectionWorker w in workerList)
146+
{
147+
w.doneEvent.Wait();
148+
}
149+
}
150+
151+
public void SqlConnectionOpen()
152+
{
153+
startEvent.Wait();
154+
155+
Stopwatch sw = new Stopwatch();
156+
using (SqlConnection con = new SqlConnection(connectionString))
157+
{
158+
sw.Start();
159+
try
160+
{
161+
con.Open();
162+
}
163+
catch { }
164+
sw.Stop();
165+
}
166+
167+
double elapsed = sw.Elapsed.TotalMilliseconds;
168+
if (maxTimeElapsed < elapsed)
169+
{
170+
maxTimeElapsed = elapsed;
171+
}
172+
173+
doneEvent.Set();
174+
}
175+
}
82176
}
83177
}

0 commit comments

Comments
 (0)