Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5ac507

Browse files
authored
tests: Improve file_data_sources raciness (graphprotocol#4073)
1 parent 1b66722 commit b5ac507

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

tests/src/fixture.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,9 @@ impl<C: Blockchain> RuntimeAdapter<C> for NoopRuntimeAdapter<C> {
472472
}
473473
}
474474

475-
struct NoopAdapterSelector<C> {
476-
x: PhantomData<C>,
475+
pub struct NoopAdapterSelector<C> {
476+
pub x: PhantomData<C>,
477+
pub triggers_in_block_sleep: Duration,
477478
}
478479

479480
impl<C: Blockchain> TriggersAdapterSelector<C> for NoopAdapterSelector<C> {
@@ -483,12 +484,16 @@ impl<C: Blockchain> TriggersAdapterSelector<C> for NoopAdapterSelector<C> {
483484
_capabilities: &<C as Blockchain>::NodeCapabilities,
484485
_unified_api_version: graph::data::subgraph::UnifiedMappingApiVersion,
485486
) -> Result<Arc<dyn graph::blockchain::TriggersAdapter<C>>, Error> {
486-
Ok(Arc::new(NoopTriggersAdapter { x: PhantomData }))
487+
Ok(Arc::new(NoopTriggersAdapter {
488+
x: PhantomData,
489+
triggers_in_block_sleep: self.triggers_in_block_sleep,
490+
}))
487491
}
488492
}
489493

490494
struct NoopTriggersAdapter<C> {
491495
x: PhantomData<C>,
496+
triggers_in_block_sleep: Duration,
492497
}
493498

494499
#[async_trait]
@@ -516,6 +521,8 @@ impl<C: Blockchain> TriggersAdapter<C> for NoopTriggersAdapter<C> {
516521
block: <C as Blockchain>::Block,
517522
_filter: &<C as Blockchain>::TriggerFilter,
518523
) -> Result<BlockWithTriggers<C>, Error> {
524+
tokio::time::sleep(self.triggers_in_block_sleep).await;
525+
519526
// Return no triggers on data source reprocessing.
520527
Ok(BlockWithTriggers::new(block, Vec::new()))
521528
}

tests/src/fixture/ethereum.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::marker::PhantomData;
22
use std::sync::Arc;
3+
use std::time::Duration;
34

45
use super::{
56
test_ptr, NoopAdapterSelector, NoopRuntimeAdapter, StaticStreamBuilder, Stores, NODE_ID,
67
};
7-
use graph::blockchain::BlockPtr;
8+
use graph::blockchain::{BlockPtr, TriggersAdapterSelector};
89
use graph::cheap_clone::CheapClone;
910
use graph::firehose::{FirehoseEndpoint, FirehoseEndpoints};
1011
use graph::prelude::ethabi::ethereum_types::H256;
@@ -19,6 +20,22 @@ use graph_chain_ethereum::{Chain, ENV_VARS};
1920
use graph_mock::MockMetricsRegistry;
2021

2122
pub async fn chain(blocks: Vec<BlockWithTriggers<Chain>>, stores: &Stores) -> Chain {
23+
chain_with_adapter_selector(
24+
blocks,
25+
stores,
26+
NoopAdapterSelector {
27+
x: PhantomData,
28+
triggers_in_block_sleep: Duration::ZERO,
29+
},
30+
)
31+
.await
32+
}
33+
34+
pub async fn chain_with_adapter_selector(
35+
blocks: Vec<BlockWithTriggers<Chain>>,
36+
stores: &Stores,
37+
adapter_selector: impl TriggersAdapterSelector<Chain> + 'static,
38+
) -> Chain {
2239
let logger = graph::log::logger(true);
2340
let logger_factory = LoggerFactory::new(logger.cheap_clone(), None);
2441
let node_id = NodeId::new(NODE_ID).unwrap();
@@ -49,7 +66,7 @@ pub async fn chain(blocks: Vec<BlockWithTriggers<Chain>>, stores: &Stores) -> Ch
4966
EthereumNetworkAdapters { adapters: vec![] },
5067
stores.chain_head_listener.cheap_clone(),
5168
Arc::new(StaticStreamBuilder { chain: blocks }),
52-
Arc::new(NoopAdapterSelector { x: PhantomData }),
69+
Arc::new(adapter_selector),
5370
Arc::new(NoopRuntimeAdapter { x: PhantomData }),
5471
ENV_VARS.reorg_threshold,
5572
// We assume the tested chain is always ingestible for now

tests/tests/runner.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use std::marker::PhantomData;
12
use std::sync::Arc;
3+
use std::time::Duration;
24

35
use cid::Cid;
46
use graph::blockchain::{Block, BlockPtr};
@@ -9,7 +11,7 @@ use graph::prelude::ethabi::ethereum_types::H256;
911
use graph::prelude::{CheapClone, SubgraphStore};
1012
use graph::prelude::{SubgraphAssignmentProvider, SubgraphName};
1113
use graph_tests::fixture::ethereum::{chain, empty_block, genesis};
12-
use graph_tests::fixture::{self, stores, test_ptr};
14+
use graph_tests::fixture::{self, stores, test_ptr, NoopAdapterSelector};
1315

1416
#[tokio::test]
1517
async fn data_source_revert() -> anyhow::Result<()> {
@@ -142,7 +144,19 @@ async fn file_data_sources() {
142144
vec![block_0, block_1, block_2, block_3, block_4]
143145
};
144146
let stop_block = test_ptr(1);
145-
let chain = Arc::new(chain(blocks, &stores).await);
147+
148+
// This test assumes the file data sources will be processed in the same block in which they are
149+
// created. But the test might fail due to a race condition if for some reason it takes longer
150+
// than expectd to fetch the file from IPFS. The sleep here will conveniently happen after the
151+
// data source is added to the offchain monitor but before the monitor is checked, in an an
152+
// attempt to ensure the monitor has enough time to fetch the file.
153+
let adapter_selector = NoopAdapterSelector {
154+
x: PhantomData,
155+
triggers_in_block_sleep: Duration::from_millis(100),
156+
};
157+
let chain = Arc::new(
158+
fixture::ethereum::chain_with_adapter_selector(blocks, &stores, adapter_selector).await,
159+
);
146160
let ctx = fixture::setup(subgraph_name.clone(), &hash, &stores, chain, None, None).await;
147161
ctx.start_and_sync_to(stop_block).await;
148162

0 commit comments

Comments
 (0)