Skip to content

Commit

Permalink
Add env test
Browse files Browse the repository at this point in the history
  • Loading branch information
akiradeveloper committed May 23, 2024
1 parent 4c8d8c0 commit 76d715e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
15 changes: 9 additions & 6 deletions tests/env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,17 @@ impl Env {
Ok(())
}

pub async fn ping(&self, id: u8) -> Result<()> {
let chan = self.connect(id);
let mut cli = testapp::PingClient::new(chan);
cli.ping(()).await?;
Ok(())
pub async fn connect_ping_client(&self, id: u8) -> Result<testapp::PingClient<Channel>> {
let uri: Uri = address_from_id(id).parse().unwrap();
let endpoint = Endpoint::from(uri)
.timeout(std::time::Duration::from_secs(1))
.connect_timeout(std::time::Duration::from_secs(1));
let chan = endpoint.connect().await?;
let cli = testapp::PingClient::new(chan);
Ok(cli)
}

pub fn connect(&self, id: u8) -> Channel {
pub fn connect_lazy(&self, id: u8) -> Channel {
let uri: Uri = address_from_id(id).parse().unwrap();
let endpoint = Endpoint::from(uri)
.timeout(std::time::Duration::from_secs(1))
Expand Down
35 changes: 33 additions & 2 deletions tests/env/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,41 @@ async fn start_stop() -> Result<()> {
env.start(0).await?;
env.connect_network(0).await?;

env.ping(0).await?;
let mut cli = env.connect_ping_client(0).await?;
cli.ping(()).await?;

env.stop(0).await?;
assert!(env.ping(0).await.is_err());
assert!(env.connect_ping_client(0).await.is_err());

Ok(())
}

#[serial]
#[tokio::test(flavor = "multi_thread")]
async fn panic_loop() -> Result<()> {
let mut env = env::Env::new()?;
env.create(0, 1).await?;
env.start(0).await?;
env.connect_network(0).await?;

for i in 0..1000 {
dbg!(i);
let mut cli = env.connect_ping_client(0).await?;
cli.panic(()).await.ok();
}

Ok(())
}

#[serial]
#[tokio::test(flavor = "multi_thread")]
async fn drop_env() -> Result<()> {
for _ in 0..100 {
let mut env = env::Env::new()?;
env.create(0, 1).await?;
env.start(0).await?;
env.connect_network(0).await?;
}

Ok(())
}
4 changes: 2 additions & 2 deletions tests/lol-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ impl Cluster {

/// Get an application client to connect to node `id`.
pub fn user(&self, id: u8) -> testapp::Client {
let conn = self.env.connect(id);
let conn = self.env.connect_lazy(id);
testapp::Client::new(conn)
}

pub fn admin(&self, id: u8) -> RaftClient {
let conn = self.env.connect(id);
let conn = self.env.connect_lazy(id);
lolraft::client::RaftClient::new(conn)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/lol-tests/tests/n1_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn n1_exec_once() -> Result<()> {
let mut cluster = Cluster::new(1, 1).await?;
cluster.add_server(0, 0, 0).await?;

let chan = cluster.env().connect(0);
let chan = cluster.env().connect_lazy(0);
let cli = lolraft::client::RaftClient::new(chan);

let req = lolraft::client::WriteRequest {
Expand Down
1 change: 1 addition & 0 deletions tests/testapp/proto/testapp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ package testapp;

service Ping {
rpc Ping (google.protobuf.Empty) returns (google.protobuf.Empty);
rpc Panic (google.protobuf.Empty) returns (google.protobuf.Empty);
}
7 changes: 7 additions & 0 deletions tests/testapp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ impl proto::ping_server::Ping for PingApp {
) -> std::result::Result<tonic::Response<()>, tonic::Status> {
Ok(tonic::Response::new(()))
}

async fn panic(
&self,
_: tonic::Request<()>,
) -> std::result::Result<tonic::Response<()>, tonic::Status> {
panic!()
}
}

#[derive(serde::Deserialize, Debug)]
Expand Down

0 comments on commit 76d715e

Please sign in to comment.