implements PLN-0149
This commit is contained in:
@@ -2,6 +2,7 @@ use crate::os::LifecycleError;
|
||||
use crate::os::SystemOS;
|
||||
use crate::task::TaskId;
|
||||
use crate::vm_session::{VmSession, VmSessionError};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct SessionsFacade<'a> {
|
||||
pub(in crate::os) os: &'a mut SystemOS,
|
||||
@@ -62,6 +63,10 @@ impl<'a> SessionsFacade<'a> {
|
||||
self.os.vm_sessions.for_task(task_id)
|
||||
}
|
||||
|
||||
pub fn vm_session_for_task_mut(&mut self, task_id: TaskId) -> Option<&mut VmSession> {
|
||||
self.os.vm_sessions.for_task_mut(task_id)
|
||||
}
|
||||
|
||||
pub fn resident_game_session_for_app(&self, app_id: u32) -> Option<&VmSession> {
|
||||
self.os.vm_sessions.resident_game_for_app(app_id)
|
||||
}
|
||||
@@ -79,6 +84,11 @@ impl<'a> SessionsFacade<'a> {
|
||||
.expect("session process should exist")
|
||||
.clone();
|
||||
|
||||
self.os.vm_sessions.create_for_task(&task, &process, "").map(|_| ())
|
||||
let cap_config = self.os.vm_runtime.certifier.config;
|
||||
let logs_count = Arc::clone(&self.os.log_service.logs_count);
|
||||
self.os
|
||||
.vm_sessions
|
||||
.create_for_task(&task, &process, "", Some(cap_config), logs_count)
|
||||
.map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,6 +259,42 @@ mod tests {
|
||||
assert_eq!(sessions.vm_session_count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vm_sessions_hold_independent_runtime_state() {
|
||||
let mut os = SystemOS::new(None);
|
||||
let game = os.sessions().create_vm_game_task(42, "Sector Crawl");
|
||||
os.lifecycle().request_home_from_game(game).expect("home request should succeed");
|
||||
os.lifecycle().advance_game_pause_budget(game).expect("budget should expire");
|
||||
let shell = os.sessions().create_vm_shell_task(7, "Settings");
|
||||
|
||||
{
|
||||
let mut sessions = os.sessions();
|
||||
let game_session =
|
||||
sessions.vm_session_for_task_mut(game).expect("game VM session should exist");
|
||||
game_session.runtime.tick_index = 100;
|
||||
game_session.open_files.insert(1, "/game.dat".to_string());
|
||||
}
|
||||
|
||||
{
|
||||
let mut sessions = os.sessions();
|
||||
let shell_session =
|
||||
sessions.vm_session_for_task_mut(shell).expect("shell VM session should exist");
|
||||
shell_session.runtime.tick_index = 5;
|
||||
shell_session.open_files.insert(1, "/shell.dat".to_string());
|
||||
}
|
||||
|
||||
let sessions = os.sessions();
|
||||
let game_session =
|
||||
sessions.vm_session_for_task(game).expect("game VM session should exist");
|
||||
let shell_session =
|
||||
sessions.vm_session_for_task(shell).expect("shell VM session should exist");
|
||||
|
||||
assert_eq!(game_session.runtime.tick_index, 100);
|
||||
assert_eq!(shell_session.runtime.tick_index, 5);
|
||||
assert_eq!(game_session.open_files.get(&1).map(String::as_str), Some("/game.dat"));
|
||||
assert_eq!(shell_session.open_files.get(&1).map(String::as_str), Some("/shell.dat"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn home_request_notifies_game_before_budget_suspends_it() {
|
||||
let mut os = SystemOS::new(None);
|
||||
|
||||
@@ -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