mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-20 23:10:37 +00:00
feat: Implements shutdown for GrpcServer and HttpServer (#372)
* fix: Fix TestGuard being dropped before grpc test starts * feat: Let start and shutdown takes immutable reference to self Also implement shutdown for GrpcServer * feat: Implement shutdown for HttpServer * style: Fix clippy * chore: Add name to AlreadyStarted error
This commit is contained in:
@@ -181,7 +181,7 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_create_expr_to_request() {
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts("create_expr_to_request");
|
||||
let instance = Instance::new(&opts).await.unwrap();
|
||||
instance.start().await.unwrap();
|
||||
|
||||
|
||||
@@ -15,37 +15,42 @@ use servers::grpc::GrpcServer;
|
||||
use servers::server::Server;
|
||||
|
||||
use crate::instance::Instance;
|
||||
use crate::tests::test_util;
|
||||
use crate::tests::test_util::{self, TestGuard};
|
||||
|
||||
async fn setup_grpc_server(port: usize) -> String {
|
||||
async fn setup_grpc_server(name: &str, port: usize) -> (String, TestGuard, Arc<GrpcServer>) {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
|
||||
let (mut opts, _guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (mut opts, guard) = test_util::create_tmp_dir_and_datanode_opts(name);
|
||||
let addr = format!("127.0.0.1:{}", port);
|
||||
opts.rpc_addr = addr.clone();
|
||||
let instance = Arc::new(Instance::new(&opts).await.unwrap());
|
||||
instance.start().await.unwrap();
|
||||
|
||||
let addr_cloned = addr.clone();
|
||||
let grpc_server = Arc::new(GrpcServer::new(instance.clone(), instance));
|
||||
|
||||
let grpc_server_clone = grpc_server.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut grpc_server = GrpcServer::new(instance.clone(), instance);
|
||||
let addr = addr_cloned.parse::<SocketAddr>().unwrap();
|
||||
grpc_server.start(addr).await.unwrap()
|
||||
grpc_server_clone.start(addr).await.unwrap()
|
||||
});
|
||||
|
||||
// wait for GRPC server to start
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
addr
|
||||
|
||||
(addr, guard, grpc_server)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_auto_create_table() {
|
||||
let addr = setup_grpc_server(3991).await;
|
||||
let (addr, _guard, grpc_server) = setup_grpc_server("auto_create_table", 3991).await;
|
||||
|
||||
let grpc_client = Client::connect(format!("http://{}", addr)).await.unwrap();
|
||||
let db = Database::new("greptime", grpc_client);
|
||||
|
||||
insert_and_assert(&db).await;
|
||||
|
||||
grpc_server.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
fn expect_data() -> (Column, Column, Column, Column) {
|
||||
@@ -104,7 +109,7 @@ fn expect_data() -> (Column, Column, Column, Column) {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_insert_and_select() {
|
||||
let addr = setup_grpc_server(3990).await;
|
||||
let (addr, _guard, grpc_server) = setup_grpc_server("insert_and_select", 3990).await;
|
||||
|
||||
let grpc_client = Client::connect(format!("http://{}", addr)).await.unwrap();
|
||||
|
||||
@@ -143,6 +148,8 @@ async fn test_insert_and_select() {
|
||||
|
||||
// insert
|
||||
insert_and_assert(&db).await;
|
||||
|
||||
grpc_server.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
async fn insert_and_assert(db: &Database) {
|
||||
|
||||
@@ -13,8 +13,8 @@ use test_util::TestGuard;
|
||||
use crate::instance::Instance;
|
||||
use crate::tests::test_util;
|
||||
|
||||
async fn make_test_app() -> (Router, TestGuard) {
|
||||
let (opts, guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
async fn make_test_app(name: &str) -> (Router, TestGuard) {
|
||||
let (opts, guard) = test_util::create_tmp_dir_and_datanode_opts(name);
|
||||
let instance = Arc::new(Instance::new(&opts).await.unwrap());
|
||||
instance.start().await.unwrap();
|
||||
test_util::create_test_table(&instance, ConcreteDataType::timestamp_millis_datatype())
|
||||
@@ -27,7 +27,7 @@ async fn make_test_app() -> (Router, TestGuard) {
|
||||
#[tokio::test]
|
||||
async fn test_sql_api() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
let (app, _guard) = make_test_app().await;
|
||||
let (app, _guard) = make_test_app("sql_api").await;
|
||||
let client = TestClient::new(app);
|
||||
let res = client.get("/v1/sql").send().await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
@@ -88,7 +88,7 @@ async fn test_sql_api() {
|
||||
async fn test_metrics_api() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
common_telemetry::init_default_metrics_recorder();
|
||||
let (app, _guard) = make_test_app().await;
|
||||
let (app, _guard) = make_test_app("metrics_api").await;
|
||||
let client = TestClient::new(app);
|
||||
|
||||
// Send a sql
|
||||
@@ -108,7 +108,7 @@ async fn test_metrics_api() {
|
||||
#[tokio::test]
|
||||
async fn test_scripts_api() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
let (app, _guard) = make_test_app().await;
|
||||
let (app, _guard) = make_test_app("scripts_api").await;
|
||||
let client = TestClient::new(app);
|
||||
let res = client
|
||||
.post("/v1/scripts")
|
||||
@@ -140,10 +140,10 @@ def test(n):
|
||||
}
|
||||
|
||||
async fn start_test_app(addr: &str) -> (SocketAddr, TestGuard) {
|
||||
let (opts, guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (opts, guard) = test_util::create_tmp_dir_and_datanode_opts("py_side_scripts_api");
|
||||
let instance = Arc::new(Instance::new(&opts).await.unwrap());
|
||||
instance.start().await.unwrap();
|
||||
let mut http_server = HttpServer::new(instance);
|
||||
let http_server = HttpServer::new(instance);
|
||||
(
|
||||
http_server.start(addr.parse().unwrap()).await.unwrap(),
|
||||
guard,
|
||||
|
||||
@@ -13,7 +13,7 @@ use crate::tests::test_util;
|
||||
async fn test_execute_insert() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts("execute_insert");
|
||||
let instance = Instance::new(&opts).await.unwrap();
|
||||
instance.start().await.unwrap();
|
||||
|
||||
@@ -37,7 +37,7 @@ async fn test_execute_insert() {
|
||||
async fn test_execute_insert_query_with_i64_timestamp() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts("insert_query_i64_timestamp");
|
||||
let instance = Instance::new(&opts).await.unwrap();
|
||||
instance.start().await.unwrap();
|
||||
|
||||
@@ -74,7 +74,7 @@ async fn test_execute_insert_query_with_i64_timestamp() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_execute_query() {
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts("execute_query");
|
||||
let instance = Instance::new(&opts).await.unwrap();
|
||||
instance.start().await.unwrap();
|
||||
|
||||
@@ -100,7 +100,8 @@ async fn test_execute_query() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_execute_show_databases_tables() {
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (opts, _guard) =
|
||||
test_util::create_tmp_dir_and_datanode_opts("execute_show_databases_tables");
|
||||
let instance = Instance::new(&opts).await.unwrap();
|
||||
instance.start().await.unwrap();
|
||||
|
||||
@@ -191,7 +192,7 @@ async fn test_execute_show_databases_tables() {
|
||||
pub async fn test_execute_create() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts("execute_create");
|
||||
let instance = Instance::new(&opts).await.unwrap();
|
||||
instance.start().await.unwrap();
|
||||
|
||||
@@ -215,7 +216,8 @@ pub async fn test_execute_create() {
|
||||
pub async fn test_create_table_illegal_timestamp_type() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
|
||||
let (opts, _guard) = test_util::create_tmp_dir_and_datanode_opts();
|
||||
let (opts, _guard) =
|
||||
test_util::create_tmp_dir_and_datanode_opts("create_table_illegal_timestamp_type");
|
||||
let instance = Instance::new(&opts).await.unwrap();
|
||||
instance.start().await.unwrap();
|
||||
|
||||
|
||||
@@ -24,9 +24,9 @@ pub struct TestGuard {
|
||||
_data_tmp_dir: TempDir,
|
||||
}
|
||||
|
||||
pub fn create_tmp_dir_and_datanode_opts() -> (DatanodeOptions, TestGuard) {
|
||||
let wal_tmp_dir = TempDir::new("/tmp/greptimedb_test_wal").unwrap();
|
||||
let data_tmp_dir = TempDir::new("/tmp/greptimedb_test_data").unwrap();
|
||||
pub fn create_tmp_dir_and_datanode_opts(name: &str) -> (DatanodeOptions, TestGuard) {
|
||||
let wal_tmp_dir = TempDir::new(&format!("gt_wal_{}", name)).unwrap();
|
||||
let data_tmp_dir = TempDir::new(&format!("gt_data_{}", name)).unwrap();
|
||||
let opts = DatanodeOptions {
|
||||
wal_dir: wal_tmp_dir.path().to_str().unwrap().to_string(),
|
||||
storage: ObjectStoreConfig::File {
|
||||
|
||||
Reference in New Issue
Block a user