Skip to content

Commit

Permalink
src/client: Implement init proceedure
Browse files Browse the repository at this point in the history
Before running a test case, the sdk-go does:

- Waits for the network sidecar to initialize the network.
- Signals *being initialized* to the global count.
- Signals *being initialized* to the group count.
- Records the global sequence number and the group sequence number as a message.

With this commit the sdk-rust follows the same steps. In addition it not only
signals but also waits for all instances and the instances in the group to
*being initialized*.
  • Loading branch information
mxinden committed Jun 15, 2022
1 parent 3bc9dc4 commit be67422
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (client, run_parameters) = testground::client::Client::new().await?;
let (client, run_parameters) = testground::client::Client::new_and_init().await?;

client.record_message(format!(
"{}, sdk-rust!",
Expand Down
3 changes: 3 additions & 0 deletions src/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ impl BackgroundTask {
}
Command::WaitNetworkInitializedBarrier { sender } => {
if !self.params.test_sidecar {
// TODO: The Go implementation just returns immediately.
//
// https://github.com/testground/sdk-go/blob/49c90fa754052018b70c63d87b7f1d37f6080a78/network/client.go#L36
let _ = sender.send(Err(Error::SideCar));
return;
}
Expand Down
27 changes: 24 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,37 @@ pub struct Client {
}

impl Client {
pub async fn new() -> Result<(Self, RunParameters), Box<dyn std::error::Error>> {
pub async fn new_and_init() -> Result<(Self, RunParameters), Box<dyn std::error::Error>> {
let params = RunParameters::try_parse()?;

let (cmd_tx, cmd_rx) = channel(1);

let background = BackgroundTask::new(cmd_rx, params.clone()).await?;
let client = Self { cmd_tx };

tokio::spawn(background.run());

Ok((Self { cmd_tx }, params))
client.wait_network_initialized().await?;

let global_seq_num = client
// Note that the sdk-go only signals, but not waits.
.signal_and_wait("initialized_global", params.test_instance_count)
.await?;

let group_seq_num = client
// Note that the sdk-go only signals, but not waits.
.signal_and_wait(
format!("initialized_group_{}", params.test_group_id),
params.test_group_instance_count as u64,
)
.await?;

client.record_message(format!(
"claimed sequence numbers; global={}, group({})={}",
global_seq_num, params.test_group_id, group_seq_num
));

Ok((client, params))
}

/// ```publish``` publishes an item on the supplied topic.
Expand Down Expand Up @@ -133,7 +154,7 @@ impl Client {

/// ```wait_network_initialized``` waits for the sidecar to initialize the network,
/// if the sidecar is enabled.
pub async fn wait_network_initialized(&self) -> Result<(), Error> {
async fn wait_network_initialized(&self) -> Result<(), Error> {
// Event
let (sender, receiver) = oneshot::channel();

Expand Down
1 change: 1 addition & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct RunParameters {
#[clap(env)]
pub test_capture_profiles: String, // TEST_CAPTURE_PROFILES:

// TODO: Why is this a usize and the total count a u64?
#[clap(env)]
pub test_group_instance_count: usize, // TEST_GROUP_INSTANCE_COUNT: 1
#[clap(env)]
Expand Down

0 comments on commit be67422

Please sign in to comment.