diff options
author | Aram Drevekenin <aram@poor.dev> | 2023-05-16 00:37:12 +0200 |
---|---|---|
committer | Aram Drevekenin <aram@poor.dev> | 2023-05-16 00:37:12 +0200 |
commit | d726323055bfca37abdb1c661e97f68c81840161 (patch) | |
tree | 8d3002f78d7049684a9ca4d33025779f8ff4a852 | |
parent | 00b7d893729f9d4d28f1536be8002ca476a09936 (diff) |
add test for plugin worker persistence
-rw-r--r-- | default-plugins/fixture-plugin-for-tests/src/main.rs | 7 | ||||
-rw-r--r-- | zellij-server/src/plugins/plugin_loader.rs | 3 | ||||
-rw-r--r-- | zellij-server/src/plugins/plugin_map.rs | 9 | ||||
-rw-r--r-- | zellij-server/src/plugins/unit/plugin_tests.rs | 69 | ||||
-rw-r--r-- | zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers.snap | 4 | ||||
-rw-r--r-- | zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers_persist_state.snap | 12 | ||||
-rw-r--r-- | zellij-server/src/plugins/wasm_bridge.rs | 1 | ||||
-rwxr-xr-x | zellij-utils/assets/plugins/compact-bar.wasm | bin | 796375 -> 791665 bytes | |||
-rwxr-xr-x | zellij-utils/assets/plugins/fixture-plugin-for-tests.wasm | bin | 729883 -> 732287 bytes | |||
-rwxr-xr-x | zellij-utils/assets/plugins/status-bar.wasm | bin | 921497 -> 920797 bytes | |||
-rwxr-xr-x | zellij-utils/assets/plugins/strider.wasm | bin | 908672 -> 916361 bytes | |||
-rwxr-xr-x | zellij-utils/assets/plugins/tab-bar.wasm | bin | 765594 -> 762202 bytes |
12 files changed, 88 insertions, 17 deletions
diff --git a/default-plugins/fixture-plugin-for-tests/src/main.rs b/default-plugins/fixture-plugin-for-tests/src/main.rs index e46086ca3..486f4ea93 100644 --- a/default-plugins/fixture-plugin-for-tests/src/main.rs +++ b/default-plugins/fixture-plugin-for-tests/src/main.rs @@ -12,12 +12,15 @@ struct State { } #[derive(Default, Serialize, Deserialize)] -struct TestWorker {} +struct TestWorker { + number_of_messages_received: usize +} impl<'de> ZellijWorker<'de> for TestWorker { fn on_message(&mut self, message: String, payload: String) { if message == "ping" { - post_message_to_plugin("pong".into(), payload); + self.number_of_messages_received += 1; + post_message_to_plugin("pong".into(), format!("{}, received {} messages", payload, self.number_of_messages_received)); } } } diff --git a/zellij-server/src/plugins/plugin_loader.rs b/zellij-server/src/plugins/plugin_loader.rs index f207e7a16..5e98a2bfd 100644 --- a/zellij-server/src/plugins/plugin_loader.rs +++ b/zellij-server/src/plugins/plugin_loader.rs @@ -438,10 +438,7 @@ impl<'a> PluginLoader<'a> { let plugin_map = plugin_map.lock().unwrap(); plugin_map .get_running_plugin(plugin_id, None) - // .iter() - // .find(|((p_id, _c_id), _)| p_id == &plugin_id) .with_context(err_context)? - // .1 .clone() }; let running_plugin = running_plugin.lock().unwrap(); diff --git a/zellij-server/src/plugins/plugin_map.rs b/zellij-server/src/plugins/plugin_map.rs index 3e7efd8ba..0c3df931e 100644 --- a/zellij-server/src/plugins/plugin_map.rs +++ b/zellij-server/src/plugins/plugin_map.rs @@ -24,7 +24,6 @@ use zellij_utils::{ // them without blocking other instances #[derive(Default)] pub struct PluginMap { - // TODO: types plugin_assets: HashMap< (PluginId, ClientId), ( @@ -56,7 +55,6 @@ impl PluginMap { } removed } - // TODO: CONTINUE HERE - implement these pub fn remove_single_plugin( &mut self, plugin_id: PluginId, @@ -259,18 +257,11 @@ impl RunningPlugin { } } -// TODO: CONTINUE HERE (30/04) - implement this and then populate it for Strider and then get the message -// to pass pub struct RunningWorker { pub instance: Instance, pub name: String, pub plugin_config: PluginConfig, pub plugin_env: PluginEnv, - // pub plugin_env: PluginEnv, - // pub rows: usize, - // pub columns: usize, - // next_event_ids: HashMap<AtomicEvent, usize>, - // last_applied_event_ids: HashMap<AtomicEvent, usize>, } impl RunningWorker { diff --git a/zellij-server/src/plugins/unit/plugin_tests.rs b/zellij-server/src/plugins/unit/plugin_tests.rs index b8ac6d45e..d1e79a5fe 100644 --- a/zellij-server/src/plugins/unit/plugin_tests.rs +++ b/zellij-server/src/plugins/unit/plugin_tests.rs @@ -249,3 +249,72 @@ pub fn plugin_workers() { }); assert_snapshot!(format!("{:#?}", plugin_bytes_event)); } + +#[test] +#[ignore] +pub fn plugin_workers_persist_state() { + let (plugin_thread_sender, screen_receiver, mut teardown) = create_plugin_thread(); + let plugin_should_float = Some(false); + let plugin_title = Some("test_plugin".to_owned()); + let run_plugin = RunPlugin { + _allow_exec_host_cmd: false, + location: RunPluginLocation::File(PathBuf::from(&*PLUGIN_FIXTURE)), + }; + let tab_index = 1; + let client_id = 1; + let size = Size { + cols: 121, + rows: 20, + }; + let received_screen_instructions = Arc::new(Mutex::new(vec![])); + let screen_thread = log_actions_in_thread!( + received_screen_instructions, + ScreenInstruction::PluginBytes, + screen_receiver, + 5 + ); + + let _ = plugin_thread_sender.send(PluginInstruction::AddClient(client_id)); + let _ = plugin_thread_sender.send(PluginInstruction::Load( + plugin_should_float, + plugin_title, + run_plugin, + tab_index, + client_id, + size, + )); + // we send a SystemClipboardFailure to trigger the custom handler in the fixture plugin that + // will send a message to the worker and in turn back to the plugin to be rendered, so we know + // that this cycle is working + // we do this a second time so that the worker will log the first message on its own state and + // then send us the "received 2 messages" indication we check for below, letting us know it + // managed to persist its own state and act upon it + let _ = plugin_thread_sender.send(PluginInstruction::Update(vec![( + None, + Some(client_id), + Event::SystemClipboardFailure, + )])); + let _ = plugin_thread_sender.send(PluginInstruction::Update(vec![( + None, + Some(client_id), + Event::SystemClipboardFailure, + )])); + screen_thread.join().unwrap(); // this might take a while if the cache is cold + teardown(); + let plugin_bytes_event = received_screen_instructions + .lock() + .unwrap() + .iter() + .find_map(|i| { + if let ScreenInstruction::PluginBytes(plugin_bytes) = i { + for (plugin_id, client_id, plugin_bytes) in plugin_bytes { + let plugin_bytes = String::from_utf8_lossy(plugin_bytes).to_string(); + if plugin_bytes.contains("received 2 messages") { + return Some((*plugin_id, *client_id, plugin_bytes)); + } + } + } + None + }); + assert_snapshot!(format!("{:#?}", plugin_bytes_event)); +} diff --git a/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers.snap b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers.snap index 4aeccc31d..91417b768 100644 --- a/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers.snap +++ b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers.snap @@ -1,12 +1,12 @@ --- source: zellij-server/src/plugins/./unit/plugin_tests.rs -assertion_line: 782 +assertion_line: 250 expression: "format!(\"{:#?}\", plugin_bytes_event)" --- Some( ( 0, 1, - "Payload from worker: \"gimme_back_my_payload\"\n\r", + "Payload from worker: \"gimme_back_my_payload, received 1 messages\"\n\r", ), ) diff --git a/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers_persist_state.snap b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers_persist_state.snap new file mode 100644 index 000000000..51693505d --- /dev/null +++ b/zellij-server/src/plugins/unit/snapshots/zellij_server__plugins__plugin_tests__plugin_workers_persist_state.snap @@ -0,0 +1,12 @@ +--- +source: zellij-server/src/plugins/./unit/plugin_tests.rs +assertion_line: 319 +expression: "format!(\"{:#?}\", plugin_bytes_event)" +--- +Some( + ( + 0, + 1, + "Payload from worker: \"gimme_back_my_payload, received 2 messages\"\n\r", + ), +) diff --git a/zellij-server/src/plugins/wasm_bridge.rs b/zellij-server/src/plugins/wasm_bridge.rs index 67b825225..5d180b7d9 100644 --- a/zellij-server/src/plugins/wasm_bridge.rs +++ b/zellij-server/src/plugins/wasm_bridge.rs @@ -158,7 +158,6 @@ impl WasmBridge { Ok(plugin_id) } pub fn unload_plugin(&mut self, pid: PluginId) -> Result<()> { - // TODO: these should also be called on Zellij exit info!("Bye from plugin {}", &pid); let mut plugin_map = self.plugin_map.lock().unwrap(); for (running_plugin, _, _) in plugin_map.remove_plugins(pid) { diff --git a/zellij-utils/assets/plugins/compact-bar.wasm b/zellij-utils/assets/plugins/compact-bar.wasm Binary files differindex c84978068..3145fe1da 100755 --- a/zellij-utils/assets/plugins/compact-bar.wasm +++ b/zellij-utils/assets/plugins/compact-bar.wasm diff --git a/zellij-utils/assets/plugins/fixture-plugin-for-tests.wasm b/zellij-utils/assets/plugins/fixture-plugin-for-tests.wasm Binary files differindex 8e9b4dfaa..a5421d720 100755 --- a/zellij-utils/assets/plugins/fixture-plugin-for-tests.wasm +++ b/zellij-utils/assets/plugins/fixture-plugin-for-tests.wasm diff --git a/zellij-utils/assets/plugins/status-bar.wasm b/zellij-utils/assets/plugins/status-bar.wasm Binary files differindex 68598ddd6..8f7ed7826 100755 --- a/zellij-utils/assets/plugins/status-bar.wasm +++ b/zellij-utils/assets/plugins/status-bar.wasm diff --git a/zellij-utils/assets/plugins/strider.wasm b/zellij-utils/assets/plugins/strider.wasm Binary files differindex 9b53678c3..929f5e8c7 100755 --- a/zellij-utils/assets/plugins/strider.wasm +++ b/zellij-utils/assets/plugins/strider.wasm diff --git a/zellij-utils/assets/plugins/tab-bar.wasm b/zellij-utils/assets/plugins/tab-bar.wasm Binary files differindex 4eb6d31cd..a8742f676 100755 --- a/zellij-utils/assets/plugins/tab-bar.wasm +++ b/zellij-utils/assets/plugins/tab-bar.wasm |