Skip to content

Commit f781a76

Browse files
committed
Only acquire write while test starts
Signed-off-by: Xuanwo <github@xuanwo.io>
1 parent 5674e0b commit f781a76

File tree

3 files changed

+25
-41
lines changed

3 files changed

+25
-41
lines changed

src/common/base/src/base/singleton_instance.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Singleton {
4545
Some(name) => name,
4646
None => panic!("thread doesn't have name"),
4747
};
48-
let guard = c.read_recursive();
48+
let guard = c.read();
4949
let v: &T = guard
5050
.get(thread_name)
5151
.unwrap_or_else(|| panic!("thread {thread_name} is not initiated"))
@@ -65,15 +65,11 @@ impl Singleton {
6565
Some(name) => name,
6666
None => panic!("thread doesn't have name"),
6767
};
68-
let guard = c.upgradable_read();
69-
match guard.get(thread_name) {
70-
Some(c) => c.set(value),
71-
None => {
72-
let mut guard = parking_lot::RwLockUpgradableReadGuard::upgrade(guard);
73-
let c = guard.entry(thread_name.to_string()).or_default();
74-
c.set(value)
75-
}
76-
}
68+
let guard = c.read();
69+
guard
70+
.get(thread_name)
71+
.unwrap_or_else(|| panic!("thread {thread_name} is not initiated"))
72+
.set(value)
7773
}
7874
}
7975
}
@@ -109,8 +105,12 @@ impl GlobalInstance {
109105
///
110106
/// Should only be initiated once and only used in testing.
111107
#[cfg(debug_assertions)]
112-
pub fn init_testing() {
108+
pub fn init_testing(thread_name: &str) {
113109
let _ = GLOBAL.set(Singleton::Testing(parking_lot::RwLock::default()));
110+
if let Singleton::Testing(v) = GLOBAL.wait() {
111+
let mut guard = v.write();
112+
guard.insert(thread_name.to_string(), <Container![Send + Sync]>::new());
113+
}
114114
}
115115

116116
/// drop testing global data by thread name.

src/query/service/src/global_services.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,11 @@ pub struct GlobalServices;
3636

3737
impl GlobalServices {
3838
pub async fn init(config: Config) -> Result<()> {
39-
GlobalServices::init_with(config, true).await
39+
GlobalInstance::init_production();
40+
GlobalServices::init_with(config).await
4041
}
4142

42-
pub async fn init_with(config: Config, prodcution: bool) -> Result<()> {
43-
#[cfg(debug_assertions)]
44-
if prodcution {
45-
GlobalInstance::init_production();
46-
} else {
47-
GlobalInstance::init_testing();
48-
}
49-
#[cfg(not(debug_assertions))]
50-
if prodcution {
51-
GlobalInstance::init_production();
52-
} else {
53-
unreachable!("release build must init with production global")
54-
}
55-
43+
pub async fn init_with(config: Config) -> Result<()> {
5644
// The order of initialization is very important
5745
GlobalConfig::init(config.clone())?;
5846

src/query/service/tests/it/tests/sessions.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ impl TestGlobalServices {
3333
set_panic_hook();
3434
std::env::set_var("UNIT_TEST", "TRUE");
3535

36-
GlobalServices::init_with(config.clone(), false).await?;
36+
let thread_name = match std::thread::current().name() {
37+
None => panic!("thread name is none"),
38+
Some(thread_name) => thread_name.to_string(),
39+
};
40+
41+
GlobalInstance::init_testing(&thread_name);
42+
GlobalServices::init_with(config.clone()).await?;
3743

3844
// Cluster register.
3945
{
@@ -46,12 +52,9 @@ impl TestGlobalServices {
4652
);
4753
}
4854

49-
match std::thread::current().name() {
50-
None => panic!("thread name is none"),
51-
Some(thread_name) => Ok(TestGuard {
52-
thread_name: thread_name.to_string(),
53-
}),
54-
}
55+
Ok(TestGuard {
56+
thread_name: thread_name.to_string(),
57+
})
5558
}
5659
}
5760

@@ -61,11 +64,6 @@ pub struct TestGuard {
6164

6265
impl Drop for TestGuard {
6366
fn drop(&mut self) {
64-
debug!(
65-
"test {} is finished, starting dropping all resources",
66-
&self.thread_name
67-
);
68-
6967
// Check if session manager sill have active sessions.
7068
{
7169
let session_mgr = SessionManager::instance();
@@ -79,8 +77,6 @@ impl Drop for TestGuard {
7977
}
8078
}
8179

82-
GlobalInstance::drop_testing(&self.thread_name);
83-
84-
debug!("test {} resources have been dropped", &self.thread_name);
80+
// GlobalInstance::drop_testing(&self.thread_name);
8581
}
8682
}

0 commit comments

Comments
 (0)