Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mm2src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "common.rs"
doctest = false

[features]
for-tests = []
track-ctx-pointer = ["shared_ref_counter/enable", "shared_ref_counter/log"]

[dependencies]
Expand Down
10 changes: 10 additions & 0 deletions mm2src/common/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,18 @@ impl<Id> Default for PagingOptionsEnum<Id> {
}

#[inline(always)]
#[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()
}

Copy link
Copy Markdown

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)

Copy link
Copy Markdown
Collaborator Author

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))

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Collaborator Author

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)

Copy link
Copy Markdown

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.

Comment thread
borngraced marked this conversation as resolved.
#[inline(always)]
pub fn get_utc_timestamp_nanos() -> i64 { Utc::now().timestamp_nanos() }

Expand Down
1 change: 1 addition & 0 deletions mm2src/mm2_main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ winapi = "0.3"
[dev-dependencies]
coins = { path = "../coins", features = ["for-tests"] }
coins_activation = { path = "../coins_activation", features = ["for-tests"] }
common = { path = "../common", features = ["for-tests"] }
mm2_test_helpers = { path = "../mm2_test_helpers" }
trading_api = { path = "../trading_api", features = ["mocktopus"] }
mocktopus = "0.8.0"
Expand Down
93 changes: 93 additions & 0 deletions mm2src/mm2_main/tests/docker_tests/docker_tests_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5471,3 +5471,96 @@ fn test_approve_erc20() {

block_on(mm.stop()).unwrap();
}

#[test]
fn test_peer_time_sync_validation() {
const TIMEOFFSET_TOLERABLE: i64 = 19;
const TIMEOFFSET_TOO_BIG: i64 = 21;

Copy link
Copy Markdown

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());

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 3c6ac2a


let start_peers_with_time_offset = |offset: i64| -> (Json, Json) {
let (_ctx, _, bob_priv_key) = generate_utxo_coin_with_random_privkey("MYCOIN", 10.into());
let (_ctx, _, alice_priv_key) = generate_utxo_coin_with_random_privkey("MYCOIN1", 10.into());
let coins = json!([mycoin_conf(1000), mycoin1_conf(1000)]);
let mut mm_bob = block_on(MarketMakerIt::start_with_envs(
json!({
"gui": "nogui",
"netid": 9000,
"dht": "on", // Enable DHT without delay.
"passphrase": format!("0x{}", hex::encode(bob_priv_key)),
"coins": coins,
"rpc_password": "pass",
"i_am_seed": true,
}),
"pass".to_string(),
None,
&[],
))
.unwrap();
let (_bob_dump_log, _bob_dump_dashboard) = mm_dump(&mm_bob.log_path);
block_on(mm_bob.wait_for_log(22., |log| log.contains(">>>>>>>>> DEX stats "))).unwrap();

let mut mm_alice = block_on(MarketMakerIt::start_with_envs(
json!({
"gui": "nogui",
"netid": 9000,
"dht": "on", // Enable DHT without delay.
"passphrase": format!("0x{}", hex::encode(alice_priv_key)),
"coins": coins,
"rpc_password": "pass",
"seednodes": vec![format!("{}", mm_bob.ip)],
}),
Comment thread
onur-ozkan marked this conversation as resolved.
Outdated
"pass".to_string(),
None,
&[("TEST_TIMESTAMP_OFFSET", offset.to_string().as_str())],
))
.unwrap();
let (_alice_dump_log, _alice_dump_dashboard) = mm_dump(&mm_alice.log_path);
block_on(mm_alice.wait_for_log(22., |log| log.contains(">>>>>>>>> DEX stats "))).unwrap();

let res_bob = block_on(mm_bob.rpc(&json!({
"userpass": mm_bob.userpass,
"method": "get_directly_connected_peers",
})))
.unwrap();
assert!(res_bob.0.is_success(), "!get_directly_connected_peers: {}", res_bob.1);
let bob_peers = serde_json::from_str::<Json>(&res_bob.1).unwrap();

let res_alice = block_on(mm_alice.rpc(&json!({
"userpass": mm_alice.userpass,
"method": "get_directly_connected_peers",
})))
.unwrap();
assert!(
res_alice.0.is_success(),
"!get_directly_connected_peers: {}",
res_alice.1
);
let alice_peers = serde_json::from_str::<Json>(&res_alice.1).unwrap();

block_on(mm_bob.stop()).unwrap();
block_on(mm_alice.stop()).unwrap();
(bob_peers, alice_peers)
};

// check with small time offset:
let (bob_peers, alice_peers) = start_peers_with_time_offset(TIMEOFFSET_TOLERABLE);
assert!(
bob_peers["result"].as_object().unwrap().len() == 1,
"bob must have one peer"
);
assert!(
alice_peers["result"].as_object().unwrap().len() == 1,
"alice must have one peer"
);

// check with too big time offset:
let (bob_peers, alice_peers) = start_peers_with_time_offset(TIMEOFFSET_TOO_BIG);
assert!(
bob_peers["result"].as_object().unwrap().is_empty(),
"bob must have no peers"
);
assert!(
alice_peers["result"].as_object().unwrap().is_empty(),
"alice must have no peers"
);
}
1 change: 1 addition & 0 deletions mm2src/mm2_p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ timed-map = { version = "1.1.1", features = ["rustc-hash"] }
[dev-dependencies]
async-std = "1.6.2"
env_logger = "0.9.3"
common = { path = "../common", features = ["for-tests"] }