fix: stop background task when LogFile is dropped (#440)

This commit is contained in:
Lei, Huang
2022-11-10 11:33:33 +08:00
committed by GitHub
parent 056d7cb911
commit c3776ddd18

View File

@@ -113,6 +113,13 @@ pub struct LogFile {
append_buffer_size: usize,
}
impl Drop for LogFile {
fn drop(&mut self) {
self.state.stopped.store(true, Ordering::Relaxed);
info!("Stopping log file {}", self.name);
}
}
impl LogFile {
/// Opens a file in path with given log config.
pub async fn open(path: impl Into<String>, config: &LogConfig) -> Result<Self> {
@@ -826,4 +833,24 @@ mod tests {
.collect::<Vec<_>>()
);
}
#[tokio::test]
async fn test_shutdown() {
logging::init_default_ut_logging();
let config = LogConfig::default();
let dir = TempDir::new("greptimedb-store-test").unwrap();
let path_buf = dir.path().join("0010.log");
let path = path_buf.to_str().unwrap().to_string();
File::create(path.as_str()).unwrap();
let mut file = LogFile::open(path.clone(), &config)
.await
.unwrap_or_else(|_| panic!("Failed to open file: {}", path));
let state = file.state.clone();
file.start().await.unwrap();
drop(file);
assert!(state.stopped.load(Ordering::Relaxed));
}
}