Skip to content

Commit

Permalink
fix: Discards clean_test_sqlite3 since it breaks parallel tests and i…
Browse files Browse the repository at this point in the history
…ntroduces SqliteDatabaseCleaner (#154)

Co-authored-by: workflow user <workflow@example.com>
  • Loading branch information
laysakura and workflow user committed May 16, 2021
1 parent 789f5a0 commit 9f9ece2
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 64 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/correct-and-check-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ jobs:
- name: checks if client runs quickly
run: |
echo 'CREATE DATABASE abc' | ./target/debug/apllodb-client
rm *.sqlite3*
- name: cargo build --tests
uses: actions-rs/cargo@v1
Expand All @@ -150,6 +151,10 @@ jobs:
command: test
args: --all-features

- name: checks if tests clean .sqlite3* files (use SqliteDatabaseCleaner if fails)
run: |
[ $(find . -name '*.sqlite3*' |wc -l) = '0' ] || ( find . -name '*.sqlite3*' ; false )
check_rustdoc:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions apllodb-immutable-schema-engine-infra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

//! Infrastructure layer of apllodb-immutable-schema-engine.

#[macro_use]
extern crate derive_new;

mod engine;

mod access_methods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl SqliteDatabase {
&self.name
}

fn sqlite_db_path(db_name: &DatabaseName) -> PathBuf {
pub(crate) fn sqlite_db_path(db_name: &DatabaseName) -> PathBuf {
PathBuf::from(format!("immutable_schema_{}.sqlite3", db_name.as_str())) // FIXME: path from configuration
}

Expand Down
35 changes: 5 additions & 30 deletions apllodb-immutable-schema-engine-infra/src/test_support.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
use apllodb_test_support::setup::setup_test_logger;
use glob::glob;
use std::env;
pub mod sqlite_database_cleaner;
pub mod util;

pub use util::*;

use apllodb_shared_components::ApllodbResult;
use apllodb_test_support::setup::setup_test_logger;

/// general test setup sequence
pub fn test_setup() {
setup_test_logger();
clean_test_sqlite3().unwrap();
}

/// recursively rm all test sqlite3 files under PWD.
pub fn clean_test_sqlite3() -> ApllodbResult<()> {
let cd = env::current_dir()?;

log::debug!(
"clean_test_sqlite3(): searching .sqlite3* files under current dir: {}",
cd.display()
);

// TODO prevent removing non-test database files
for entry in glob("./**/*.sqlite3*").unwrap() {
if let Ok(path) = entry {
log::debug!(
"clean_test_sqlite3(): found {}. removing...",
path.display()
);
std::fs::remove_file(&path)?;
}
}

log::debug!("clean_test_sqlite3(): done",);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::sqlite::database::SqliteDatabase;
use apllodb_shared_components::DatabaseName;
use glob::glob;

/// Removes sqlite3 database file on drop().
///
/// Use this in "panic guard" pattern.
#[derive(Debug, new)]
pub struct SqliteDatabaseCleaner(DatabaseName);

impl Drop for SqliteDatabaseCleaner {
fn drop(&mut self) {
let sqlite3_path = SqliteDatabase::sqlite_db_path(&self.0);
let sqlite3_files_pattern = format!("{}*", sqlite3_path.as_os_str().to_str().unwrap());

for entry in glob(&sqlite3_files_pattern).unwrap() {
if let Ok(path) = entry {
log::debug!(
"SqliteDatabaseCleaner: found {}. removing...",
path.as_os_str().to_str().unwrap()
);

std::fs::remove_file(&path)
.or_else(|ioerr| match ioerr.kind() {
std::io::ErrorKind::NotFound => Ok(()),
_ => Err(ioerr),
})
.unwrap();

log::debug!("SqliteDatabaseCleaner: done");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::{StorageEngine, WithDbMethods, WithoutDbMethods};
use apllodb_shared_components::{
ApllodbResult, DatabaseName, Session, SessionWithDb, SessionWithTx, SessionWithoutDb,
};
use apllodb_storage_engine_interface::{StorageEngine, WithDbMethods, WithoutDbMethods};

use super::sqlite_database_cleaner::SqliteDatabaseCleaner;

pub async fn session_with_db<Engine: StorageEngine>(
engine: &Engine,
) -> ApllodbResult<SessionWithDb> {
let db_name = DatabaseName::random();
let _db_cleaner = SqliteDatabaseCleaner::new(db_name.clone());

let _ = engine
.without_db()
Expand Down
3 changes: 1 addition & 2 deletions apllodb-immutable-schema-engine/tests/immutable_ddl.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
mod test_support;

use apllodb_immutable_schema_engine::ApllodbImmutableSchemaEngine;
use apllodb_immutable_schema_engine_infra::test_support::test_setup;
use apllodb_immutable_schema_engine_infra::test_support::{session_with_tx, test_setup};
use apllodb_shared_components::{
AlterTableAction, ApllodbResult, ColumnConstraints, ColumnDataType, ColumnDefinition,
FieldIndex, NNSqlValue, SqlType, SqlValue, SqlValues, TableConstraintKind, TableConstraints,
TableName,
};
use apllodb_storage_engine_interface::test_support::session_with_tx;
use apllodb_storage_engine_interface::{ProjectionQuery, StorageEngine, WithTxMethods};

#[ctor::ctor]
Expand Down
3 changes: 1 addition & 2 deletions apllodb-immutable-schema-engine/tests/pk.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
mod test_support;

use apllodb_immutable_schema_engine::ApllodbImmutableSchemaEngine;
use apllodb_immutable_schema_engine_infra::test_support::test_setup;
use apllodb_immutable_schema_engine_infra::test_support::{session_with_tx, test_setup};
use apllodb_shared_components::{
ApllodbResult, ColumnConstraints, ColumnDataType, ColumnDefinition, FieldIndex, NNSqlValue,
SqlType, SqlValue, SqlValues, TableConstraintKind, TableConstraints, TableName,
};
use apllodb_storage_engine_interface::test_support::session_with_tx;
use apllodb_storage_engine_interface::{ProjectionQuery, StorageEngine, WithTxMethods};

#[ctor::ctor]
Expand Down
3 changes: 1 addition & 2 deletions apllodb-immutable-schema-engine/tests/standard_sql.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
mod test_support;

use apllodb_immutable_schema_engine::ApllodbImmutableSchemaEngine;
use apllodb_immutable_schema_engine_infra::test_support::test_setup;
use apllodb_immutable_schema_engine_infra::test_support::{session_with_tx, test_setup};
use apllodb_shared_components::{
ApllodbError, ApllodbErrorKind, ApllodbResult, ColumnConstraints, ColumnDataType,
ColumnDefinition, Expression, FieldIndex, FullFieldReference, NNSqlValue, RecordFieldRefSchema,
SqlType, SqlValue, SqlValues, TableConstraintKind, TableConstraints, TableName,
};
use apllodb_storage_engine_interface::test_support::session_with_tx;
use apllodb_storage_engine_interface::{ProjectionQuery, StorageEngine, WithTxMethods};

#[ctor::ctor]
Expand Down
7 changes: 5 additions & 2 deletions apllodb-immutable-schema-engine/tests/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod test_support;

use apllodb_immutable_schema_engine::ApllodbImmutableSchemaEngine;
use apllodb_immutable_schema_engine_infra::test_support::test_setup;
use apllodb_immutable_schema_engine_infra::test_support::{
sqlite_database_cleaner::SqliteDatabaseCleaner, test_setup,
};
use apllodb_shared_components::{
ApllodbError, ApllodbErrorKind, ApllodbResult, ColumnConstraints, ColumnDataType,
ColumnDefinition, DatabaseName, Session, SessionWithoutDb, SqlType, TableConstraintKind,
Expand All @@ -19,7 +21,8 @@ fn setup() {
#[async_std::test]
async fn test_wait_lock() -> ApllodbResult<()> {
let engine = ApllodbImmutableSchemaEngine::default();
let db = DatabaseName::new("test_wait_lock")?;
let db = DatabaseName::random();
let _db_cleaner = SqliteDatabaseCleaner::new(db.clone());

let _ = engine
.without_db()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod test_support;

use apllodb_immutable_schema_engine::ApllodbImmutableSchemaEngine;
use apllodb_immutable_schema_engine_infra::test_support::test_setup;
use apllodb_immutable_schema_engine_infra::test_support::{
sqlite_database_cleaner::SqliteDatabaseCleaner, test_setup,
};
use apllodb_shared_components::{
ApllodbResult, ColumnConstraints, ColumnDataType, ColumnDefinition, DatabaseName, Session,
SessionWithoutDb, SqlType, TableConstraintKind, TableConstraints, TableName,
Expand All @@ -19,7 +21,9 @@ fn setup() {
async fn test_use_apllodb_immutable_schema_engine() -> ApllodbResult<()> {
let engine = ApllodbImmutableSchemaEngine::default();

let db_name = DatabaseName::new("db")?;
let db_name = DatabaseName::random();
let _db_cleaner = SqliteDatabaseCleaner::new(db_name.clone());

let t_name = TableName::new("t")?;

let c1_def = ColumnDefinition::new(
Expand Down
2 changes: 0 additions & 2 deletions apllodb-server/src/test_support.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
pub mod fixture;

use apllodb_immutable_schema_engine_infra::test_support::clean_test_sqlite3;
use apllodb_test_support::setup::setup_test_logger;

/// general test setup sequence
pub fn test_setup() {
setup_test_logger();
clean_test_sqlite3().unwrap();
}
48 changes: 33 additions & 15 deletions apllodb-server/tests/database.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod sql_test;

use apllodb_immutable_schema_engine_infra::test_support::sqlite_database_cleaner::SqliteDatabaseCleaner;
use apllodb_server::test_support::test_setup;
use apllodb_shared_components::ApllodbErrorKind;
use apllodb_shared_components::{ApllodbErrorKind, ApllodbResult, DatabaseName};
use sql_test::{SessionAB, SqlTest, SqlTestSessionAB, Step, StepRes};

#[ctor::ctor]
Expand All @@ -11,13 +12,16 @@ fn setup() {

#[async_std::test]
async fn test_create_database() {
let db_name = DatabaseName::random();
let _db_cleaner = SqliteDatabaseCleaner::new(db_name.clone());

SqlTest::default()
.add_step(Step::new(
"CREATE DATABASE test_create_database",
format!("CREATE DATABASE {}", db_name.as_str()),
StepRes::Ok,
))
.add_step(Step::new(
"CREATE DATABASE test_create_database",
format!("CREATE DATABASE {}", db_name.as_str()),
StepRes::Err(ApllodbErrorKind::DuplicateDatabase),
))
.run_with_manual_db_control()
Expand All @@ -26,31 +30,40 @@ async fn test_create_database() {

#[async_std::test]
async fn test_use_database() {
let db_name = DatabaseName::random();
let _db_cleaner = SqliteDatabaseCleaner::new(db_name.clone());

SqlTest::default()
.add_step(Step::new(
"USE DATABASE test_use_database",
format!("USE DATABASE {}", db_name.as_str()),
StepRes::Err(ApllodbErrorKind::UndefinedObject),
))
.add_step(Step::new("CREATE DATABASE test_use_database", StepRes::Ok))
.add_step(Step::new("USE DATABASE test_use_database", StepRes::Ok))
.add_step(Step::new(
format!("CREATE DATABASE {}", db_name.as_str()),
StepRes::Ok,
))
.add_step(Step::new(
format!("USE DATABASE {}", db_name.as_str()),
StepRes::Ok,
))
.run_with_manual_db_control()
.await;
}

#[async_std::test]
async fn test_create_database_session_ab() {
let db_name = DatabaseName::random();
let _db_cleaner = SqliteDatabaseCleaner::new(db_name.clone());

SqlTestSessionAB::default()
.add_step(
SessionAB::A,
Step::new(
"CREATE DATABASE test_create_database_session_ab",
StepRes::Ok,
),
Step::new(format!("CREATE DATABASE {}", db_name.as_str()), StepRes::Ok),
)
.add_step(
SessionAB::B,
Step::new(
"CREATE DATABASE test_create_database_session_ab",
format!("CREATE DATABASE {}", db_name.as_str()),
StepRes::Err(ApllodbErrorKind::DuplicateDatabase),
),
)
Expand All @@ -59,20 +72,25 @@ async fn test_create_database_session_ab() {
}

#[async_std::test]
async fn test_use_database_session_ab() {
async fn test_use_database_session_ab() -> ApllodbResult<()> {
let db_name = DatabaseName::random();
let _db_cleaner = SqliteDatabaseCleaner::new(db_name.clone());

SqlTestSessionAB::default()
.add_step(
SessionAB::A,
Step::new("CREATE DATABASE test_use_database_session_ab", StepRes::Ok),
Step::new(format!("CREATE DATABASE {}", db_name.as_str()), StepRes::Ok),
)
.add_step(
SessionAB::B,
Step::new("USE DATABASE test_use_database_session_ab", StepRes::Ok),
Step::new(format!("USE DATABASE {}", db_name.as_str()), StepRes::Ok),
)
.add_step(
SessionAB::A,
Step::new("USE DATABASE test_use_database_session_ab", StepRes::Ok),
Step::new(format!("USE DATABASE {}", db_name.as_str()), StepRes::Ok),
)
.run_with_manual_db_control()
.await;

Ok(())
}
3 changes: 3 additions & 0 deletions apllodb-server/tests/sql_test/sql_test_session_ab.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use apllodb_immutable_schema_engine_infra::test_support::sqlite_database_cleaner::SqliteDatabaseCleaner;
use apllodb_server::ApllodbServer;
use apllodb_shared_components::{DatabaseName, Session, SessionWithoutDb};
use futures::FutureExt;
Expand Down Expand Up @@ -69,6 +70,8 @@ impl SqlTestSessionAB {
let database_name_a = DatabaseName::random();
let database_name_b = database_name_a.clone();

let _db_cleaner = SqliteDatabaseCleaner::new(database_name_a.clone());

let mut session_a_fut =
async move { Session::from(session_with_db(&server_a, database_name_a).await) }
.boxed_local();
Expand Down
7 changes: 5 additions & 2 deletions apllodb-server/tests/sql_test/sqltest.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use apllodb_immutable_schema_engine_infra::test_support::sqlite_database_cleaner::SqliteDatabaseCleaner;
use apllodb_server::ApllodbServer;
use apllodb_shared_components::{DatabaseName, Session, SessionWithoutDb};

Expand Down Expand Up @@ -28,8 +29,10 @@ impl SqlTest {

#[allow(dead_code)]
pub async fn run(self) {
let mut cur_session =
Session::from(session_with_db(&self.server, DatabaseName::random()).await);
let db_name = DatabaseName::random();
let _db_cleaner = SqliteDatabaseCleaner::new(db_name.clone());

let mut cur_session = Session::from(session_with_db(&self.server, db_name).await);
for step in &self.steps {
cur_session = step.run(&self.server, cur_session).await;
}
Expand Down
1 change: 1 addition & 0 deletions apllodb-sql-processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ env_logger = "0.8"
log = "0.4"

[dev-dependencies]
apllodb-immutable-schema-engine-infra = {path = "../apllodb-immutable-schema-engine-infra", features = ["test-support"]}
apllodb-shared-components = {path = "../apllodb-shared-components", features = ["test-support"]}
apllodb-storage-engine-interface = {path = "../apllodb-storage-engine-interface", features = ["test-support"]}
apllodb-test-support = {path = "../apllodb-test-support"}
Expand Down
3 changes: 2 additions & 1 deletion apllodb-sql-processor/src/local_test_support/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use crate::{
},
SQLProcessorContext,
};
use apllodb_immutable_schema_engine_infra::test_support::session_with_tx;
use apllodb_shared_components::{ApllodbError, ApllodbResult, Records};
use apllodb_sql_parser::apllodb_ast;
use apllodb_storage_engine_interface::{test_support::session_with_tx, MockStorageEngine};
use apllodb_storage_engine_interface::MockStorageEngine;
use std::sync::Arc;

impl QueryProcessor<MockStorageEngine> {
Expand Down

0 comments on commit 9f9ece2

Please sign in to comment.