Skip to content

Commit

Permalink
update test to single thread and multithread test
Browse files Browse the repository at this point in the history
  • Loading branch information
cavemanloverboy committed Dec 18, 2022
1 parent 23a9af4 commit af81d0b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
34 changes: 32 additions & 2 deletions client/example/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,39 @@ main() {
sleep 5

#
# Run Test.
# Run single threaded test.
#
cargo run -- --composite-pid $composite_pid --basic-2-pid $basic_2_pid --basic-4-pid $basic_4_pid --events-pid $events_pid --optional-pid $optional_pid
cargo run -- \
--composite-pid $composite_pid \
--basic-2-pid $basic_2_pid \
--basic-4-pid $basic_4_pid \
--events-pid $events_pid \
--optional-pid $optional_pid

#
# Restart validator for multithreaded test
#
cleanup
solana-test-validator -r \
--bpf-program $composite_pid ../../tests/composite/target/deploy/composite.so \
--bpf-program $basic_2_pid ../../examples/tutorial/basic-2/target/deploy/basic_2.so \
--bpf-program $basic_4_pid ../../examples/tutorial/basic-4/target/deploy/basic_4.so \
--bpf-program $events_pid ../../tests/events/target/deploy/events.so \
--bpf-program $optional_pid ../../tests/optional/target/deploy/optional.so \
> test-validator.log &
sleep 5

#
# Run multi threaded test.
#
cargo run -- \
--composite-pid $composite_pid \
--basic-2-pid $basic_2_pid \
--basic-4-pid $basic_4_pid \
--events-pid $events_pid \
--optional-pid $optional_pid \
--multithreaded

}

cleanup() {
Expand Down
55 changes: 40 additions & 15 deletions client/example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize};
use composite::instruction as composite_instruction;
use composite::{DummyA, DummyB};
use optional::account::{DataAccount, DataPda};
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;

#[derive(Parser, Debug)]
Expand All @@ -39,12 +39,15 @@ pub struct Opts {
events_pid: Pubkey,
#[clap(long)]
optional_pid: Pubkey,
#[clap(long, default_value = "false")]
multithreaded: bool,
}

type TestFn = &'static (dyn Fn(&Client<Keypair>, Pubkey) -> Result<()> + Send + Sync);

// This example assumes a local validator is running with the programs
// deployed at the addresses given by the CLI args.
fn main() -> Result<()> {
println!("Starting test...");
let opts = Opts::parse();

// Wallet and cluster params.
Expand All @@ -56,14 +59,36 @@ fn main() -> Result<()> {
);

// Client.
let client = Client::new_with_options(url, Rc::new(payer), CommitmentConfig::processed());

// Run tests.
composite(&client, opts.composite_pid)?;
basic_2(&client, opts.basic_2_pid)?;
basic_4(&client, opts.basic_4_pid)?;
events(&client, opts.events_pid)?;
optional(&client, opts.optional_pid)?;
let client = Client::new_with_options(url, Arc::new(payer), CommitmentConfig::processed());

if !opts.multithreaded {
// Run tests on single thread with a single client
println!("\nStarting single thread test...");
composite(&client, opts.composite_pid)?;
basic_2(&client, opts.basic_2_pid)?;
basic_4(&client, opts.basic_4_pid)?;
events(&client, opts.events_pid)?;
optional(&client, opts.optional_pid)?;
} else {
// Run tests multithreaded, sharing a client
println!("\nStarting multithread test...");
let client = Arc::new(client);
let tests: Vec<(TestFn, Pubkey)> = vec![
(&composite, opts.composite_pid),
(&basic_2, opts.basic_2_pid),
(&basic_4, opts.basic_4_pid),
(&events, opts.events_pid),
(&optional, opts.optional_pid),
];
let mut handles = vec![];
for (test, arg) in tests {
let local_client = Arc::clone(&client);
handles.push(std::thread::spawn(move || test(&local_client, arg)));
}
for handle in handles {
assert!(handle.join().unwrap().is_ok());
}
}

// Success.
Ok(())
Expand All @@ -72,7 +97,7 @@ fn main() -> Result<()> {
// Runs a client for examples/tutorial/composite.
//
// Make sure to run a localnet with the program deploy to run this example.
fn composite(client: &Client, pid: Pubkey) -> Result<()> {
fn composite<S: Signer + Send + Sync>(client: &Client<S>, pid: Pubkey) -> Result<()> {
// Program client.
let program = client.program(pid);

Expand Down Expand Up @@ -143,7 +168,7 @@ fn composite(client: &Client, pid: Pubkey) -> Result<()> {
// Runs a client for examples/tutorial/basic-2.
//
// Make sure to run a localnet with the program deploy to run this example.
fn basic_2(client: &Client, pid: Pubkey) -> Result<()> {
fn basic_2<S: Signer + Send + Sync>(client: &Client<S>, pid: Pubkey) -> Result<()> {
let program = client.program(pid);

// `Create` parameters.
Expand Down Expand Up @@ -172,7 +197,7 @@ fn basic_2(client: &Client, pid: Pubkey) -> Result<()> {
Ok(())
}

fn events(client: &Client, pid: Pubkey) -> Result<()> {
fn events<S: Signer + Send + Sync>(client: &Client<S>, pid: Pubkey) -> Result<()> {
let program = client.program(pid);

let (sender, receiver) = std::sync::mpsc::channel();
Expand Down Expand Up @@ -204,7 +229,7 @@ fn events(client: &Client, pid: Pubkey) -> Result<()> {
Ok(())
}

pub fn basic_4(client: &Client, pid: Pubkey) -> Result<()> {
pub fn basic_4<S: Signer + Send + Sync>(client: &Client<S>, pid: Pubkey) -> Result<()> {
let program = client.program(pid);
let authority = program.payer();

Expand Down Expand Up @@ -236,7 +261,7 @@ pub fn basic_4(client: &Client, pid: Pubkey) -> Result<()> {
// Runs a client for tests/optional.
//
// Make sure to run a localnet with the program deploy to run this example.
fn optional(client: &Client, pid: Pubkey) -> Result<()> {
fn optional<S: Signer + Send + Sync>(client: &Client<S>, pid: Pubkey) -> Result<()> {
// Program client.
let program = client.program(pid);

Expand Down

0 comments on commit af81d0b

Please sign in to comment.