예제 #1
0
        public void QueryBinaryGuid(bool oldGuids)
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.OldGuids = oldGuids;
            csb.Database = "datatypes";
            using (var connection = new MySqlConnection(csb.ConnectionString))
            {
                connection.Open();
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"select guidbin from blobs order by rowid;";
                    using (var reader = cmd.ExecuteReader())
                    {
                        Assert.True(reader.Read());
                        Assert.Equal(DBNull.Value, reader.GetValue(0));
                        Assert.True(reader.Read());
                        if (oldGuids)
                        {
                            var expected = new Guid(0x33221100, 0x5544, 0x7766, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF);
                            Assert.Equal(expected, reader.GetValue(0));
                            Assert.Equal(expected, reader.GetGuid(0));
                        }
                        else
                        {
                            var expected = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
                            Assert.Equal(expected, GetBytes(reader));
                            Assert.Equal(expected, reader.GetValue(0));
                        }
                        Assert.False(reader.Read());
                    }

                    cmd.CommandText = @"select guidbin from strings order by rowid;";
                    using (var reader = cmd.ExecuteReader())
                    {
                        Assert.True(reader.Read());
                        Assert.Equal(DBNull.Value, reader.GetValue(0));
                        Assert.True(reader.Read());
                        if (oldGuids)
                        {
                            Assert.Equal("00000000-0000-0000-0000-000000000000", reader.GetValue(0));
                            Assert.Equal("00000000-0000-0000-0000-000000000000", reader.GetString(0));
                        }
                        else
                        {
                            Assert.Equal(Guid.Empty, reader.GetValue(0));
                            Assert.Equal(Guid.Empty, reader.GetGuid(0));
                        }
                    }
                }
            }
        }
예제 #2
0
        public void WithUserVariables()
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.AllowUserVariables = true;
            using (var connection = new MySqlConnection(csb.ConnectionString))
            {
                connection.Open();
                var cmd = connection.CreateCommand();
                cmd.CommandText = "set @var = 1; select @var + 1;";
                Assert.Equal(2L, cmd.ExecuteScalar());
            }
        }
예제 #3
0
        public void WithoutUserVariables()
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.AllowUserVariables = false;
            using (var connection = new MySqlConnection(csb.ConnectionString))
            {
                connection.Open();
                var cmd = connection.CreateCommand();
                cmd.CommandText = "set @var = 1; select @var + 1;";
                Assert.Throws <MySqlException>(() => cmd.ExecuteScalar());
            }
        }
예제 #4
0
        public async Task QueryWithGuidParameter(bool oldGuids)
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.OldGuids = oldGuids;
            csb.Database = "datatypes";
            using (var connection = new MySqlConnection(csb.ConnectionString))
            {
                await connection.OpenAsync().ConfigureAwait(false);

                Assert.Equal(oldGuids? 0L : 1L, (await connection.QueryAsync <long>(@"select count(*) from strings where guid = @guid", new { guid = new Guid("fd24a0e8-c3f2-4821-a456-35da2dc4bb8f") }).ConfigureAwait(false)).SingleOrDefault());
                Assert.Equal(oldGuids ? 0L : 1L, (await connection.QueryAsync <long>(@"select count(*) from strings where guidbin = @guid", new { guid = new Guid("fd24a0e8-c3f2-4821-a456-35da2dc4bb8f") }).ConfigureAwait(false)).SingleOrDefault());
                Assert.Equal(oldGuids ? 1L : 0L, (await connection.QueryAsync <long>(@"select count(*) from blobs where guidbin = @guid", new { guid = new Guid(0x33221100, 0x5544, 0x7766, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF) }).ConfigureAwait(false)).SingleOrDefault());
            }
        }
예제 #5
0
        public async Task CharacterSet()
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.Pooling         = true;
            csb.MinimumPoolSize = 0;
            csb.MaximumPoolSize = 21;             // use a uniqe pool size to create a unique connection string to force a unique pool to be created
#if BASELINE
            csb.CharacterSet = "utf8mb4";
#endif

            // verify that connection charset is the same when retrieving a connection from the pool
            await CheckCharacterSetAsync(csb.ConnectionString).ConfigureAwait(false);
            await CheckCharacterSetAsync(csb.ConnectionString).ConfigureAwait(false);
            await CheckCharacterSetAsync(csb.ConnectionString).ConfigureAwait(false);
        }
