|
8 | 8 | namespace MySqlConnector.Tests; |
9 | 9 |
|
10 | 10 | public class ConnectionTests : IDisposable |
| 11 | +{ |
| 12 | + public ConnectionTests() |
11 | 13 | { |
12 | | - public ConnectionTests() |
13 | | - { |
14 | | - m_server = new(); |
15 | | - m_server.Start(); |
16 | | - |
17 | | - m_csb = new() |
18 | | - { |
19 | | - Server = "localhost", |
20 | | - Port = (uint) m_server.Port, |
21 | | - }; |
22 | | - } |
| 14 | + m_server = new(); |
| 15 | + m_server.Start(); |
23 | 16 |
|
24 | | - public void Dispose() |
| 17 | + m_csb = new() |
25 | 18 | { |
26 | | - m_server.Stop(); |
27 | | - } |
| 19 | + Server = "localhost", |
| 20 | + Port = (uint) m_server.Port, |
| 21 | + }; |
| 22 | + } |
28 | 23 |
|
29 | | - [Fact] |
30 | | - public void PooledConnectionIsReturnedToPool() |
31 | | - { |
32 | | - Assert.Equal(0, m_server.ActiveConnections); |
| 24 | + public void Dispose() |
| 25 | + { |
| 26 | + m_server.Stop(); |
| 27 | + } |
33 | 28 |
|
34 | | - m_csb.Pooling = true; |
35 | | - using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
36 | | - { |
37 | | - connection.Open(); |
38 | | - Assert.Equal(1, m_server.ActiveConnections); |
| 29 | + [Fact] |
| 30 | + public void PooledConnectionIsReturnedToPool() |
| 31 | + { |
| 32 | + Assert.Equal(0, m_server.ActiveConnections); |
39 | 33 |
|
40 | | - Assert.Equal(m_server.ServerVersion, connection.ServerVersion); |
41 | | - connection.Close(); |
42 | | - Assert.Equal(1, m_server.ActiveConnections); |
43 | | - } |
| 34 | + m_csb.Pooling = true; |
| 35 | + using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
| 36 | + { |
| 37 | + connection.Open(); |
| 38 | + Assert.Equal(1, m_server.ActiveConnections); |
44 | 39 |
|
| 40 | + Assert.Equal(m_server.ServerVersion, connection.ServerVersion); |
| 41 | + connection.Close(); |
45 | 42 | Assert.Equal(1, m_server.ActiveConnections); |
46 | 43 | } |
47 | 44 |
|
48 | | - [Fact] |
49 | | - public async Task PooledConnectionIsReturnedToPoolAsync() |
50 | | - { |
51 | | - Assert.Equal(0, m_server.ActiveConnections); |
| 45 | + Assert.Equal(1, m_server.ActiveConnections); |
| 46 | + } |
52 | 47 |
|
53 | | - m_csb.Pooling = true; |
54 | | - using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
55 | | - { |
56 | | - await connection.OpenAsync(); |
57 | | - Assert.Equal(1, m_server.ActiveConnections); |
| 48 | + [Fact] |
| 49 | + public async Task PooledConnectionIsReturnedToPoolAsync() |
| 50 | + { |
| 51 | + Assert.Equal(0, m_server.ActiveConnections); |
58 | 52 |
|
59 | | - Assert.Equal(m_server.ServerVersion, connection.ServerVersion); |
60 | | - await connection.CloseAsync(); |
61 | | - Assert.Equal(1, m_server.ActiveConnections); |
62 | | - } |
| 53 | + m_csb.Pooling = true; |
| 54 | + using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
| 55 | + { |
| 56 | + await connection.OpenAsync(); |
| 57 | + Assert.Equal(1, m_server.ActiveConnections); |
63 | 58 |
|
| 59 | + Assert.Equal(m_server.ServerVersion, connection.ServerVersion); |
| 60 | + await connection.CloseAsync(); |
64 | 61 | Assert.Equal(1, m_server.ActiveConnections); |
65 | 62 | } |
66 | 63 |
|
67 | | - [Fact] |
68 | | - public async Task UnpooledConnectionIsClosed() |
69 | | - { |
70 | | - Assert.Equal(0, m_server.ActiveConnections); |
| 64 | + Assert.Equal(1, m_server.ActiveConnections); |
| 65 | + } |
71 | 66 |
|
72 | | - m_csb.Pooling = false; |
73 | | - using var connection = new MySqlConnection(m_csb.ConnectionString); |
74 | | - await connection.OpenAsync(); |
75 | | - Assert.Equal(1, m_server.ActiveConnections); |
| 67 | + [Fact] |
| 68 | + public async Task UnpooledConnectionIsClosed() |
| 69 | + { |
| 70 | + Assert.Equal(0, m_server.ActiveConnections); |
76 | 71 |
|
77 | | - Assert.Equal(m_server.ServerVersion, connection.ServerVersion); |
78 | | - connection.Close(); |
| 72 | + m_csb.Pooling = false; |
| 73 | + using var connection = new MySqlConnection(m_csb.ConnectionString); |
| 74 | + await connection.OpenAsync(); |
| 75 | + Assert.Equal(1, m_server.ActiveConnections); |
79 | 76 |
|
80 | | - await WaitForConditionAsync(0, () => m_server.ActiveConnections); |
81 | | - } |
| 77 | + Assert.Equal(m_server.ServerVersion, connection.ServerVersion); |
| 78 | + connection.Close(); |
82 | 79 |
|
83 | | - [Theory] |
84 | | - [InlineData(2u, 3u, true)] |
85 | | - [InlineData(180u, 3u, false)] |
86 | | - public async Task ConnectionLifeTime(uint lifeTime, uint delaySeconds, bool shouldTimeout) |
87 | | - { |
88 | | - m_csb.Pooling = true; |
89 | | - m_csb.MinimumPoolSize = 0; |
90 | | - m_csb.MaximumPoolSize = 1; |
91 | | - m_csb.ConnectionLifeTime = lifeTime; |
92 | | - int serverThread; |
93 | | - |
94 | | - using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
95 | | - { |
96 | | - await connection.OpenAsync(); |
97 | | - serverThread = connection.ServerThread; |
98 | | - await Task.Delay(TimeSpan.FromSeconds(delaySeconds)); |
99 | | - } |
100 | | - |
101 | | - using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
102 | | - { |
103 | | - await connection.OpenAsync(); |
104 | | - if (shouldTimeout) |
105 | | - Assert.NotEqual(serverThread, connection.ServerThread); |
106 | | - else |
107 | | - Assert.Equal(serverThread, connection.ServerThread); |
108 | | - } |
109 | | - } |
| 80 | + await WaitForConditionAsync(0, () => m_server.ActiveConnections); |
| 81 | + } |
| 82 | + |
| 83 | + [Theory] |
| 84 | + [InlineData(2u, 3u, true)] |
| 85 | + [InlineData(180u, 3u, false)] |
| 86 | + public async Task ConnectionLifeTime(uint lifeTime, uint delaySeconds, bool shouldTimeout) |
| 87 | + { |
| 88 | + m_csb.Pooling = true; |
| 89 | + m_csb.MinimumPoolSize = 0; |
| 90 | + m_csb.MaximumPoolSize = 1; |
| 91 | + m_csb.ConnectionLifeTime = lifeTime; |
| 92 | + int serverThread; |
110 | 93 |
|
111 | | - [Theory] |
112 | | - [InlineData(3)] |
113 | | - [InlineData(7)] |
114 | | - public async Task MinimumPoolSize(int size) |
| 94 | + using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
115 | 95 | { |
116 | | - m_csb.MinimumPoolSize = (uint) size; |
117 | | - using var connection = new MySqlConnection(m_csb.ConnectionString); |
118 | 96 | await connection.OpenAsync(); |
119 | | - Assert.Equal(size, m_server.ActiveConnections); |
| 97 | + serverThread = connection.ServerThread; |
| 98 | + await Task.Delay(TimeSpan.FromSeconds(delaySeconds)); |
120 | 99 | } |
121 | 100 |
|
122 | | - [Fact] |
123 | | - public void LeakReaders() |
| 101 | + using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
124 | 102 | { |
125 | | - m_csb.Pooling = true; |
126 | | - m_csb.MinimumPoolSize = 0; |
127 | | - m_csb.MaximumPoolSize = 6; |
128 | | - m_csb.ConnectionTimeout = 30u; |
129 | | - |
130 | | - for (var i = 0; i < m_csb.MaximumPoolSize + 2; i++) |
131 | | - { |
132 | | - var connection = new MySqlConnection(m_csb.ConnectionString); |
133 | | - connection.Open(); |
134 | | - |
135 | | - var cmd = connection.CreateCommand(); |
136 | | - cmd.CommandText = "SELECT 1;"; |
137 | | - using (var reader = cmd.ExecuteReader()) |
138 | | - Assert.True(reader.Read()); |
139 | | - |
140 | | - // have to GC for leaked connections to be removed from the pool |
141 | | - GC.Collect(); |
142 | | - |
143 | | - // HACK: have to sleep (so that RecoverLeakedSessions is called in ConnectionPool.GetSessionAsync) |
144 | | - Thread.Sleep(250); |
145 | | - } |
| 103 | + await connection.OpenAsync(); |
| 104 | + if (shouldTimeout) |
| 105 | + Assert.NotEqual(serverThread, connection.ServerThread); |
| 106 | + else |
| 107 | + Assert.Equal(serverThread, connection.ServerThread); |
146 | 108 | } |
| 109 | + } |
147 | 110 |
|
148 | | - [Fact] |
149 | | - public void AuthPluginNameNotNullTerminated() |
150 | | - { |
151 | | - m_server.SuppressAuthPluginNameTerminatingNull = true; |
152 | | - using var connection = new MySqlConnection(m_csb.ConnectionString); |
153 | | - connection.Open(); |
154 | | - Assert.Equal(ConnectionState.Open, connection.State); |
155 | | - } |
| 111 | + [Theory] |
| 112 | + [InlineData(3)] |
| 113 | + [InlineData(7)] |
| 114 | + public async Task MinimumPoolSize(int size) |
| 115 | + { |
| 116 | + m_csb.MinimumPoolSize = (uint) size; |
| 117 | + using var connection = new MySqlConnection(m_csb.ConnectionString); |
| 118 | + await connection.OpenAsync(); |
| 119 | + Assert.Equal(size, m_server.ActiveConnections); |
| 120 | + } |
156 | 121 |
|
157 | | - [Fact] |
158 | | - public void IncompleteServerHandshake() |
159 | | - { |
160 | | - m_server.SendIncompletePostHandshakeResponse = true; |
161 | | - using var connection = new MySqlConnection(m_csb.ConnectionString); |
162 | | - Assert.Throws<MySqlException>(() => connection.Open()); |
163 | | - } |
| 122 | + [Fact] |
| 123 | + public void LeakReaders() |
| 124 | + { |
| 125 | + m_csb.Pooling = true; |
| 126 | + m_csb.MinimumPoolSize = 0; |
| 127 | + m_csb.MaximumPoolSize = 6; |
| 128 | + m_csb.ConnectionTimeout = 30u; |
164 | 129 |
|
165 | | - [Fact] |
166 | | - public void Ping() |
| 130 | + for (var i = 0; i < m_csb.MaximumPoolSize + 2; i++) |
167 | 131 | { |
168 | | - using var connection = new MySqlConnection(m_csb.ConnectionString); |
| 132 | + var connection = new MySqlConnection(m_csb.ConnectionString); |
169 | 133 | connection.Open(); |
170 | | - Assert.Equal(ConnectionState.Open, connection.State); |
171 | | - Assert.True(connection.Ping()); |
172 | | - Assert.Equal(ConnectionState.Open, connection.State); |
173 | | - } |
174 | 134 |
|
175 | | - [Fact] |
176 | | - public void PingWhenClosed() |
177 | | - { |
178 | | - using var connection = new MySqlConnection(m_csb.ConnectionString); |
179 | | - connection.Open(); |
180 | | - Assert.Equal(ConnectionState.Open, connection.State); |
181 | | - m_server.Stop(); |
182 | | - Assert.False(connection.Ping()); |
183 | | - Assert.Equal(ConnectionState.Closed, connection.State); |
| 135 | + var cmd = connection.CreateCommand(); |
| 136 | + cmd.CommandText = "SELECT 1;"; |
| 137 | + using (var reader = cmd.ExecuteReader()) |
| 138 | + Assert.True(reader.Read()); |
| 139 | + |
| 140 | + // have to GC for leaked connections to be removed from the pool |
| 141 | + GC.Collect(); |
| 142 | + |
| 143 | + // HACK: have to sleep (so that RecoverLeakedSessions is called in ConnectionPool.GetSessionAsync) |
| 144 | + Thread.Sleep(250); |
184 | 145 | } |
| 146 | + } |
185 | 147 |
|
186 | | - [Fact] |
187 | | - public void ConnectionTimeout() |
| 148 | + [Fact] |
| 149 | + public void AuthPluginNameNotNullTerminated() |
| 150 | + { |
| 151 | + m_server.SuppressAuthPluginNameTerminatingNull = true; |
| 152 | + using var connection = new MySqlConnection(m_csb.ConnectionString); |
| 153 | + connection.Open(); |
| 154 | + Assert.Equal(ConnectionState.Open, connection.State); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public void IncompleteServerHandshake() |
| 159 | + { |
| 160 | + m_server.SendIncompletePostHandshakeResponse = true; |
| 161 | + using var connection = new MySqlConnection(m_csb.ConnectionString); |
| 162 | + Assert.Throws<MySqlException>(() => connection.Open()); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public void Ping() |
| 167 | + { |
| 168 | + using var connection = new MySqlConnection(m_csb.ConnectionString); |
| 169 | + connection.Open(); |
| 170 | + Assert.Equal(ConnectionState.Open, connection.State); |
| 171 | + Assert.True(connection.Ping()); |
| 172 | + Assert.Equal(ConnectionState.Open, connection.State); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public void PingWhenClosed() |
| 177 | + { |
| 178 | + using var connection = new MySqlConnection(m_csb.ConnectionString); |
| 179 | + connection.Open(); |
| 180 | + Assert.Equal(ConnectionState.Open, connection.State); |
| 181 | + m_server.Stop(); |
| 182 | + Assert.False(connection.Ping()); |
| 183 | + Assert.Equal(ConnectionState.Closed, connection.State); |
| 184 | + } |
| 185 | + |
| 186 | + [Fact] |
| 187 | + public void ConnectionTimeout() |
| 188 | + { |
| 189 | + m_server.BlockOnConnect = true; |
| 190 | + var csb = new MySqlConnectionStringBuilder(m_csb.ConnectionString); |
| 191 | + csb.ConnectionTimeout = 4; |
| 192 | + using var connection = new MySqlConnection(csb.ConnectionString); |
| 193 | + var stopwatch = Stopwatch.StartNew(); |
| 194 | + try |
188 | 195 | { |
189 | | - m_server.BlockOnConnect = true; |
190 | | - var csb = new MySqlConnectionStringBuilder(m_csb.ConnectionString); |
191 | | - csb.ConnectionTimeout = 4; |
192 | | - using var connection = new MySqlConnection(csb.ConnectionString); |
193 | | - var stopwatch = Stopwatch.StartNew(); |
194 | | - try |
195 | | - { |
196 | | - connection.Open(); |
197 | | - Assert.False(true); |
198 | | - } |
199 | | - catch (MySqlException ex) |
200 | | - { |
201 | | - Assert.InRange(stopwatch.ElapsedMilliseconds, 3900, 4100); |
202 | | - Assert.Equal(MySqlErrorCode.UnableToConnectToHost, (MySqlErrorCode) ex.Number); |
203 | | - } |
| 196 | + connection.Open(); |
| 197 | + Assert.False(true); |
204 | 198 | } |
205 | | - |
206 | | - private static async Task WaitForConditionAsync<T>(T expected, Func<T> getValue) |
| 199 | + catch (MySqlException ex) |
207 | 200 | { |
208 | | - var sw = Stopwatch.StartNew(); |
209 | | - while (sw.ElapsedMilliseconds < 1000 && !expected.Equals(getValue())) |
210 | | - await Task.Delay(50); |
211 | | - Assert.Equal(expected, getValue()); |
| 201 | + Assert.InRange(stopwatch.ElapsedMilliseconds, 3900, 4100); |
| 202 | + Assert.Equal(MySqlErrorCode.UnableToConnectToHost, (MySqlErrorCode) ex.Number); |
212 | 203 | } |
| 204 | + } |
213 | 205 |
|
214 | | - readonly FakeMySqlServer m_server; |
215 | | - readonly MySqlConnectionStringBuilder m_csb; |
| 206 | + private static async Task WaitForConditionAsync<T>(T expected, Func<T> getValue) |
| 207 | + { |
| 208 | + var sw = Stopwatch.StartNew(); |
| 209 | + while (sw.ElapsedMilliseconds < 1000 && !expected.Equals(getValue())) |
| 210 | + await Task.Delay(50); |
| 211 | + Assert.Equal(expected, getValue()); |
216 | 212 | } |
| 213 | + |
| 214 | + readonly FakeMySqlServer m_server; |
| 215 | + readonly MySqlConnectionStringBuilder m_csb; |
| 216 | +} |
0 commit comments