Skip to content

Commit

Permalink
feat: add easy to use isolate runner test (#1750)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Jan 2, 2025
1 parent 911358e commit 1994555
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 108 deletions.
12 changes: 11 additions & 1 deletion packages/infra/client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/infra/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ anyhow = "1.0.79"
tokio = { version = "1.40.0", default-features = false, features = [ "full" ] }
nix = { version = "0.27", default-features = false, features = ["fs", "user", "signal"] }
tracing = "0.1"
tracing-logfmt = "0.3"
tracing-logfmt = { version = "0.3", features = ["ansi_logs"] }
tracing-subscriber = { version = "0.3", default-features = false, features = [
"ansi",
"fmt",
Expand Down
120 changes: 119 additions & 1 deletion packages/infra/client/isolate-v8-runner/src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub fn run(
Ok(())
}

// TODO: Cleanup this function
pub async fn run_inner(
config: config::Config,
actor_path: PathBuf,
Expand Down Expand Up @@ -508,3 +507,122 @@ fn wait_logs_complete(

Ok(())
}

#[cfg(test)]
mod tests {
use std::{net::SocketAddr, path::Path, result::Result::Ok};

use anyhow::*;
use deno_runtime::worker::MainWorkerTerminateHandle;
use foundationdb as fdb;
use pegboard::protocol;
use pegboard_config::isolate_runner as config;
use tracing_subscriber::prelude::*;
use uuid::Uuid;

use super::run_inner;
use crate::utils;

// TODO: Currently requires an fdb container to be running already
#[tokio::test]
async fn test_isolate() -> Result<()> {
tracing_subscriber::registry()
.with(
tracing_logfmt::builder()
.with_ansi_color(true)
.layer()
.with_filter(tracing_subscriber::filter::LevelFilter::INFO),
)
.init();

let tmp_dir = tempfile::TempDir::new().unwrap();
let actors_path = tmp_dir.path().join("actors");
let actor_id = Uuid::nil();

let fs_path = actors_path.join(actor_id.to_string()).join("fs");
std::fs::create_dir_all(&fs_path)?;

std::fs::copy(
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/index.js"),
fs_path.join("index.js"),
)?;
std::fs::write(tmp_dir.path().join("fdb.cluster"), "fdb:fdb@127.0.0.1:4500")?;

let config = config::Config {
// Not important
actors_path: Path::new("").to_path_buf(),
fdb_cluster_path: tmp_dir.path().join("fdb.cluster"),
runner_addr: SocketAddr::from(([0, 0, 0, 0], 0)),
};

deno_core::v8_set_flags(vec![
// Binary name
"UNUSED_BUT_NECESSARY_ARG0".into(),
// Disable eval
"--disallow-code-generation-from-strings".into(),
]);

// Start FDB network thread
let _network = unsafe { fdb::boot() };
tokio::spawn(utils::fdb_health_check(config.clone()));

// For receiving the terminate handle
let (terminate_tx, _terminate_rx) =
tokio::sync::mpsc::channel::<MainWorkerTerminateHandle>(1);

let actor_config = config::actor::Config {
resources: config::actor::Resources {
memory: 26843545600,
memory_max: 26843545600,
},
ports: Default::default(),
env: Default::default(),
metadata: protocol::Raw::new(&protocol::ActorMetadata {
actor: protocol::ActorMetadataActor {
actor_id: Uuid::nil(),
tags: [("foo".to_string(), "bar".to_string())]
.into_iter()
.collect(),
create_ts: 0,
},
project: protocol::ActorMetadataProject {
project_id: Uuid::nil(),
slug: "foo".into(),
},
environment: protocol::ActorMetadataEnvironment {
env_id: Uuid::nil(),
slug: "foo".into(),
},
datacenter: protocol::ActorMetadataDatacenter {
name_id: "local".to_string(),
display_name: "Local".to_string(),
},
cluster: protocol::ActorMetadataCluster {
cluster_id: Uuid::nil(),
},
build: protocol::ActorMetadataBuild {
build_id: Uuid::nil(),
},
})
.unwrap(),
owner: protocol::ActorOwner::DynamicServer {
server_id: actor_id,
},
vector_socket_addr: Default::default(),
};

let exit_code = run_inner(
config,
actors_path.join(actor_id.to_string()).to_path_buf(),
actor_id,
terminate_tx,
None,
actor_config,
)
.await?;

ensure!(exit_code == 0);

Ok(())
}
}
16 changes: 16 additions & 0 deletions packages/infra/client/isolate-v8-runner/tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Used by the test in isolate.rs

export default {
async start(ctx) {
console.log(ctx);

await ctx.kv.put(['foob', 'b'], 1);

let res = await ctx.kv.getBatch(['foob', 'b']);
console.log(res, res.get(['foob', 'b']));

Deno.exit(2);

throw new Error('bingus');
}
};
105 changes: 0 additions & 105 deletions packages/infra/client/isolate-v8-runner/tests/integration.rs

This file was deleted.

0 comments on commit 1994555

Please sign in to comment.