-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(P2P): add test for peer time sync validation #2304
Conversation
mm2src/common/common.rs
Outdated
#[cfg(not(feature = "for-tests"))] | ||
pub fn get_utc_timestamp() -> i64 { Utc::now().timestamp() } | ||
|
||
/// get_utc_timestamp for tests allowing to add some bias to 'now' | ||
#[cfg(feature = "for-tests")] | ||
pub fn get_utc_timestamp() -> i64 { | ||
Utc::now().timestamp() | ||
+ std::env::var("TEST_TIMESTAMP_OFFSET") | ||
.map(|s| s.as_str().parse::<i64>().unwrap_or_default()) | ||
.unwrap_or_default() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use built-in cfg(test)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to call it for other crates and I believe cfg(test) is in effect only for unit tests in same crate
(checked btw - test does not work with cfg(test))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is run-docker-tests
feature too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"for-tests" feature is also used already and looks more general for me (not specific for docker tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I see you just added for-tests
feature, idk how it's already there but it's fine, I will make cfg(test)
working and replace all these test flags in another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one note
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM other than last nit
#[test] | ||
fn test_peer_time_sync_validation() { | ||
const TIMEOFFSET_TOLERABLE: i64 = 19; | ||
const TIMEOFFSET_TOO_BIG: i64 = 21; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can stabilize this logic a bit more here:
diff --git a/mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs b/mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
index acf60ee55..e2dcd228f 100644
--- a/mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
+++ b/mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
@@ -16,6 +16,7 @@ use common::{block_on, block_on_f01, executor::Timer, get_utc_timestamp, now_sec
use crypto::privkey::key_pair_from_seed;
use crypto::{CryptoCtx, DerivationPath, KeyPairPolicy};
use http::StatusCode;
+use mm2_libp2p::behaviours::atomicdex::MAX_TIME_GAP_FOR_CONNECTED_PEER;
use mm2_number::{BigDecimal, BigRational, MmNumber};
use mm2_test_helpers::for_tests::{check_my_swap_status_amounts, disable_coin, disable_coin_err, enable_eth_coin,
enable_eth_with_tokens_v2, erc20_dev_conf, eth_dev_conf, get_locked_amount,
@@ -5474,8 +5475,8 @@ fn test_approve_erc20() {
#[test]
fn test_peer_time_sync_validation() {
- const TIMEOFFSET_TOLERABLE: i64 = 19;
- const TIMEOFFSET_TOO_BIG: i64 = 21;
+ const TIMEOFFSET_TOLERABLE: i64 = MAX_TIME_GAP_FOR_CONNECTED_PEER - 1;
+ const TIMEOFFSET_TOO_BIG: i64 = MAX_TIME_GAP_FOR_CONNECTED_PEER + 1;
let start_peers_with_time_offset = |offset: i64| -> (Json, Json) {
let (_ctx, _, bob_priv_key) = generate_utxo_coin_with_random_privkey("MYCOIN", 10.into());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 3c6ac2a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, feel free to merge with/without the nit.
let timeoffset_tolerable = TryInto::<i64>::try_into(MAX_TIME_GAP_FOR_CONNECTED_PEER).unwrap() - 1; | ||
let timeoffset_too_big = TryInto::<i64>::try_into(MAX_TIME_GAP_FOR_CONNECTED_PEER).unwrap() + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: casting to i64
with TryInto
s and unwrap
s seems unnecessary since we are only using this value to convert it into a String
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intended to have it as i64 to indicate it's possible to emulate time deviation in both directions, if anyone needs
While doing review for peer time sync validation #2255 code I wrote a test for that.
(Attn: @onur-ozkan)