Skip to content

Commit 923e65a

Browse files
committed
test: run kvsd in test
1 parent 370c594 commit 923e65a

File tree

7 files changed

+138
-24
lines changed

7 files changed

+138
-24
lines changed

Cargo.lock

+72-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+11-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ readme = "README"
1313
repository = "https://github.com/ymgyt/syndicationd"
1414

1515
[workspace.dependencies]
16-
anyhow = { version = "1" }
17-
async-trait = { version = "0.1.77" }
18-
axum = { version = "0.7.4" }
19-
chrono = { version = "0.4.31" }
20-
clap = { version = "4.4" }
21-
feed-rs = { version = "1.4" }
22-
futures-util = { version = "0.3.30" }
23-
graphql_client = { version = "0.13.0", default-features = false }
24-
http = { version = "0.2" } # request use 0.2
16+
anyhow = { version = "1" }
17+
async-trait = { version = "0.1.77" }
18+
axum = { version = "0.7.4" }
19+
chrono = { version = "0.4.31" }
20+
clap = { version = "4.4" }
21+
feed-rs = { version = "1.4" }
22+
futures-util = { version = "0.3.30" }
23+
graphql_client = { version = "0.13.0", default-features = false }
24+
http = { version = "0.2" } # request use 0.2
25+
kvsd = { version = "0.1.2", default-features = false }
26+
# kvsd = { path = "../kvsd" }
2527
moka = { version = "0.12.4", features = ["future"] }
2628
opentelemetry = { version = "0.21.0" }
2729
opentelemetry-http = { version = "0.10.0" }

crates/synd_term/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ integration = []
5252
synd_api = { path = "../synd_api" }
5353
synd_test = { path = "../synd_test" }
5454

55+
kvsd = { workspace = true }
5556
serial_test = { version = "3.0.0", default_features = false, features = ["async", "file_locks"] }
57+
tempdir = "0.3.7"
5658
tokio-stream = "0.1.14"
5759

5860
[lints]

crates/synd_term/tests/integration.rs

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ mod test {
3333

3434
tracing::info!("TEST hello_world run");
3535

36+
let _kvsd_client = helper::run_kvsd().await?;
37+
3638
let mock_port = 6000;
3739
let oauth_addr = ("127.0.0.1", mock_port);
3840
let oauth_listener = TcpListener::bind(oauth_addr).await?;

crates/synd_term/tests/test/helper.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
use std::future::pending;
2+
3+
use kvsd::client::Api;
14
use ratatui::backend::TestBackend;
25
use synd_api::{client::github::GithubClient, serve::auth::Authenticator};
36
use synd_term::terminal::Terminal;
7+
use tokio::net::{TcpListener, TcpStream};
48

59
pub fn new_test_terminal() -> Terminal {
610
let backend = TestBackend::new(80, 20);
@@ -20,11 +24,54 @@ pub fn new_test_terminal() -> Terminal {
2024
// * datastore
2125
// * fetch cached feed
2226
// * authorizer
23-
pub async fn serve_api(mock_port: u16) -> anyhow::Result<()> {
27+
#[allow(unused)]
28+
pub fn serve_api(mock_port: u16) -> anyhow::Result<()> {
2429
let github_endpoint: &'static str =
2530
format!("http://localhost:{mock_port}/github/graphql").leak();
26-
let github_client = GithubClient::new()?.with_endpoint(&github_endpoint);
31+
let github_client = GithubClient::new()?.with_endpoint(github_endpoint);
2732
let authenticator = Authenticator::new()?.with_client(github_client);
2833

2934
Ok(())
3035
}
36+
37+
pub async fn run_kvsd() -> anyhow::Result<kvsd::client::tcp::Client<TcpStream>> {
38+
let root_dir = temp_dir();
39+
let mut config = kvsd::config::Config::default();
40+
41+
// Setup user credential.
42+
config.kvsd.users = vec![kvsd::core::UserEntry {
43+
username: "test".into(),
44+
password: "test".into(),
45+
}];
46+
config.server.set_disable_tls(&mut Some(true));
47+
48+
// Test Server listen addr
49+
let addr = ("localhost", 47379);
50+
51+
let mut initializer = kvsd::config::Initializer::from_config(config);
52+
53+
initializer.set_root_dir(root_dir.path());
54+
initializer.set_listener(TcpListener::bind(addr).await.unwrap());
55+
56+
initializer.init_dir().await.unwrap();
57+
58+
let _server_handler = tokio::spawn(initializer.run_kvsd(pending::<()>()));
59+
60+
// TODO: retry
61+
let mut client = kvsd::client::tcp::UnauthenticatedClient::insecure_from_addr(addr.0, addr.1)
62+
.await
63+
.unwrap()
64+
.authenticate("test", "test")
65+
.await
66+
.unwrap();
67+
68+
// Ping
69+
let ping_duration = client.ping().await.unwrap();
70+
assert!(ping_duration.num_nanoseconds().unwrap() > 0);
71+
72+
Ok(client)
73+
}
74+
75+
pub fn temp_dir() -> tempdir::TempDir {
76+
tempdir::TempDir::new("synd_term").unwrap()
77+
}

justfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test:
3232

3333
# Run integration test
3434
integration-test:
35-
RUST_LOG="synd,integration=debug" cargo nextest run --package synd_term --features integration --test integration --no-capture
35+
RUST_LOG="synd,integration,kvsd=debug" cargo nextest run --package synd_term --features integration --test integration --no-capture
3636

3737
update-gql-schema:
3838
@graphql-client introspect-schema http://localhost:5959/graphql \

rust-toolchain.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[toolchain]
2-
channel = "1.75.0"
2+
channel = "1.76.0"

0 commit comments

Comments
 (0)