Exemplo n.º 1
0
        public void Open_decrypts_lazily_when_no_password()
        {
            try
            {
                using var connection1 = new SqliteConnection("Data Source=encrypted2.db;Password=password");
                connection1.Open();

                // NB: The file is only encrypted after writing
                connection1.ExecuteNonQuery(
                    "CREATE TABLE IF NOT EXISTS data (value); INSERT INTO data (value) VALUES (1);");

                using var connection2 = new SqliteConnection("Data Source=encrypted2.db");
                connection2.Open();
                connection2.ExecuteNonQuery("PRAGMA key = 'password';");

                var value = connection2.ExecuteScalar <long>("SELECT value FROM data;");

                Assert.Equal(1L, value);
            }
            finally
            {
                SqliteConnection.ClearPool(new SqliteConnection("Data Source=encrypted2.db;Password=password"));
                SqliteConnection.ClearPool(new SqliteConnection("Data Source=encrypted2.db"));
                File.Delete("encrypted2.db");
            }
        }
Exemplo n.º 2
0
        public void Clear_works_when_connection_leaked()
        {
            using var connection = new SqliteConnection(ConnectionString);
            connection.Open();

            LeakConnection();
            GC.Collect();

            SqliteConnection.ClearPool(connection);
Exemplo n.º 3
0
        public async Task ExecuteReader_retries_when_busy()
        {
            const string connectionString = "Data Source=busy.db";

            var selectedSignal = new AutoResetEvent(initialState: false);

            try
            {
                await Task.WhenAll(
                    Task.Run(
                        async() =>
                {
                    using (var connection = new SqliteConnection(connectionString))
                    {
                        connection.Open();

                        connection.ExecuteNonQuery(
                            "CREATE TABLE Data (Value); INSERT INTO Data VALUES (0);");

                        using (connection.ExecuteReader("SELECT * FROM Data;"))
                        {
                            selectedSignal.Set();

                            await Task.Delay(1000);
                        }
                    }
                }),
                    Task.Run(
                        () =>
                {
                    using (var connection = new SqliteConnection(connectionString))
                    {
                        connection.Open();

                        selectedSignal.WaitOne();

                        var command         = connection.CreateCommand();
                        command.CommandText = "DROP TABLE Data;";

                        command.ExecuteNonQuery();
                    }
                }));
            }
            finally
            {
                SqliteConnection.ClearPool(new SqliteConnection(connectionString));
                File.Delete("busy.db");
            }
        }
Exemplo n.º 4
0
        public void Internal_connections_are_not_reused_after_clearing_pool(bool allPools)
        {
            using var connection = new SqliteConnection(ConnectionString);

            connection.Open();
            var db = connection.Handle;

            connection.Close();

            if (allPools)
            {
                SqliteConnection.ClearAllPools();
            }
            else
            {
                SqliteConnection.ClearPool(connection);
            }

            connection.Open();

            Assert.NotSame(db, connection.Handle);
        }