implements PLN-0149
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
use crate::VirtualMachineRuntime;
|
||||
use crate::fs::FsState;
|
||||
use crate::process::{Process, ProcessId, ProcessKind};
|
||||
use crate::task::{Task, TaskId, TaskKind};
|
||||
use prometeu_hal::app_mode::AppMode;
|
||||
use prometeu_hal::telemetry::CertificationConfig;
|
||||
use prometeu_vm::VirtualMachine;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct VmSessionId(pub TaskId);
|
||||
@@ -24,6 +29,10 @@ pub struct VmSession {
|
||||
pub app_version: String,
|
||||
pub app_mode: AppMode,
|
||||
pub vm: VirtualMachine,
|
||||
pub runtime: VirtualMachineRuntime,
|
||||
pub fs_state: FsState,
|
||||
pub open_files: HashMap<u32, String>,
|
||||
pub next_handle: u32,
|
||||
}
|
||||
|
||||
impl VmSession {
|
||||
@@ -32,6 +41,8 @@ impl VmSession {
|
||||
process: &Process,
|
||||
app_mode: AppMode,
|
||||
app_version: impl Into<String>,
|
||||
cap_config: Option<CertificationConfig>,
|
||||
logs_count: Arc<AtomicU32>,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: VmSessionId(task.id),
|
||||
@@ -42,8 +53,17 @@ impl VmSession {
|
||||
app_version: app_version.into(),
|
||||
app_mode,
|
||||
vm: VirtualMachine::default(),
|
||||
runtime: VirtualMachineRuntime::new_with_log_counter(cap_config, logs_count),
|
||||
fs_state: FsState::Unmounted,
|
||||
open_files: HashMap::new(),
|
||||
next_handle: 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_cartridge_service_state(&mut self) {
|
||||
self.open_files.clear();
|
||||
self.next_handle = 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -61,6 +81,8 @@ impl VmSessionRegistry {
|
||||
task: &Task,
|
||||
process: &Process,
|
||||
app_version: impl Into<String>,
|
||||
cap_config: Option<CertificationConfig>,
|
||||
logs_count: Arc<AtomicU32>,
|
||||
) -> Result<VmSessionId, VmSessionError> {
|
||||
if task.process_id != process.id {
|
||||
return Err(VmSessionError::TaskProcessMismatch {
|
||||
@@ -76,7 +98,10 @@ impl VmSessionRegistry {
|
||||
return Err(VmSessionError::SessionAlreadyExists(task.id));
|
||||
}
|
||||
|
||||
self.sessions.insert(id, VmSession::new(task, process, app_mode, app_version));
|
||||
self.sessions.insert(
|
||||
id,
|
||||
VmSession::new(task, process, app_mode, app_version, cap_config, logs_count),
|
||||
);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
@@ -143,7 +168,9 @@ mod tests {
|
||||
let task = Task::new(TaskId(7), process.id, 42, "Stress", TaskKind::Game);
|
||||
let mut registry = VmSessionRegistry::new();
|
||||
|
||||
let id = registry.create_for_task(&task, &process, "").expect("session should create");
|
||||
let id = registry
|
||||
.create_for_task(&task, &process, "", None, Arc::new(AtomicU32::new(0)))
|
||||
.expect("session should create");
|
||||
let session = registry.get(id).expect("session should exist");
|
||||
|
||||
assert_eq!(session.id, VmSessionId(task.id));
|
||||
@@ -152,6 +179,9 @@ mod tests {
|
||||
assert_eq!(session.app_id, 42);
|
||||
assert_eq!(session.title, "Stress");
|
||||
assert_eq!(session.app_mode, AppMode::Game);
|
||||
assert_eq!(session.runtime.current_app_id, 0);
|
||||
assert_eq!(session.fs_state, FsState::Unmounted);
|
||||
assert_eq!(session.next_handle, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -160,7 +190,9 @@ mod tests {
|
||||
let task = Task::new(TaskId(3), process.id, 7, "Shell", TaskKind::Shell);
|
||||
let mut registry = VmSessionRegistry::new();
|
||||
|
||||
let id = registry.create_for_task(&task, &process, "1.0").expect("session should create");
|
||||
let id = registry
|
||||
.create_for_task(&task, &process, "1.0", None, Arc::new(AtomicU32::new(0)))
|
||||
.expect("session should create");
|
||||
let session = registry.get(id).expect("session should exist");
|
||||
|
||||
assert_eq!(session.app_mode, AppMode::Shell);
|
||||
@@ -174,7 +206,7 @@ mod tests {
|
||||
let mut registry = VmSessionRegistry::new();
|
||||
|
||||
assert_eq!(
|
||||
registry.create_for_task(&task, &process, ""),
|
||||
registry.create_for_task(&task, &process, "", None, Arc::new(AtomicU32::new(0))),
|
||||
Err(VmSessionError::ProcessIsNotVmBacked(process.id))
|
||||
);
|
||||
}
|
||||
@@ -184,7 +216,9 @@ mod tests {
|
||||
let process = Process::new(ProcessId(1), 42, "Stress", ProcessKind::VmGame);
|
||||
let task = Task::new(TaskId(7), process.id, 42, "Stress", TaskKind::Game);
|
||||
let mut registry = VmSessionRegistry::new();
|
||||
registry.create_for_task(&task, &process, "").expect("session should create");
|
||||
registry
|
||||
.create_for_task(&task, &process, "", None, Arc::new(AtomicU32::new(0)))
|
||||
.expect("session should create");
|
||||
|
||||
assert_eq!(
|
||||
registry.resident_game_for_app(42).map(|session| session.task_id),
|
||||
@@ -192,4 +226,52 @@ mod tests {
|
||||
);
|
||||
assert_eq!(registry.resident_game_for_app(43).map(|session| session.task_id), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_runtime_state_is_independent_per_session() {
|
||||
let game_process = Process::new(ProcessId(1), 42, "Stress", ProcessKind::VmGame);
|
||||
let game_task = Task::new(TaskId(7), game_process.id, 42, "Stress", TaskKind::Game);
|
||||
let shell_process = Process::new(ProcessId(2), 9, "Shell", ProcessKind::VmShell);
|
||||
let shell_task = Task::new(TaskId(8), shell_process.id, 9, "Shell", TaskKind::Shell);
|
||||
let mut registry = VmSessionRegistry::new();
|
||||
|
||||
registry
|
||||
.create_for_task(&game_task, &game_process, "", None, Arc::new(AtomicU32::new(0)))
|
||||
.expect("game session should create");
|
||||
registry
|
||||
.create_for_task(&shell_task, &shell_process, "", None, Arc::new(AtomicU32::new(0)))
|
||||
.expect("shell session should create");
|
||||
|
||||
registry
|
||||
.for_task_mut(game_task.id)
|
||||
.expect("game session should exist")
|
||||
.runtime
|
||||
.tick_index = 11;
|
||||
registry
|
||||
.for_task_mut(shell_task.id)
|
||||
.expect("shell session should exist")
|
||||
.runtime
|
||||
.tick_index = 3;
|
||||
registry
|
||||
.for_task_mut(game_task.id)
|
||||
.expect("game session should exist")
|
||||
.open_files
|
||||
.insert(1, "/game.save".to_string());
|
||||
|
||||
assert_eq!(
|
||||
registry.for_task(game_task.id).map(|session| session.runtime.tick_index),
|
||||
Some(11)
|
||||
);
|
||||
assert_eq!(
|
||||
registry.for_task(shell_task.id).map(|session| session.runtime.tick_index),
|
||||
Some(3)
|
||||
);
|
||||
assert!(
|
||||
registry
|
||||
.for_task(shell_task.id)
|
||||
.expect("shell session should exist")
|
||||
.open_files
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user