|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using MySql.Data.MySqlClient; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace MySqlConnector.Tests |
| 9 | +{ |
| 10 | + public class ConnectionTests : IDisposable |
| 11 | + { |
| 12 | + public ConnectionTests() |
| 13 | + { |
| 14 | + m_server = new FakeMySqlServer(); |
| 15 | + m_server.Start(); |
| 16 | + |
| 17 | + m_csb = new MySqlConnectionStringBuilder |
| 18 | + { |
| 19 | + Server = "localhost", |
| 20 | + Port = (uint) m_server.Port, |
| 21 | + }; |
| 22 | + } |
| 23 | + |
| 24 | + public void Dispose() |
| 25 | + { |
| 26 | + m_server.Stop(); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void PooledConnectionIsReturnedToPool() |
| 31 | + { |
| 32 | + Assert.Equal(0, m_server.ActiveConnections); |
| 33 | + |
| 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); |
| 39 | + |
| 40 | + Assert.Equal(m_server.ServerVersion, connection.ServerVersion); |
| 41 | + connection.Close(); |
| 42 | + Assert.Equal(1, m_server.ActiveConnections); |
| 43 | + } |
| 44 | + |
| 45 | + Assert.Equal(1, m_server.ActiveConnections); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public async Task UnpooledConnectionIsClosed() |
| 50 | + { |
| 51 | + Assert.Equal(0, m_server.ActiveConnections); |
| 52 | + |
| 53 | + m_csb.Pooling = false; |
| 54 | + using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
| 55 | + { |
| 56 | + await connection.OpenAsync(); |
| 57 | + Assert.Equal(1, m_server.ActiveConnections); |
| 58 | + |
| 59 | + Assert.Equal(m_server.ServerVersion, connection.ServerVersion); |
| 60 | + connection.Close(); |
| 61 | + |
| 62 | + await WaitForConditionAsync(0, () => m_server.ActiveConnections); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + [Theory] |
| 67 | + [InlineData(2u, 3u, true)] |
| 68 | + [InlineData(180u, 3u, false)] |
| 69 | + public async Task ConnectionLifeTime(uint lifeTime, uint delaySeconds, bool shouldTimeout) |
| 70 | + { |
| 71 | + m_csb.Pooling = true; |
| 72 | + m_csb.MinimumPoolSize = 0; |
| 73 | + m_csb.MaximumPoolSize = 1; |
| 74 | + m_csb.ConnectionLifeTime = lifeTime; |
| 75 | + int serverThread; |
| 76 | + |
| 77 | + using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
| 78 | + { |
| 79 | + await connection.OpenAsync(); |
| 80 | + serverThread = connection.ServerThread; |
| 81 | + await Task.Delay(TimeSpan.FromSeconds(delaySeconds)); |
| 82 | + } |
| 83 | + |
| 84 | + using (var connection = new MySqlConnection(m_csb.ConnectionString)) |
| 85 | + { |
| 86 | + await connection.OpenAsync(); |
| 87 | + if (shouldTimeout) |
| 88 | + Assert.NotEqual(serverThread, connection.ServerThread); |
| 89 | + else |
| 90 | + Assert.Equal(serverThread, connection.ServerThread); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void LeakReaders() |
| 96 | + { |
| 97 | + m_csb.Pooling = true; |
| 98 | + m_csb.MinimumPoolSize = 0; |
| 99 | + m_csb.MaximumPoolSize = 6; |
| 100 | + m_csb.ConnectionTimeout = 30u; |
| 101 | + |
| 102 | + for (var i = 0; i < m_csb.MaximumPoolSize + 2; i++) |
| 103 | + { |
| 104 | + var connection = new MySqlConnection(m_csb.ConnectionString); |
| 105 | + connection.Open(); |
| 106 | + |
| 107 | + var cmd = connection.CreateCommand(); |
| 108 | + cmd.CommandText = "SELECT 1;"; |
| 109 | + var reader = cmd.ExecuteReader(); |
| 110 | + Assert.True(reader.Read()); |
| 111 | + |
| 112 | + // have to GC for leaked connections to be removed from the pool |
| 113 | + GC.Collect(); |
| 114 | + |
| 115 | + // HACK: have to sleep (so that RecoverLeakedSessions is called in ConnectionPool.GetSessionAsync) |
| 116 | + Thread.Sleep(250); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static async Task WaitForConditionAsync<T>(T expected, Func<T> getValue) |
| 121 | + { |
| 122 | + var sw = Stopwatch.StartNew(); |
| 123 | + while (sw.ElapsedMilliseconds < 1000 && !expected.Equals(getValue())) |
| 124 | + await Task.Delay(50); |
| 125 | + Assert.Equal(expected, getValue()); |
| 126 | + } |
| 127 | + |
| 128 | + readonly FakeMySqlServer m_server; |
| 129 | + readonly MySqlConnectionStringBuilder m_csb; |
| 130 | + } |
| 131 | +} |
0 commit comments