Skip to content

Commit ee752c0

Browse files
committed
test
Signed-off-by: tison <wander4096@gmail.com>
1 parent 153773c commit ee752c0

File tree

13 files changed

+160
-44
lines changed

13 files changed

+160
-44
lines changed

Cargo.lock

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ version = "0.1.0"
1515

1616
[workspace.dependencies]
1717
# Workspace members
18+
atrium = { path = "atrium-server" }
1819
atrium-client = { path = "atrium-client" }
1920
atrium-core = { path = "atrium-core" }
20-
atrium = { path = "atrium-server" }
2121
tests-toolkit = { path = "tests/toolkit" }
2222

2323
# Crates.io dependencies

atrium-client/src/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ impl ClientBuilder {
1010
}
1111

1212
pub fn build(self) -> Client {
13-
Client {
14-
endpoint: self.endpoint,
15-
}
13+
let builder = reqwest::ClientBuilder::new().no_proxy();
14+
// FIXME(tisonkun): fallible over unwrap
15+
Client::new(self.endpoint, builder).unwrap()
1616
}
1717
}

atrium-client/src/client.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@ pub enum Error {
1616
}
1717

1818
pub struct Client {
19-
pub(crate) endpoint: String,
19+
endpoint: String,
20+
client: reqwest::Client,
2021
}
2122

