Skip to content

Commit

Permalink
pocketic restart tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed May 23, 2024
1 parent 4b34f21 commit 462b818
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
46 changes: 44 additions & 2 deletions e2e/tests-dfx/start.bash
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ teardown() {
}

@test "dfx restarts the replica" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: pid"
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: state persistence"
dfx_new hello
dfx_start

Expand Down Expand Up @@ -130,6 +130,20 @@ teardown() {
assert_eq '("Hello, Omega!")'
}

@test "dfx restarts pocketic" {
[[ "$USE_POCKETIC" ]] || skip "skipped for replica"
dfx_start

POCKETIC_PID=$(get_pocketic_pid)
echo "pocketic pid is $POCKETIC_PID"
kill -KILL "$POCKETIC_PID"
assert_process_exits "$POCKETIC_PID" 15s
timeout 15s sh -c \
'until dfx ping; do echo waiting for pocketic to restart; sleep 1; done' \
|| (echo "pocketic did not restart" && ps aux && exit 1)
assert_command wait_until_replica_healthy
}

@test "dfx restarts icx-proxy" {
dfx_new_assets hello
dfx_start
Expand All @@ -156,7 +170,7 @@ teardown() {
}

@test "dfx restarts icx-proxy when the replica restarts" {
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: pid"
[[ "$USE_POCKETIC" ]] && skip "skipped for pocketic: state persistence"
dfx_new_assets hello
dfx_start

Expand Down Expand Up @@ -205,6 +219,34 @@ teardown() {
assert_command curl --fail http://localhost:"$(get_webserver_port)"/sample-asset.txt?canisterId="$ID"
}

@test "dfx restarts icx-proxy when pocketic restarts" {
[[ "$USE_POCKETIC" ]] || skip "skipped for replica"
dfx_start
POCKETIC_PID=$(get_pocketic_pid)
ICX_PROXY_PID=$(get_icx_proxy_pid)
echo "pocketic pid is $POCKETIC_PID"
echo "icx-proxy pid is $ICX_PROXY_PID"

kill -KILL "$POCKETIC_PID"
assert_process_exits "$POCKETIC_PID" 15s
assert_process_exits "$ICX_PROXY_PID" 15s

timeout 15s sh -c \
'until dfx ping; do echo waiting for replica to restart; sleep 1; done' \
|| (echo "replica did not restart" && ps aux && exit 1)
assert_command wait_until_replica_healthy
POCKETIC_NETWORK="http://localhost:$(get_pocketic_port)/instances/0/"
dfx_new_assets hello
assert_command dfx deploy --network "$POCKETIC_NETWORK"
ID=$(dfx canister id hello_frontend --network "$POCKETIC_NETWORK")

timeout 15s sh -c \
"until curl --fail http://localhost:\$(cat \"$E2E_SHARED_LOCAL_NETWORK_DATA_DIRECTORY/webserver-port\")/sample-asset.txt?canisterId=$ID; do echo waiting for icx-proxy to restart; sleep 1; done" \
|| (echo "icx-proxy did not restart" && ps aux && exit 1)

assert_command curl --fail http://localhost:"$(get_webserver_port)"/sample-asset.txt?canisterId="$ID"
}

@test "dfx start honors replica port configuration" {
create_networks_json
replica_port=$(get_ephemeral_port)
Expand Down
4 changes: 4 additions & 0 deletions e2e/utils/_.bash
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ get_replica_port() {
cat "$E2E_NETWORK_DATA_DIRECTORY/replica-configuration/replica-1.port"
}

get_pocketic_pid() {
cat "$E2E_NETWORK_DATA_DIRECTORY/pocket-ic-pid"
}

get_pocketic_port() {
cat "$E2E_NETWORK_DATA_DIRECTORY/pocket-ic.port"
}
Expand Down
4 changes: 4 additions & 0 deletions src/dfx-core/src/config/model/local_server_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ impl LocalServerDescriptor {
self.data_directory.join("pocket-ic.port")
}

pub fn pocketic_pid_path(&self) -> PathBuf {
self.data_directory.join("pocket-ic-pid")
}

/// Returns whether the local server is PocketIC (as opposed to the replica)
pub fn is_pocketic(&self) -> Result<bool, LoadNetworksConfigError> {
Ok(
Expand Down
1 change: 1 addition & 0 deletions src/dfx/src/actors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ pub fn start_pocketic_actor(
pocketic_path,
port: local_server_descriptor.replica.port,
port_file: pocketic_port_path,
pid_file: local_server_descriptor.pocketic_pid_path(),
shutdown_controller,
logger: Some(env.get_logger().clone()),
verbose: env.get_verbose_level() > 0,
Expand Down
10 changes: 9 additions & 1 deletion src/dfx/src/actors/pocketic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use actix::{
use anyhow::bail;
use candid::Principal;
use crossbeam::channel::{unbounded, Receiver, Sender};
use slog::{debug, error, info, Logger};
use slog::{debug, error, info, warn, Logger};
use std::path::{Path, PathBuf};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
Expand All @@ -38,6 +38,7 @@ pub struct Config {
pub pocketic_path: PathBuf,
pub port: Option<u16>,
pub port_file: PathBuf,
pub pid_file: PathBuf,
pub shutdown_controller: Addr<ShutdownController>,
pub logger: Option<Logger>,
pub verbose: bool,
Expand Down Expand Up @@ -225,6 +226,13 @@ fn pocketic_start_thread(
let last_start = std::time::Instant::now();
debug!(logger, "Starting PocketIC...");
let mut child = cmd.spawn().expect("Could not start PocketIC.");
if let Err(e) = std::fs::write(&config.pid_file, child.id().to_string()) {
warn!(
logger,
"Failed to write PocketIC PID to {}: {e}",
config.pid_file.display()
);
}

let port = PocketIc::wait_for_port_file(&config.port_file).unwrap();

Expand Down

0 comments on commit 462b818

Please sign in to comment.