use super::*; #[test] fn concurrent_loops_round_trip_bound_capacity_and_expose_cleanup() { let (project, _) = checked( include_str!("../../native/concurrent.dever"), "concurrent-loop", ); let path = project.sources[0].path.to_str().unwrap(); success(cli(&["fmt", path])); success(cli(&["fmt", path, "--check"])); let index = dever::compiler::inspect::analyze(&project, Some("main.main.app.ordered"), 1, true) .unwrap(); let text = serde_json::to_string(&index).unwrap(); for label in [ "并发容量", "在途工作与待交付结果共用容量", "同一元素内逐行执行", "保留首个观察错误", "按输入顺序交付剩余结果", ] { assert!(text.contains(label), "{label}"); } let cases = [ "loop bad(n: Int) (out: List)\n| each value in range(0, n) parallel n | result |\n| out | list value |", "loop bad() (out: List)\n| each value in [1] parallel 0 | result |\n| out | list value |", "loop bad() (out: List)\n| each value in [1] parallel 65537 | result |\n| out | list value |", "loop bad() (out: Int)\n| each value in [1] parallel 2 | result |\n| out | sum value |", "loop bad() (out: List)\n| each value in [1] parallel 2 | take | result |\n| out | 1 | list value |", "loop bad() (out: List)\n| each value in [1] parallel 2 | sort | result |\n| out | [value.asc] | list value |", "loop bad() (out: List)\n| each value in [1] parallel value | result |\n| out | list value |", "loop bad(file: dever.system.File) ()\n| each value in [1] parallel 2 | result |\n| - | do dever.io.close(file) |", "loop bad(values: Stream) (out: List)\n| each value in values parallel 2 | result |\n| out | list value |", ]; for (index, source) in cases.iter().enumerate() { let path = directory(&format!("invalid-concurrent-{index}")).join("app.dever"); fs::write(&path, source).unwrap(); let project = Project::load(&path).unwrap(); assert!( project.diagnostics.is_empty(), "{index}: {:?}", project.diagnostics ); assert!( !program::analyze(&project.declarations, &project.modules) .diagnostics .is_empty(), "{index}" ); } } #[test] fn catching_concurrent_failure_does_not_erase_cancelled_input_obligations() { let source = r#" type Attempt { Ok, error Bad } type Trouble { error Broken } type Caught { Done(values: List), error Failed(reason: Trouble) } rule resolve(value: Attempt) (out: Int) recover("report failure as -1") | value | out | | Ok | 1 | | Bad | -1 | flow work(value: Attempt, valid: Bool) (out: Int) | step | kind | do | | - | check | valid else Trouble.Broken | | out | rule | resolve(value) | loop jobs(values: List, valid: Bool) (out: List) | each value in values parallel 2 | result | | out | list work(value, valid) | flow forwarded(values: List, valid: Bool) (out: List) | step | kind | do | | out | loop | jobs(values, valid) | rule recoverCaught(value: Caught) (out: List) recover("report failed batch as empty") | value | out | | Done(values) | values | | Failed(reason) | [] | flow bad(values: List, valid: Bool) (out: List) | step | kind | do | | attempt: Caught | loop | result(jobs(values, valid)) | | out | rule | recoverCaught(attempt) | "#; for target in ["jobs", "forwarded"] { let source = source .replace( "result(jobs(values, valid))", &format!("result({target}(values, valid))"), ) .replace( "| attempt: Caught | loop |", if target == "jobs" { "| attempt: Caught | loop |" } else { "| attempt: Caught | flow |" }, ); let path = directory(&format!("concurrent-cancelled-input-{target}")).join("app.dever"); fs::write(&path, &source).unwrap(); let project = Project::load(&path).unwrap(); assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics); let errors = program::analyze(&project.declarations, &project.modules).diagnostics; assert!( errors .iter() .any(|error| error.code == "C012" && error.message.contains("failure is not handled")), "{errors:?}" ); let valid = source .replace("| attempt: Caught |", "| out | ") .split("| out | rule | recoverCaught") .next() .unwrap() .replace("result(jobs(values, valid))", "jobs(values, valid)") .replace( "result(forwarded(values, valid))", "forwarded(values, valid)", ); checked(&valid, &format!("concurrent-propagated-input-{target}")); } } #[test] fn parallel_groups_round_trip_check_dependencies_and_expose_fork_join_cleanup() { let source = include_str!("../../native/parallel.dever").replace( "| left | flow | read | delayed(11, 5) |", "| left | - | read | delayed(11, 5) |", ); let (project, program) = checked(&source, "parallel"); let source = project.sources[0].path.to_str().unwrap(); success(cli(&["fmt", source])); success(cli(&["fmt", source, "--check"])); let formatted = fs::read_to_string(source).unwrap(); assert!(formatted.contains("parallel")); let row = formatted .lines() .find(|row| row.contains("delayed(11, 5)")) .unwrap(); assert_eq!( row.split('|').map(str::trim).collect::>(), ["", "left", "flow", "read", "delayed(11, 5)", ""] ); let pure = program .functions .iter() .find(|f| f.signature.name.ends_with(".pureGroup")) .unwrap(); assert!(pure.effects.pure()); let generated = emit::emit(&program, &ownership::analyze(&program), &project.sources); let text = generated .values() .map(|file| file.text.as_str()) .collect::(); assert!(text.contains("try_join!(biased;")); assert!(!text.contains("tokio::spawn") && !text.contains("Box::pin")); assert!(text.contains("pub fn d_4_main_4_main_3_app_9_pureGroup")); let index = dever::compiler::inspect::analyze(&project, Some("main.main.app.together"), 0, true) .unwrap(); let data = serde_json::to_value(index).unwrap(); let flow = &data["flows"][0]; for label in [ "并发组 read", "等待本组全部步骤成功", "取消其余步骤,等待资源释放", ] { assert!( flow["nodes"] .as_array() .unwrap() .iter() .any(|node| node["label"] == label), "{label}" ); } assert_eq!( flow["edges"] .as_array() .unwrap() .iter() .filter(|edge| edge["label"] == "并发推进") .count(), 2 ); } #[test] fn parallel_rejects_single_split_guard_dependent_and_hidden_effect_steps() { let root = directory("parallel-errors"); let path = root.join("app.dever"); let prefix = "identity(value: Int) (out: Int) { out = value }\nflow delayed(value: Int) (out: Int)\n| step | kind | do |\n| - | flow | dever.time.sleep(1) |\n| out | calc | value |\n"; for rows in [ "| a | calc | group | identity(value) |\n| out | calc | - | a |", "| a | calc | group | identity(value) |\n| b | calc | group | identity(value) |\n| c | calc | - | a |\n| d | calc | group | identity(value) |\n| out | calc | group | identity(value) |", "| - | check | group | value != 0 else Invalid |\n| out | calc | group | identity(value) |", "| a | calc | group | identity(value) |\n| out | calc | group | identity(a) |", "| a | calc | group | identity(delayed(value)) |\n| out | calc | group | identity(value) |", ] { fs::write(&path, format!("{prefix}\nflow bad(value: Int) (out: Int)\n| step | kind | parallel | do |\n{rows}\n")).unwrap(); let project = Project::load(&path).unwrap(); assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics); let errors = program::analyze(&project.declarations, &project.modules).diagnostics; assert!( errors.iter().any(|error| error.code == "C013"), "{errors:?}" ); } }