예제 #6
0
        public async Task ExhaustConnectionPool()
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.Pooling           = true;
            csb.MinimumPoolSize   = 0;
            csb.MaximumPoolSize   = 3;
            csb.ConnectionTimeout = 60;

            var connections = new List <MySqlConnection>();

            for (int i = 0; i < csb.MaximumPoolSize; i++)
            {
                var connection = new MySqlConnection(csb.ConnectionString);
                await connection.OpenAsync().ConfigureAwait(false);

                connections.Add(connection);
            }

            var closeTask = Task.Run(() =>
            {
                Thread.Sleep(5000);
                connections[0].Dispose();
                connections.RemoveAt(0);
            });

            using (var extraConnection = new MySqlConnection(csb.ConnectionString))
            {
                var stopwatch = Stopwatch.StartNew();
                await extraConnection.OpenAsync().ConfigureAwait(false);

                stopwatch.Stop();
                Assert.InRange(stopwatch.ElapsedMilliseconds, 4500, 7500);
            }

            closeTask.Wait();

            foreach (var connection in connections)
            {
                connection.Dispose();
            }
        }
예제 #7
0
        public void PersistSecurityInfo(bool persistSecurityInfo)
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.PersistSecurityInfo = persistSecurityInfo;
            var connectionStringWithoutPassword = csb.ConnectionString.Replace("Password", "password").Replace(";password='******'", "");

            using (var connection = new MySqlConnection(csb.ConnectionString))
            {
                Assert.Equal(csb.ConnectionString, connection.ConnectionString);
                connection.Open();
                if (persistSecurityInfo)
                {
                    Assert.Equal(csb.ConnectionString, connection.ConnectionString);
                }
                else
                {
                    Assert.Equal(connectionStringWithoutPassword, connection.ConnectionString);
                }
            }
        }
예제 #8
0
        public void QueryZeroDateTime(bool convertZeroDateTime)
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.ConvertZeroDateTime = convertZeroDateTime;
            csb.Database            = "datatypes";
            using (var connection = new MySqlConnection(csb.ConnectionString))
            {
                connection.Open();
                using (var cmd = connection.CreateCommand())
                {
                    cmd.CommandText = @"select `Date`, `DateTime`, `Timestamp` from times where `Date` = 0;";
                    using (var reader = cmd.ExecuteReader())
                    {
                        Assert.True(reader.Read());
                        if (convertZeroDateTime)
                        {
                            Assert.Equal(DateTime.MinValue, reader.GetDateTime(0));
                            Assert.Equal(DateTime.MinValue, reader.GetDateTime(1));
                            Assert.Equal(DateTime.MinValue, reader.GetDateTime(2));
                        }
                        else
                        {
#if BASELINE
                            Assert.Throws <MySql.Data.Types.MySqlConversionException>(() => reader.GetDateTime(0));
                            Assert.Throws <MySql.Data.Types.MySqlConversionException>(() => reader.GetDateTime(1));
                            Assert.Throws <MySql.Data.Types.MySqlConversionException>(() => reader.GetDateTime(2));
#else
                            Assert.Throws <InvalidCastException>(() => reader.GetDateTime(0));
                            Assert.Throws <InvalidCastException>(() => reader.GetDateTime(1));
                            Assert.Throws <InvalidCastException>(() => reader.GetDateTime(2));
#endif
                        }
                    }
                }
            }
        }
예제 #9
0
 public DatabaseFixture()
 {
     Connection = new MySqlConnection(Constants.CreateConnectionStringBuilder().ConnectionString);
 }
예제 #10
0
        public async Task ClearConnectionPool()
        {
            var csb = Constants.CreateConnectionStringBuilder();

            csb.Pooling         = true;
            csb.MinimumPoolSize = 0;
            csb.MaximumPoolSize = 3;

            var connections = new List <MySqlConnection>();

            for (int i = 0; i < csb.MaximumPoolSize; i++)
            {
                var connection = new MySqlConnection(csb.ConnectionString);
                connections.Add(connection);
            }

            Func <HashSet <long> > getConnectionIds = () =>
            {
                var cids = GetConnectionIds(connections);
                Assert.Equal(connections.Count, cids.Count);
                return(cids);
            };

            Func <Task> openConnections = async() =>
            {
                foreach (var connection in connections)
                {
                    await connection.OpenAsync();
                }
            };

            Action closeConnections = () =>
            {
                foreach (var connection in connections)
                {
                    connection.Close();
                }
            };

            // connections should all be disposed when returned to pool
            await openConnections();

            var connectionIds = getConnectionIds();

            await ClearPoolAsync(connections[0]);

            closeConnections();
            await openConnections();

            var connectionIds2 = getConnectionIds();

            Assert.Empty(connectionIds.Intersect(connectionIds2));
            closeConnections();

            // connections should all be disposed in ClearPoolAsync
            await ClearPoolAsync(connections[0]);
            await openConnections();

            var connectionIds3 = getConnectionIds();

            Assert.Empty(connectionIds2.Intersect(connectionIds3));
            closeConnections();

            // some connections may be disposed in ClearPoolAsync, others in OpenAsync
            var clearTask = ClearPoolAsync(connections[0]);

            await openConnections();

            var connectionIds4 = GetConnectionIds(connections);

            Assert.Empty(connectionIds3.Intersect(connectionIds4));
            await clearTask;

            closeConnections();

            foreach (var connection in connections)
            {
                connection.Dispose();
            }
        }