Skip to content

Commit 7bcf28f

Browse files
committed
Fix test.
1 parent 8530a63 commit 7bcf28f

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

src/query/config/src/global.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static GLOBAL_CONFIG: OnceCell<Singleton<Arc<Config>>> = OnceCell::new();
2727
impl GlobalConfig {
2828
pub fn init(config: Config, v: Singleton<Arc<Config>>) -> Result<()> {
2929
v.init(Arc::new(config))?;
30-
GLOBAL_CONFIG.set(v.clone()).ok();
30+
GLOBAL_CONFIG.set(v).ok();
3131
Ok(())
3232
}
3333

src/query/config/tests/it/global.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::cell::UnsafeCell;
16+
use std::sync::Arc;
17+
18+
use common_base::base::SingletonImpl;
19+
use common_config::Config;
20+
use common_config::GlobalConfig;
21+
use common_exception::Result;
22+
use once_cell::sync::OnceCell;
23+
24+
struct ConfigSingleton {
25+
config: UnsafeCell<Option<Arc<Config>>>,
26+
}
27+
28+
unsafe impl Send for ConfigSingleton {}
29+
30+
unsafe impl Sync for ConfigSingleton {}
31+
32+
static GLOBAL: OnceCell<Arc<ConfigSingleton>> = OnceCell::new();
33+
34+
impl SingletonImpl<Arc<Config>> for ConfigSingleton {
35+
fn get(&self) -> Arc<Config> {
36+
unsafe {
37+
match &*self.config.get() {
38+
None => panic!("GlobalConfig is not init"),
39+
Some(config) => config.clone(),
40+
}
41+
}
42+
}
43+
44+
fn init(&self, value: Arc<Config>) -> Result<()> {
45+
unsafe {
46+
*(self.config.get() as *mut Option<Arc<Config>>) = Some(value);
47+
Ok(())
48+
}
49+
}
50+
}
51+
52+
#[test]
53+
fn test_global_config() -> Result<()> {
54+
let config_singleton = GLOBAL.get_or_init(|| {
55+
Arc::new(ConfigSingleton {
56+
config: UnsafeCell::new(None),
57+
})
58+
});
59+
60+
let mut config = Config::default();
61+
62+
GlobalConfig::init(config.clone(), config_singleton.clone())?;
63+
assert_eq!(GlobalConfig::instance().as_ref(), &config);
64+
65+
config.cmd = "test".to_string();
66+
67+
GlobalConfig::init(config.clone(), config_singleton.clone())?;
68+
assert_eq!(GlobalConfig::instance().as_ref(), &config);
69+
70+
Ok(())
71+
}

src/query/config/tests/it/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
mod global;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ impl TestGlobalServices {
135135
}
136136

137137
pub fn remove_services(&self, key: &str) {
138+
{
139+
let mut global_config_guard = self.query_config.lock();
140+
let global_config = global_config_guard.remove(key);
141+
drop(global_config_guard);
142+
drop(global_config);
143+
}
138144
{
139145
let mut global_runtime_guard = self.global_runtime.lock();
140146
let global_runtime = global_runtime_guard.remove(key);

0 commit comments

Comments
 (0)