Skip to content

Commit

Permalink
Improve plugin load/reload unit tests (solana-labs#33126)
Browse files Browse the repository at this point in the history
* Improve plugin load/reload unit tests
* check expectations after reload
  • Loading branch information
lijunwangs authored and pull[bot] committed Dec 26, 2023
1 parent 8d4072a commit 16de5c8
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions geyser-plugin-manager/src/geyser_plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,34 +324,56 @@ 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
//
/// This returns mocked values for the geyser plugin, the dynamic library, and the parsed config file as a &str.
/// (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<dyn GeyserPlugin>, 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},
};

pub(super) fn dummy_plugin_and_library<P: GeyserPlugin>(
plugin: P,
config_path: &'static str,
) -> (Box<dyn GeyserPlugin>, Library, &'static str) {
(
Box::new(plugin),
Library::from(libloading::os::unix::Library::this()),
DUMMY_CONFIG,
config_path,
)
}

Expand Down Expand Up @@ -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);
Expand All @@ -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]
Expand All @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 16de5c8

Please sign in to comment.