From ee9c679d41457b52195064d6b357a2239f476812 Mon Sep 17 00:00:00 2001 From: teor Date: Mon, 25 Apr 2022 10:24:29 +1000 Subject: [PATCH] Turn a function argument into an enum field --- zebrad/tests/acceptance.rs | 27 +++++++++++---------- zebrad/tests/common/lightwalletd.rs | 37 +++++++++++++++++++---------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/zebrad/tests/acceptance.rs b/zebrad/tests/acceptance.rs index 61774ba8b5e..df07ca88b43 100644 --- a/zebrad/tests/acceptance.rs +++ b/zebrad/tests/acceptance.rs @@ -998,7 +998,7 @@ async fn rpc_endpoint() -> Result<()> { #[test] #[cfg(not(target_os = "windows"))] fn lightwalletd_integration() -> Result<()> { - lightwalletd_integration_test(LaunchWithEmptyState, false) + lightwalletd_integration_test(LaunchWithEmptyState) } /// Make sure `lightwalletd` can sync from Zebra, in update sync mode. @@ -1013,7 +1013,7 @@ fn lightwalletd_integration() -> Result<()> { #[test] #[cfg(not(target_os = "windows"))] fn lightwalletd_update_sync() -> Result<()> { - lightwalletd_integration_test(UpdateCachedState, false) + lightwalletd_integration_test(UpdateCachedState) } /// Make sure `lightwalletd` can fully sync from genesis using Zebra. @@ -1026,7 +1026,9 @@ fn lightwalletd_update_sync() -> Result<()> { #[ignore] #[cfg(not(target_os = "windows"))] fn lightwalletd_full_sync() -> Result<()> { - lightwalletd_integration_test(FullSyncFromGenesis, false) + lightwalletd_integration_test(FullSyncFromGenesis { + allow_lightwalletd_cached_state: false, + }) } /// Make sure `lightwalletd` can sync from Zebra, in all available modes. @@ -1041,14 +1043,16 @@ fn lightwalletd_full_sync() -> Result<()> { #[ignore] #[cfg(not(target_os = "windows"))] fn lightwalletd_test_suite() -> Result<()> { - lightwalletd_integration_test(LaunchWithEmptyState, false)?; + lightwalletd_integration_test(LaunchWithEmptyState)?; // Only runs when ZEBRA_CACHED_STATE_PATH is set. // When manually running the test suite, allow cached state in the full sync test. - lightwalletd_integration_test(FullSyncFromGenesis, true)?; + lightwalletd_integration_test(FullSyncFromGenesis { + allow_lightwalletd_cached_state: true, + })?; // Only runs when LIGHTWALLETD_DATA_DIR and ZEBRA_CACHED_STATE_PATH are set - lightwalletd_integration_test(UpdateCachedState, false)?; + lightwalletd_integration_test(UpdateCachedState)?; Ok(()) } @@ -1059,10 +1063,7 @@ fn lightwalletd_test_suite() -> Result<()> { /// /// The random ports in this test can cause [rare port conflicts.](#Note on port conflict) #[cfg(not(target_os = "windows"))] -fn lightwalletd_integration_test( - test_type: LightwalletdTestType, - allow_cached_state_for_full_sync: bool, -) -> Result<()> { +fn lightwalletd_integration_test(test_type: LightwalletdTestType) -> Result<()> { zebra_test::init(); // Skip the test unless the user specifically asked for it @@ -1106,7 +1107,7 @@ fn lightwalletd_integration_test( let (zebrad_failure_messages, zebrad_ignore_messages) = test_type.zebrad_failure_messages(); let (lightwalletd_failure_messages, lightwalletd_ignore_messages) = - test_type.lightwalletd_failure_messages(allow_cached_state_for_full_sync); + test_type.lightwalletd_failure_messages(); // Launch zebrad let zdir = testdir()?.with_exact_config(&config)?; @@ -1164,7 +1165,7 @@ fn lightwalletd_integration_test( if test_type.needs_lightwalletd_cached_state() { // TODO: expect `[0-9]{7}` when we're using the tip cached state (#4155) lightwalletd.expect_stdout_line_matches("Found [0-9]{6,7} blocks in cache")?; - } else if !allow_cached_state_for_full_sync { + } else if test_type.allow_lightwalletd_cached_state() { // Timeout the test if we're somehow accidentally using a cached state in our temp dir lightwalletd.expect_stdout_line_matches("Found 0 blocks in cache")?; } @@ -1194,7 +1195,7 @@ fn lightwalletd_integration_test( ))?; } - if test_type == UpdateCachedState || test_type == FullSyncFromGenesis { + if matches!(test_type, UpdateCachedState | FullSyncFromGenesis { .. }) { // Wait for Zebra to sync its cached state to the chain tip zebrad.expect_stdout_line_matches(regex::escape("sync_percent=100"))?; diff --git a/zebrad/tests/common/lightwalletd.rs b/zebrad/tests/common/lightwalletd.rs index 61180bf54fe..6f3cb310112 100644 --- a/zebrad/tests/common/lightwalletd.rs +++ b/zebrad/tests/common/lightwalletd.rs @@ -215,12 +215,17 @@ pub enum LightwalletdTestType { /// Do a full sync from an empty lightwalletd state. /// - /// This test is much faster if it has a cached Zebra state. - FullSyncFromGenesis, + /// This test requires a cached Zebra state. + FullSyncFromGenesis { + /// Should the test allow a cached lightwalletd state? + /// + /// If `false`, the test fails if the lightwalletd state is populated. + allow_lightwalletd_cached_state: bool, + }, /// Sync to tip from a lightwalletd cached state. /// - /// This test is much faster if it has a cached Zebra state. + /// This test requires a cached Zebra and lightwalletd state. UpdateCachedState, } @@ -229,14 +234,25 @@ impl LightwalletdTestType { pub fn needs_zebra_cached_state(&self) -> bool { match self { LaunchWithEmptyState => false, - FullSyncFromGenesis | UpdateCachedState => true, + FullSyncFromGenesis { .. } | UpdateCachedState => true, } } /// Does this test need a lightwalletd cached state? pub fn needs_lightwalletd_cached_state(&self) -> bool { match self { - LaunchWithEmptyState | FullSyncFromGenesis => false, + LaunchWithEmptyState | FullSyncFromGenesis { .. } => false, + UpdateCachedState => true, + } + } + + /// Does this test allow a lightwalletd cached state, even if it is not required? + pub fn allow_lightwalletd_cached_state(&self) -> bool { + match self { + LaunchWithEmptyState => false, + FullSyncFromGenesis { + allow_lightwalletd_cached_state, + } => *allow_lightwalletd_cached_state, UpdateCachedState => true, } } @@ -289,7 +305,7 @@ impl LightwalletdTestType { pub fn zebrad_timeout(&self) -> Duration { match self { LaunchWithEmptyState => LIGHTWALLETD_DELAY, - FullSyncFromGenesis | UpdateCachedState => LIGHTWALLETD_UPDATE_TIP_DELAY, + FullSyncFromGenesis { .. } | UpdateCachedState => LIGHTWALLETD_UPDATE_TIP_DELAY, } } @@ -298,7 +314,7 @@ impl LightwalletdTestType { match self { LaunchWithEmptyState => LIGHTWALLETD_DELAY, UpdateCachedState => LIGHTWALLETD_UPDATE_TIP_DELAY, - FullSyncFromGenesis => LIGHTWALLETD_FULL_SYNC_TIP_DELAY, + FullSyncFromGenesis { .. } => LIGHTWALLETD_FULL_SYNC_TIP_DELAY, } } @@ -328,10 +344,7 @@ impl LightwalletdTestType { /// Returns `lightwalletd` log regexes that indicate the tests have failed, /// and regexes of any failures that should be ignored. - pub fn lightwalletd_failure_messages( - &self, - allow_cached_state_for_full_sync: bool, - ) -> (Vec, Vec) { + pub fn lightwalletd_failure_messages(&self) -> (Vec, Vec) { let mut lightwalletd_failure_messages: Vec = LIGHTWALLETD_FAILURE_MESSAGES .iter() .chain(PROCESS_FAILURE_MESSAGES) @@ -347,7 +360,7 @@ impl LightwalletdTestType { lightwalletd_failure_messages .push("Got sapling height 419200 block height [0-9]{1,6} chain main".to_string()); } - if *self == FullSyncFromGenesis && !allow_cached_state_for_full_sync { + if !self.allow_lightwalletd_cached_state() { // Fail if we need an empty lightwalletd state, but it has blocks lightwalletd_failure_messages .push("Got sapling height 419200 block height [1-9][0-9]* chain main".to_string());