Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,203 @@
|
||||
use dever_runtime::{
|
||||
task,
|
||||
wire::json::Json,
|
||||
worker::{self, Contract, Reply},
|
||||
};
|
||||
use std::{fs, io, os::unix::fs::PermissionsExt, sync::Arc};
|
||||
mod performance;
|
||||
static CONTRACT: Contract = Contract {
|
||||
port: "demo.test",
|
||||
schema: "runtime-worker-test",
|
||||
adapter: "default",
|
||||
capabilities: &[],
|
||||
operations: &["echo", "hang", "partial", "invalid"],
|
||||
};
|
||||
struct Fixture {
|
||||
directory: tempfile::TempDir,
|
||||
path: Arc<dever_runtime::process::Executable>,
|
||||
}
|
||||
impl Fixture {
|
||||
fn new() -> Self {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let path = directory.path().join("worker");
|
||||
fs::write(&path, include_str!("fixture.py")).unwrap();
|
||||
fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
|
||||
Self {
|
||||
directory,
|
||||
path: Arc::new(path.as_path().into()),
|
||||
}
|
||||
}
|
||||
fn setting(&self) -> Result<Json, String> {
|
||||
Json::encode(&serde_json::json!({"directory":self.directory.path()}))
|
||||
}
|
||||
}
|
||||
async fn finish(response: worker::Response) -> Result<serde_json::Value, String> {
|
||||
let decoded = match &response.reply {
|
||||
Reply::Result(payload) => serde_json::from_str(payload).map_err(|e| e.to_string()),
|
||||
_ => Err("unexpected error".into()),
|
||||
};
|
||||
response.finish(decoded).await
|
||||
}
|
||||
async fn call(
|
||||
scope: &task::Scope,
|
||||
fixture: &Fixture,
|
||||
operation: &'static str,
|
||||
payload: serde_json::Value,
|
||||
) -> io::Result<worker::Response> {
|
||||
worker::call(
|
||||
scope,
|
||||
&fixture.path,
|
||||
&CONTRACT,
|
||||
|| fixture.setting(),
|
||||
operation,
|
||||
Json::encode(&payload).unwrap(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
async fn marked(path: &std::path::Path) {
|
||||
tokio::time::timeout(std::time::Duration::from_secs(2), async {
|
||||
while !path.exists() {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(2)).await
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_cancel_drains_partial_frames_and_reuses_session() {
|
||||
for operation in ["hang", "partial"] {
|
||||
let fixture = Fixture::new();
|
||||
let marker = fixture.directory.path().join("started");
|
||||
let result = task::run(true, 16, async |scope| {
|
||||
let before = finish(
|
||||
call(scope, &fixture, "echo", serde_json::json!({"value":1}))
|
||||
.await
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let path = fixture.path.clone();
|
||||
let setting = fixture.setting().unwrap();
|
||||
let pending = scope
|
||||
.spawn(move |child| async move {
|
||||
worker::call(
|
||||
&child,
|
||||
&path,
|
||||
&CONTRACT,
|
||||
|| Ok(setting),
|
||||
operation,
|
||||
Json::encode(&serde_json::json!({})).unwrap(),
|
||||
)
|
||||
.await
|
||||
})
|
||||
.unwrap();
|
||||
marked(&marker).await;
|
||||
pending.stop().await.unwrap();
|
||||
assert!(fixture.directory.path().join("cancelled").exists());
|
||||
let after = finish(
|
||||
call(scope, &fixture, "echo", serde_json::json!({"value":2}))
|
||||
.await
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(before["pid"], after["pid"]);
|
||||
assert_eq!(after["value"], 2);
|
||||
Ok::<_, String>(after["pid"].as_u64().unwrap())
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(!std::path::Path::new(&format!("/proc/{result}")).exists());
|
||||
assert!(fixture.directory.path().join("shutdown").exists());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_protocol_or_output_rejection_reaps_before_return() {
|
||||
for invalid in [true, false] {
|
||||
let fixture = Fixture::new();
|
||||
task::run(true, 16, async |scope| {
|
||||
let value = finish(
|
||||
call(scope, &fixture, "echo", serde_json::json!({"value":1}))
|
||||
.await
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let pid = value["pid"].as_u64().unwrap();
|
||||
if invalid {
|
||||
assert!(
|
||||
call(scope, &fixture, "invalid", serde_json::json!({}))
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
} else {
|
||||
assert!(
|
||||
call(scope, &fixture, "echo", serde_json::json!({"value":2}))
|
||||
.await
|
||||
.unwrap()
|
||||
.finish::<()>(Err("bad typed output".into()))
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
assert!(!std::path::Path::new(&format!("/proc/{pid}")).exists());
|
||||
let restarted = finish(
|
||||
call(scope, &fixture, "echo", serde_json::json!({"value":3}))
|
||||
.await
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_ne!(restarted["pid"].as_u64().unwrap(), pid);
|
||||
assert_eq!(restarted["value"], 3);
|
||||
Ok::<_, String>(())
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_failure_invalidates_queued_calls_and_limits_restarts() {
|
||||
let fixture = Fixture::new();
|
||||
task::run(true, 16, async |scope| {
|
||||
let response = call(scope, &fixture, "echo", serde_json::json!({"value":1}))
|
||||
.await
|
||||
.unwrap();
|
||||
// 已持有响应直到类型验证,后续调用必定还在等待同一会话许可。
|
||||
let queued = call(scope, &fixture, "echo", serde_json::json!({"value":2}));
|
||||
tokio::pin!(queued);
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = &mut queued => panic!("queued call crossed validation boundary"),
|
||||
_ = tokio::task::yield_now() => {},
|
||||
}
|
||||
assert!(
|
||||
response
|
||||
.finish::<()>(Err("bad typed output".into()))
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(queued.await.err().unwrap().to_string().contains("queued"));
|
||||
for _ in 0..2 {
|
||||
assert!(
|
||||
call(scope, &fixture, "invalid", serde_json::json!({}))
|
||||
.await
|
||||
.err()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.contains("out of order")
|
||||
);
|
||||
}
|
||||
let error = call(scope, &fixture, "echo", serde_json::json!({"value":3}))
|
||||
.await
|
||||
.err()
|
||||
.unwrap();
|
||||
assert!(error.to_string().contains("restart budget"), "{error}");
|
||||
Ok::<_, String>(())
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user