| 1 | #![forbid(unsafe_code)] |
| 2 | #![allow(dead_code)] |
| 3 | |
| 4 | extern crate self as zipelo_ui_memory_app; |
| 5 | include!("../rust-ui-memory-races/assembly.rs"); |
| 6 | mod gates; |
| 7 | |
| 8 | use std::sync::atomic::Ordering; |
| 9 | |
| 10 | mod original_app { |
| 11 | include!("../../experiments/rust-ui-memory-app/src/app.rs"); |
| 12 | |
| 13 | use crate::{controller::Status, gates}; |
| 14 | use std::{ |
| 15 | sync::atomic::Ordering, |
| 16 | time::{Duration, Instant}, |
| 17 | }; |
| 18 | |
| 19 | struct Harness { |
| 20 | original: App, |
| 21 | control: gates::Control, |
| 22 | scenario: String, |
| 23 | started: Instant, |
| 24 | previous: Option<String>, |
| 25 | frames: u64, |
| 26 | operation: crate::core::OperationId, |
| 27 | } |
| 28 | |
| 29 | pub fn create( |
| 30 | controller: Controller, |
| 31 | control: gates::Control, |
| 32 | scenario: String, |
| 33 | ) -> Box<dyn eframe::App> { |
| 34 | let operation = controller.task.as_ref().unwrap().id; |
| 35 | Box::new(Harness { |
| 36 | original: App { controller }, |
| 37 | control, |
| 38 | scenario, |
| 39 | started: Instant::now(), |
| 40 | previous: None, |
| 41 | frames: 0, |
| 42 | operation, |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | impl eframe::App for Harness { |
| 47 | fn ui(&mut self, root: &mut egui::Ui, frame: &mut eframe::Frame) { |
| 48 | self.control.observe(); |
| 49 | egui::Panel::top("race-harness-only").show(root, |ui| { |
| 50 | ui.heading("시험용 화면 / 배포 바이너리 검증 아님"); |
| 51 | ui.label(format!( |
| 52 | "시나리오: {} / 장벽: {:?}", |
| 53 | self.scenario, self.control.stage |
| 54 | )); |
| 55 | ui.horizontal_wrapped(|ui| { |
| 56 | if ui |
| 57 | .add_enabled( |
| 58 | self.control.checkpoint_enabled(), |
| 59 | egui::Button::new("checkpoint 해제"), |
| 60 | ) |
| 61 | .clicked() |
| 62 | { |
| 63 | self.control.release_checkpoint(); |
| 64 | } |
| 65 | if ui |
| 66 | .add_enabled(self.control.join_enabled(), egui::Button::new("join 해제")) |
| 67 | .clicked() |
| 68 | { |
| 69 | self.control.release_join(); |
| 70 | } |
| 71 | }); |
| 72 | }); |
| 73 | <App as eframe::App>::ui(&mut self.original, root, frame); |
| 74 | self.frames += 1; |
| 75 | let c = &mut self.original.controller; |
| 76 | if c.task.as_ref().map(|t| t.id) != Some(self.operation) { |
| 77 | self.control.evidence.fault.store(true, Ordering::SeqCst); |
| 78 | } |
| 79 | let joined = c.task.as_ref().is_some_and(|task| task.joined()); |
| 80 | self.control.evidence.joined.store(joined, Ordering::SeqCst); |
| 81 | if joined { |
| 82 | let result = match c.status { |
| 83 | Status::Completed => 1, |
| 84 | Status::Cancelled => 2, |
| 85 | _ => 0, |
| 86 | }; |
| 87 | self.control |
| 88 | .evidence |
| 89 | .final_result |
| 90 | .store(result, Ordering::SeqCst); |
| 91 | } |
| 92 | let state = format!( |
| 93 | "scenario={} stage={:?} status={:?} close={:?} terminal={} joined={} cancel={} rows={}", |
| 94 | self.scenario, |
| 95 | self.control.stage, |
| 96 | c.status, |
| 97 | c.close, |
| 98 | c.terminal.is_some(), |
| 99 | joined, |
| 100 | c.task |
| 101 | .as_ref() |
| 102 | .is_some_and(|t| t.cancellation.is_requested()), |
| 103 | c.rows().len() |
| 104 | ); |
| 105 | if self.previous.as_ref() != Some(&state) { |
| 106 | eprintln!("harness frame={} {state}", self.frames); |
| 107 | self.previous = Some(state); |
| 108 | } |
| 109 | if c.status == Status::Panicked || self.started.elapsed() > Duration::from_secs(180) { |
| 110 | self.control.evidence.fault.store(true, Ordering::SeqCst); |
| 111 | } |
| 112 | if self.control.evidence.fault.load(Ordering::SeqCst) { |
| 113 | c.request_close(); |
| 114 | c.confirm_close(); |
| 115 | } |
| 116 | root.ctx().request_repaint_after(Duration::from_millis(30)); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | impl Drop for Harness { |
| 121 | fn drop(&mut self) { |
| 122 | let joined = self |
| 123 | .original |
| 124 | .controller |
| 125 | .task |
| 126 | .as_ref() |
| 127 | .is_some_and(|t| t.joined()); |
| 128 | self.control.evidence.joined.store(joined, Ordering::SeqCst); |
| 129 | if !joined { |
| 130 | self.control.evidence.fault.store(true, Ordering::SeqCst); |
| 131 | } |
| 132 | eprintln!("harness drop joined={joined} frames={}", self.frames); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn run() -> Result<(), String> { |
| 138 | let args: Vec<_> = std::env::args().skip(1).collect(); |
| 139 | if args.len() != 3 |
| 140 | || args[0] != "--opt-in-xvfb" |
| 141 | || args[1] != "--scenario" |
| 142 | || !["cancel", "retract", "close"].contains(&args[2].as_str()) |
| 143 | { |
| 144 | return Err("명시적 시험 실행 필요: --opt-in-xvfb --scenario cancel|retract|close".into()); |
| 145 | } |
| 146 | let display = std::env::var("DISPLAY").map_err(|_| "시험 DISPLAY 없음")?; |
| 147 | if display == ":0" |
| 148 | || std::env::var_os("WAYLAND_DISPLAY").is_some() |
| 149 | || std::env::var("ZIPELO_XVFB_HARNESS").as_deref() != Ok("1") |
| 150 | { |
| 151 | return Err("분리 Xvfb 자식 환경에서만 실행할 것".into()); |
| 152 | } |
| 153 | let font = std::fs::read("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc") |
| 154 | .map_err(|e| e.to_string())?; |
| 155 | let (controller, control) = gates::create(); |
| 156 | let evidence = control.evidence.clone(); |
| 157 | let scenario = args[2].clone(); |
| 158 | let expected = if scenario == "retract" { 1 } else { 2 }; |
| 159 | let result = eframe::run_native( |
| 160 | "Zipelo 시험용 메모리 경합 화면", |
| 161 | eframe::NativeOptions { |
| 162 | renderer: eframe::Renderer::Glow, |
| 163 | viewport: eframe::egui::ViewportBuilder::default() |
| 164 | .with_inner_size([1060.0, 840.0]) |
| 165 | .with_min_inner_size([760.0, 660.0]), |
| 166 | ..Default::default() |
| 167 | }, |
| 168 | Box::new(move |cc| { |
| 169 | use eframe::egui::{Color32, FontData, FontDefinitions, FontFamily, FontId, TextStyle}; |
| 170 | let mut fonts = FontDefinitions::empty(); |
| 171 | let mut data = FontData::from_owned(font); |
| 172 | data.index = 1; |
| 173 | fonts.font_data.insert("noto-kr".into(), data.into()); |
| 174 | fonts |
| 175 | .families |
| 176 | .insert(FontFamily::Proportional, vec!["noto-kr".into()]); |
| 177 | cc.egui_ctx.set_fonts(fonts); |
| 178 | cc.egui_ctx.set_visuals(eframe::egui::Visuals::light()); |
| 179 | cc.egui_ctx |
| 180 | .style_mut_of(eframe::egui::Theme::Light, |style| { |
| 181 | style |
| 182 | .text_styles |
| 183 | .insert(TextStyle::Body, FontId::proportional(18.0)); |
| 184 | style |
| 185 | .text_styles |
| 186 | .insert(TextStyle::Button, FontId::proportional(18.0)); |
| 187 | style |
| 188 | .text_styles |
| 189 | .insert(TextStyle::Heading, FontId::proportional(26.0)); |
| 190 | style.visuals.panel_fill = Color32::WHITE; |
| 191 | style.visuals.override_text_color = Some(Color32::from_rgb(24, 39, 57)); |
| 192 | style.visuals.selection.bg_fill = Color32::from_rgb(211, 230, 246); |
| 193 | style.spacing.item_spacing = eframe::egui::vec2(12.0, 10.0); |
| 194 | style.spacing.button_padding = eframe::egui::vec2(14.0, 10.0); |
| 195 | }); |
| 196 | Ok(original_app::create(controller, control, scenario)) |
| 197 | }), |
| 198 | ) |
| 199 | .map_err(|e| e.to_string()); |
| 200 | result?; |
| 201 | let joined = evidence.joined.load(Ordering::SeqCst); |
| 202 | let actual = evidence.final_result.load(Ordering::SeqCst); |
| 203 | if evidence.fault.load(Ordering::SeqCst) || !joined || actual != expected { |
| 204 | return Err(format!( |
| 205 | "시험 실패: joined={joined} expected={expected} actual={actual}" |
| 206 | )); |
| 207 | } |
| 208 | eprintln!("harness scenario completed joined=true result={actual}"); |
| 209 | Ok(()) |
| 210 | } |
| 211 | |
| 212 | fn main() { |
| 213 | if let Err(error) = run() { |
| 214 | eprintln!("harness failure: {error}"); |
| 215 | std::process::exit(1); |
| 216 | } |
| 217 | } |