From 16de5c88b49e5ebdba5a983064d76ee572847d51 Mon Sep 17 00:00:00 2001 From: Lijun Wang <83639177+lijunwangs@users.noreply.github.com> Date: Thu, 7 Sep 2023 11:31:23 -0700 Subject: [PATCH] Improve plugin load/reload unit tests (#33126) * Improve plugin load/reload unit tests * check expectations after reload --- .../src/geyser_plugin_manager.rs | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/geyser-plugin-manager/src/geyser_plugin_manager.rs b/geyser-plugin-manager/src/geyser_plugin_manager.rs index 2bacfbe51871b4..0698cf1a656363 100644 --- a/geyser-plugin-manager/src/geyser_plugin_manager.rs +++ b/geyser-plugin-manager/src/geyser_plugin_manager.rs @@ -324,6 +324,11 @@ pub(crate) fn load_plugin_from_config( Ok((plugin, lib, config_file)) } +#[cfg(test)] +const TESTPLUGIN_CONFIG: &str = "TESTPLUGIN_CONFIG"; +#[cfg(test)] +const TESTPLUGIN2_CONFIG: &str = "TESTPLUGIN2_CONFIG"; + // This is mocked for tests to avoid having to do IO with a dynamically linked library // across different architectures at test time // @@ -331,15 +336,31 @@ pub(crate) fn load_plugin_from_config( /// (The geyser plugin interface requires a &str for the on_load method). #[cfg(test)] pub(crate) fn load_plugin_from_config( - _geyser_plugin_config_file: &Path, + geyser_plugin_config_file: &Path, ) -> Result<(Box, Library, &str), GeyserPluginManagerError> { - Ok(tests::dummy_plugin_and_library(tests::TestPlugin)) + if geyser_plugin_config_file.ends_with(TESTPLUGIN_CONFIG) { + Ok(tests::dummy_plugin_and_library( + tests::TestPlugin, + TESTPLUGIN_CONFIG, + )) + } else if geyser_plugin_config_file.ends_with(TESTPLUGIN2_CONFIG) { + Ok(tests::dummy_plugin_and_library( + tests::TestPlugin2, + TESTPLUGIN2_CONFIG, + )) + } else { + Err(GeyserPluginManagerError::CannotOpenConfigFile( + geyser_plugin_config_file.to_str().unwrap().to_string(), + )) + } } #[cfg(test)] mod tests { use { - crate::geyser_plugin_manager::GeyserPluginManager, + crate::geyser_plugin_manager::{ + GeyserPluginManager, TESTPLUGIN2_CONFIG, TESTPLUGIN_CONFIG, + }, libloading::Library, solana_geyser_plugin_interface::geyser_plugin_interface::GeyserPlugin, std::sync::{Arc, RwLock}, @@ -347,11 +368,12 @@ mod tests { pub(super) fn dummy_plugin_and_library( plugin: P, + config_path: &'static str, ) -> (Box, Library, &'static str) { ( Box::new(plugin), Library::from(libloading::os::unix::Library::this()), - DUMMY_CONFIG, + config_path, ) } @@ -391,7 +413,7 @@ mod tests { ); // Mock having loaded plugin (TestPlugin) - let (mut plugin, lib, config) = dummy_plugin_and_library(TestPlugin); + let (mut plugin, lib, config) = dummy_plugin_and_library(TestPlugin, DUMMY_CONFIG); plugin.on_load(config).unwrap(); plugin_manager_lock.plugins.push(plugin); plugin_manager_lock.libs.push(lib); @@ -408,8 +430,14 @@ mod tests { ); // Now try a (dummy) reload, replacing TestPlugin with TestPlugin2 - let reload_result = plugin_manager_lock.reload_plugin(DUMMY_NAME, DUMMY_CONFIG); + let reload_result = plugin_manager_lock.reload_plugin(DUMMY_NAME, TESTPLUGIN2_CONFIG); assert!(reload_result.is_ok()); + + // The plugin is now replaced with ANOTHER_DUMMY_NAME + let plugins = plugin_manager_lock.list_plugins().unwrap(); + assert!(plugins.iter().any(|name| name.eq(ANOTHER_DUMMY_NAME))); + // DUMMY_NAME should no longer be present. + assert!(!plugins.iter().any(|name| name.eq(DUMMY_NAME))); } #[test] @@ -420,12 +448,12 @@ mod tests { // Load two plugins // First - let (mut plugin, lib, config) = dummy_plugin_and_library(TestPlugin); + let (mut plugin, lib, config) = dummy_plugin_and_library(TestPlugin, TESTPLUGIN_CONFIG); plugin.on_load(config).unwrap(); plugin_manager_lock.plugins.push(plugin); plugin_manager_lock.libs.push(lib); // Second - let (mut plugin, lib, config) = dummy_plugin_and_library(TestPlugin2); + let (mut plugin, lib, config) = dummy_plugin_and_library(TestPlugin2, TESTPLUGIN2_CONFIG); plugin.on_load(config).unwrap(); plugin_manager_lock.plugins.push(plugin); plugin_manager_lock.libs.push(lib); @@ -443,7 +471,7 @@ mod tests { let mut plugin_manager_lock = plugin_manager.write().unwrap(); // Load rpc call - let load_result = plugin_manager_lock.load_plugin(DUMMY_CONFIG); + let load_result = plugin_manager_lock.load_plugin(TESTPLUGIN_CONFIG); assert!(load_result.is_ok()); assert_eq!(plugin_manager_lock.plugins.len(), 1);