Skip to content

Commit

Permalink
chore: refine the unit test framework (#13902)
Browse files Browse the repository at this point in the history
* chore: refine the unit test framework

* TestFixture new and destroy

* add Drop for TestFixture

* try change to let _fixture

* Drop session first

* do nothing for the teardown of TestFixture

* rename TestFixture create -> setup

* add singleton drop in TestFixture drop

* revert to TestGuard

* fix storage fs local path to default in system unit test

* global service InnerConfig -> &InnerConfig

* try to remove create random database in the TestFixture init

* create default database to some fuse engine unit test

* fix some unit test forgot to create default database

* fix test_last_snapshot_hintsome unit test
  • Loading branch information
BohuTANG authored Dec 3, 2023
1 parent 3111235 commit bf17bcd
Show file tree
Hide file tree
Showing 73 changed files with 837 additions and 731 deletions.
140 changes: 137 additions & 3 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions scripts/setup/dev_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ if [[ "$INSTALL_BUILD_TOOLS" == "true" ]]; then
cargo quickinstall cargo-binstall
cargo binstall -y sccache
cargo binstall -y cargo-zigbuild
cargo install cargo-nextest

fi

if [[ "$INSTALL_CHECK_TOOLS" == "true" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion src/bendpy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn databend(_py: Python, m: &PyModule) -> PyResult<()> {
MetaEmbedded::init_global_meta_store(meta_dir.to_string_lossy().to_string())
.await
.unwrap();
GlobalServices::init(conf.clone()).await.unwrap();
GlobalServices::init(&conf).await.unwrap();

// init oss license manager
OssLicenseManager::init(conf.query.tenant_id.clone()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/binaries/query/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub async fn init_services(conf: &InnerConfig) -> Result<()> {
));
}
// Make sure global services have been inited.
GlobalServices::init(conf.clone()).await
GlobalServices::init(conf).await
}

async fn precheck_services(conf: &InnerConfig) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/binaries/tool/table_meta_inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async fn parse_input_data(config: &InspectorConfig) -> Result<Vec<u8>> {
builder = builder.collect(from_file(Toml, config_file));
let read_config = builder.build()?;
let inner_config: InnerConfig = read_config.clone().try_into()?;
GlobalServices::init(inner_config).await?;
GlobalServices::init(&inner_config).await?;
let storage_config: StorageConfig = read_config.storage.try_into()?;
init_operator(&storage_config.params)?
}
Expand Down
4 changes: 2 additions & 2 deletions src/query/config/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::InnerConfig;
pub struct GlobalConfig;

impl GlobalConfig {
pub fn init(config: InnerConfig) -> Result<()> {
GlobalInstance::set(Arc::new(config));
pub fn init(config: &InnerConfig) -> Result<()> {
GlobalInstance::set(Arc::new(config.clone()));
Ok(())
}

Expand Down
11 changes: 4 additions & 7 deletions src/query/ee/src/test_kits/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ use common_meta_app::storage::StorageFsConfig;
use common_meta_app::storage::StorageParams;
use databend_query::test_kits::ConfigBuilder;
use databend_query::test_kits::Setup;
use databend_query::test_kits::TestGuard;
use jwt_simple::algorithms::ECDSAP256KeyPairLike;
use jwt_simple::prelude::Claims;
use jwt_simple::prelude::Duration;
use jwt_simple::prelude::ES256KeyPair;
use tempfile::TempDir;

use crate::test_kits::sessions::TestGlobalServices;
use crate::test_kits::setup::TestFixture;

fn build_custom_claims(license_type: String, org: String) -> LicenseInfo {
LicenseInfo {
Expand Down Expand Up @@ -74,10 +73,8 @@ impl Default for EESetup {

#[async_trait::async_trait]
impl Setup for EESetup {
async fn setup(&self) -> Result<(TestGuard, InnerConfig)> {
Ok((
TestGlobalServices::setup(&self.config, self.pk.clone()).await?,
self.config.clone(),
))
async fn setup(&self) -> Result<InnerConfig> {
TestFixture::setup(&self.config, self.pk.clone()).await?;
Ok(self.config.clone())
}
}
2 changes: 1 addition & 1 deletion src/query/ee/src/test_kits/mock_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::virtual_column::RealVirtualColumnHandler;
pub struct MockServices;
impl MockServices {
#[async_backtrace::framed]
pub async fn init(cfg: InnerConfig, public_key: String) -> Result<()> {
pub async fn init(cfg: &InnerConfig, public_key: String) -> Result<()> {
let rm = RealLicenseManager::new(cfg.query.tenant_id.clone(), public_key);
let wrapper = LicenseManagerWrapper {
manager: Box::new(rm),
Expand Down
2 changes: 1 addition & 1 deletion src/query/ee/src/test_kits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

pub mod context;
pub mod mock_services;
pub mod sessions;
pub mod setup;
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,25 @@ use common_config::InnerConfig;
use common_exception::Result;
use common_tracing::set_panic_hook;
use databend_query::clusters::ClusterDiscovery;
use databend_query::test_kits::TestGuard;
use databend_query::GlobalServices;
use log::info;

use crate::test_kits::mock_services::MockServices;

pub struct TestGlobalServices;
pub struct TestFixture;

unsafe impl Send for TestGlobalServices {}

unsafe impl Sync for TestGlobalServices {}

impl TestGlobalServices {
pub async fn setup(config: &InnerConfig, public_key: String) -> Result<TestGuard> {
impl TestFixture {
pub async fn setup(config: &InnerConfig, public_key: String) -> Result<()> {
set_panic_hook();
std::env::set_var("UNIT_TEST", "TRUE");

let thread_name = match std::thread::current().name() {
None => panic!("thread name is none"),
Some(thread_name) => thread_name.to_string(),
};
let thread_name = std::thread::current().name().unwrap().to_string();

#[cfg(debug_assertions)]
common_base::base::GlobalInstance::init_testing(&thread_name);

GlobalServices::init_with(config.clone()).await?;
MockServices::init(config.clone(), public_key).await?;
GlobalServices::init_with(config).await?;
MockServices::init(config, public_key).await?;

// Cluster register.
{
Expand All @@ -55,6 +47,13 @@ impl TestGlobalServices {
);
}

Ok(TestGuard::new(thread_name.to_string()))
Ok(())
}

/// Teardown the test environment.
pub async fn teardown() -> Result<()> {
// Nothing to do.

Ok(())
}
}
Loading

0 comments on commit bf17bcd

Please sign in to comment.