Skip to content

Commit 9f0ce51

Browse files
committed
store: Make test_clear_stale_call_cache in sharded setup
The test assumed that the chain is in the primary, which is not necessarily true when tests are run against a sharded setup
1 parent f54467a commit 9f0ce51

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

store/postgres/src/chain_store.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,14 @@ impl ChainStore {
23052305
})
23062306
.await
23072307
}
2308+
2309+
/// Helper for tests that need to directly modify the tables for the
2310+
/// chain store
2311+
#[cfg(debug_assertions)]
2312+
pub async fn get_conn_for_test(&self) -> Result<AsyncPgConnection, Error> {
2313+
let conn = self.pool.get().await?;
2314+
Ok(conn)
2315+
}
23082316
}
23092317

23102318
fn json_block_to_block_ptr_ext(json_block: &JsonBlock) -> Result<ExtendedBlockPtr, Error> {

store/test-store/tests/postgres/chain_head.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,6 @@ fn test_clear_stale_call_cache() {
546546
let call: [u8; 6] = [1, 2, 3, 4, 5, 6];
547547
let return_value: [u8; 3] = [7, 8, 9];
548548

549-
let mut conn = PRIMARY_POOL.get().await.unwrap();
550-
551549
// Insert a call cache entry, otherwise it will hit an early return and won't test all queries
552550
let call = call::Request::new(address, call.to_vec(), 0);
553551
chain_store
@@ -569,30 +567,37 @@ fn test_clear_stale_call_cache() {
569567
assert!(ret.is_some());
570568

571569
// Now we need to update the accessed_at timestamp to be stale, so it gets deleted
572-
// Get namespace from chains table
573-
let namespace: String = diesel::sql_query(format!(
574-
"SELECT namespace FROM public.chains WHERE name = '{}'",
575-
chain_store.chain
576-
))
577-
.get_result::<Namespace>(&mut conn)
578-
.await
579-
.unwrap()
580-
.namespace;
570+
// Get namespace from chains table in the primary
571+
let namespace: String = {
572+
let mut conn = PRIMARY_POOL.get().await.unwrap();
573+
diesel::sql_query(format!(
574+
"SELECT namespace FROM public.chains WHERE name = '{}'",
575+
chain_store.chain
576+
))
577+
.get_result::<Namespace>(&mut conn)
578+
.await
579+
.unwrap()
580+
.namespace
581+
};
581582

582583
// Determine the correct meta table name
583584
let meta_table: String = match namespace.as_str() {
584585
"public" => "eth_call_meta".to_owned(),
585586
_ => format!("{namespace}.call_meta"),
586587
};
587588

588-
// Update accessed_at to be 8 days ago, so it's stale for a 7 day threshold
589-
let _ = diesel::sql_query(format!(
590-
"UPDATE {meta_table} SET accessed_at = NOW() - INTERVAL '8 days' WHERE contract_address = $1"
591-
)).bind::<diesel::sql_types::Bytea, _>(address.as_bytes())
592-
.execute(&mut conn)
593-
.await
594-
.unwrap();
595-
589+
// Update accessed_at to be 8 days ago, so it's stale for a 7 day
590+
// threshold in the shard where the chain lives
591+
{
592+
let mut conn = chain_store.get_conn_for_test().await.unwrap();
593+
diesel::sql_query(format!(
594+
"UPDATE {meta_table} SET accessed_at = NOW() - INTERVAL '8 days' WHERE contract_address = $1"
595+
))
596+
.bind::<diesel::sql_types::Bytea, _>(address.as_bytes())
597+
.execute(&mut conn)
598+
.await
599+
.unwrap();
600+
}
596601
let result = chain_store.clear_stale_call_cache(7, None).await;
597602
assert!(result.is_ok());
598603

0 commit comments

Comments
 (0)