Skip to content

Commit

Permalink
release: v0.6.3 (#84)
Browse files Browse the repository at this point in the history
* &str -> String to avoid lifetime issue

Signed-off-by: Runji Wang <wangrunji0408@163.com>

* release v0.6.3

Signed-off-by: Runji Wang <wangrunji0408@163.com>

* move hook functions to a new trait

Signed-off-by: Runji Wang <wangrunji0408@163.com>

Signed-off-by: Runji Wang <wangrunji0408@163.com>
  • Loading branch information
wangrunji0408 authored Aug 24, 2022
1 parent ba5a757 commit 85d5493
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.6.3] - 2022-08-24

- Support registering hook function after each query.

## [0.6.2] - 2022-08-22

- Support load balancing of multiple addr.
Expand Down
2 changes: 1 addition & 1 deletion sqllogictest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqllogictest"
version = "0.6.2"
version = "0.6.3"
edition = "2021"
homepage = "https://github.com/risinglightdb/sqllogictest-rs"
keywords = ["sql", "database", "parser", "cli"]
Expand Down
49 changes: 20 additions & 29 deletions sqllogictest/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;
use std::vec;

use async_trait::async_trait;
use futures::{executor::block_on, future::BoxFuture, stream, Future, FutureExt, StreamExt};
use futures::{executor::block_on, stream, Future, StreamExt};
use itertools::Itertools;
use tempfile::{tempdir, TempDir};

Expand Down Expand Up @@ -171,21 +171,26 @@ impl TestErrorKind {
/// By default, we will use `|x, y| x == y`.
pub type Validator = fn(&Vec<String>, &Vec<String>) -> bool;

/// A collection of hook functions.
#[async_trait]
pub trait Hook: Send {
/// Called after each statement completes.
async fn on_stmt_complete(&mut self, _sql: &str) {}

/// Called after each query completes.
async fn on_query_complete(&mut self, _sql: &str) {}
}

/// Sqllogictest runner.
pub struct Runner<D: AsyncDB> {
db: D,
// validator is used for validate if the result of query equals to expected.
validator: Validator,
testdir: Option<TempDir>,
sort_mode: Option<SortMode>,
/// An async function executed after each statement completes.
on_stmt_complete: Option<HookFn>,
/// An async function executed after each query completes.
on_query_complete: Option<HookFn>,
hook: Option<Box<dyn Hook>>,
}

type HookFn = Box<dyn FnMut(&str) -> BoxFuture<'static, ()> + Send>;

impl<D: AsyncDB> Runner<D> {
/// Create a new test runner on the database.
pub fn new(db: D) -> Self {
Expand All @@ -194,8 +199,7 @@ impl<D: AsyncDB> Runner<D> {
validator: |x, y| x == y,
testdir: None,
sort_mode: None,
on_stmt_complete: None,
on_query_complete: None,
hook: None,
}
}

Expand Down Expand Up @@ -248,8 +252,8 @@ impl<D: AsyncDB> Runner<D> {
}
_ => {}
}
if let Some(f) = &mut self.on_stmt_complete {
f(&sql).await;
if let Some(hook) = &mut self.hook {
hook.on_stmt_complete(&sql).await;
}
}
Record::Query { conditions, .. } if self.should_skip(&conditions) => {}
Expand Down Expand Up @@ -289,8 +293,8 @@ impl<D: AsyncDB> Runner<D> {
}
.at(loc));
}
if let Some(f) = &mut self.on_query_complete {
f(&sql).await;
if let Some(hook) = &mut self.hook {
hook.on_query_complete(&sql).await;
}
}
Record::Sleep { duration, .. } => D::sleep(duration).await,
Expand Down Expand Up @@ -453,22 +457,9 @@ impl<D: AsyncDB> Runner<D> {
.any(|c| c.should_skip(self.db.engine_name()))
}

/// Executes async function `f` after each query completes.
pub fn set_on_query_complete<F, Fut>(&mut self, mut f: F)
where
F: FnMut(&str) -> Fut + Send + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
self.on_query_complete = Some(Box::new(move |sql| f(sql).boxed()));
}

/// Executes async function `f` after each statement completes.
pub fn set_on_stmt_complete<F, Fut>(&mut self, mut f: F)
where
F: FnMut(&str) -> Fut + Send + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
self.on_stmt_complete = Some(Box::new(move |sql| f(sql).boxed()));
/// Set hook functions.
pub fn set_hook(&mut self, hook: impl Hook + 'static) {
self.hook = Some(Box::new(hook));
}
}

Expand Down

0 comments on commit 85d5493

Please sign in to comment.