2223
impl Client {
24+
pub(crate) fn new(
25+
endpoint: impl Into<String>,
26+
builder: reqwest::ClientBuilder,
27+
) -> Result<Self, reqwest::Error> {
28+
let client = builder.build()?;
29+
let endpoint = endpoint.into();
30+
Ok(Client { endpoint, client })
31+
}
32+
2333
pub async fn get(&self, key: &str) -> Result<Option<Vec<u8>>, Error> {
2434
do_get(self, key).await
2535
}
@@ -41,7 +51,12 @@ async fn do_get(client: &Client, key: &str) -> Result<Option<Vec<u8>>, Error> {
4151

4252
url = url.join(&encoded_key).change_context_lazy(make_error)?;
4353

44-
let resp = reqwest::get(url).await.change_context_lazy(make_error)?;
54+
let resp = client
55+
.client
56+
.get(url)
57+
.send()
58+
.await
59+
.change_context_lazy(make_error)?;
4560

4661
match resp.status() {
4762
StatusCode::OK => {
@@ -63,7 +78,8 @@ async fn do_put(client: &Client, key: &str, value: &[u8]) -> Result<(), Error> {
6378

6479
url = url.join(&encoded_key).change_context_lazy(make_error)?;
6580

66-
let resp = reqwest::Client::new()
81+
let resp = client
82+
.client
6783
.put(url)
6884
.body(value.to_vec())
6985
.send()
@@ -85,7 +101,8 @@ async fn do_delete(client: &Client, key: &str) -> Result<(), Error> {
85101

86102
url = url.join(&encoded_key).change_context_lazy(make_error)?;
87103

88-
let resp = reqwest::Client::new()
104+
let resp = client
105+
.client
89106
.delete(url)
90107
.send()
91108
.await

atrium-core/src/config.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct Config {
1010
#[serde(skip_serializing_if = "Option::is_none")]
1111
pub advertise_addr: Option<String>,
1212
#[serde(default = "default_path")]
13-
pub path: String,
13+
pub path: PathBuf,
1414
pub disk_capacity: u64,
1515
pub memory_capacity: u64,
1616
}
@@ -19,12 +19,8 @@ fn default_listen_addr() -> String {
1919
"0.0.0.0:7654".to_string()
2020
}
2121

22-
fn default_advertise_addr() -> String {
23-
"127.0.0.1:7654".to_string()
24-
}
25-
26-
fn default_path() -> String {
27-
"/usr/local/atrium".to_string()
22+
fn default_path() -> PathBuf {
23+
PathBuf::from("/usr/local/atrium")
2824
}
2925

3026
pub fn data_path(base: impl Into<PathBuf>) -> PathBuf {
@@ -37,8 +33,8 @@ impl Default for Config {
3733
listen_addr: default_listen_addr(),
3834
advertise_addr: None,
3935
path: default_path(),
40-
disk_capacity: 0,
41-
memory_capacity: 0,
36+
disk_capacity: 512 * 1024,
37+
memory_capacity: 1024 * 1024,
4238
}
4339
}
4440
}

atrium-core/src/engine.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::Path;
2+
13
use error_stack::Result;
24
use error_stack::ResultExt;
35
use error_stack::report;
@@ -24,7 +26,7 @@ pub struct FoyerEngine {
2426

2527
impl FoyerEngine {
2628
pub async fn try_new(
27-
path: &str,
29+
path: &Path,
2830
memory_capacity: u64,
2931
disk_capacity: u64,
3032
) -> Result<Self, EngineError> {
@@ -82,12 +84,11 @@ mod tests {
8284

8385
#[tokio::test]
8486
async fn test_get() {
85-
let temp_dir = tempfile::tempdir_in("/tmp/foyer").unwrap();
87+
let temp_dir = tempfile::tempdir().unwrap();
8688

87-
let engine =
88-
FoyerEngine::try_new(temp_dir.path().to_str().unwrap(), 512 * 1024, 1024 * 1024)
89-
.await
90-
.unwrap();
89+
let engine = FoyerEngine::try_new(temp_dir.path(), 512 * 1024, 1024 * 1024)
90+
.await
91+
.unwrap();
9192
engine.put(b"foo".to_vec().as_ref(), b"bar".to_vec().as_ref());
9293

9394
assert_compact_debug_snapshot!(

atrium-server/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ async fn main() -> Result<(), Error> {
4040

4141
log::info!("config: {config:#?}");
4242

43-
atrium::server::start_server(&config, ctx)
43+
let server = atrium::server::start_server(&config, ctx)
4444
.await
4545
.inspect_err(|err| {
4646
log::error!("server stopped: {}", err);
4747
})
4848
.change_context_lazy(make_error)?;
49-
49+
server.await_shutdown().await;
5050
Ok(())
5151
}

atrium-server/src/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub async fn start_server(config: &Config, ctx: Arc<Context>) -> Result<ServerSt
123123

124124
let server_fut = {
125125
let shutdown_clone = shutdown.clone();
126-
let wg_clone = wg;
126+
let wg_clone = wg.clone();
127127

128128
let route = Route::new()
129129
.at("/:key", poem::get(get).put(put).delete(delete))
@@ -159,7 +159,7 @@ fn resolve_advertise_addr(
159159
match advertise_addr {
160160
None => {
161161
if listen_addr.ip().is_unspecified() {
162-
let ip = local_ip_address::local_ip()?;
162+
let ip = local_ip_address::local_ip().map_err(io::Error::other)?;
163163
let port = listen_addr.port();
164164
Ok(SocketAddr::new(ip, port))
165165
} else {

tests/behavior/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ edition.workspace = true
55
version.workspace = true
66

77
[dependencies]
8-
atrium-core = { workspace = true }
98
atrium = { workspace = true }
9+
atrium-client = { workspace = true }
10+
atrium-core = { workspace = true }
1011
test-harness = { workspace = true }
1112
tests-toolkit = { workspace = true }
1213
tokio = { workspace = true, features = ["full"] }

tests/behavior/src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
use std::process::ExitCode;
22

3+
use atrium_client::Client;
4+
use atrium_client::ClientBuilder;
35
use tests_toolkit::make_test_name;
46

5-
pub struct Testkit {}
7+
pub struct Testkit {
8+
pub client: Client,
9+
}
610

711
pub fn harness<T, Fut>(test: impl Send + FnOnce(Testkit) -> Fut) -> ExitCode
812
where
913
T: std::process::Termination,
1014
Fut: Send + Future<Output = T>,
1115
{
16+
let rt = tokio::runtime::Runtime::new().unwrap();
17+
1218
let test_name = make_test_name::<Fut>();
13-
let Some(_state) = tests_toolkit::start_test_server(&test_name) else {
19+
let Some(state) = tests_toolkit::start_test_server(&test_name, &rt) else {
1420
return ExitCode::SUCCESS;
1521
};
1622

17-
let rt = tokio::runtime::Runtime::new().unwrap();
18-
1923
rt.block_on(async move {
20-
let exit_code = test(Testkit {}).await.report();
24+
let server_addr = format!("http://{}/", state.server_state.server_advertise_addr());
25+
let client = ClientBuilder::new(server_addr).build();
26+
27+
let exit_code = test(Testkit { client }).await.report();
28+
29+
state.server_state.shutdown();
30+
state.server_state.await_shutdown().await;
2131
exit_code
2232
})
2333
}

0 commit comments

Comments
 (0)