|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | - |
| 5 | +using System.Collections.Generic; |
6 | 6 | using System.Data.Common; |
| 7 | +using System.Diagnostics; |
7 | 8 | using System.Reflection; |
| 9 | +using System.Threading; |
8 | 10 | using Xunit; |
9 | 11 |
|
10 | 12 | namespace System.Data.SqlClient.Tests |
@@ -79,5 +81,97 @@ public void SqlConnectionInvalidParameters() |
79 | 81 | Assert.Throws<ArgumentException>(() => new SqlConnection("Timeout=1a;")); |
80 | 82 | Assert.Throws<ArgumentException>(() => new SqlConnection("Integrated Security=truee")); |
81 | 83 | } |
| 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 | + } |
82 | 176 | } |
83 | 177 | } |
0 commit comments