Skip to content

Commit 48fc80d

Browse files
authored
apollo_mempool: use expect! in metrics test (#11389)
1 parent c04b632 commit 48fc80d

File tree

5 files changed

+99
-74
lines changed

5 files changed

+99
-74
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_mempool/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ assert_matches.workspace = true
5252
blockifier = { workspace = true, features = ["mocks", "testing"] }
5353
blockifier_test_utils.workspace = true
5454
criterion = { workspace = true, features = ["async_tokio"] }
55+
expect-test.workspace = true
5556
itertools.workspace = true
5657
mempool_test_utils.workspace = true
5758
metrics.workspace = true

crates/apollo_mempool/src/mempool_test.rs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use apollo_mempool_p2p_types::communication::{
1212
use apollo_mempool_types::communication::AddTransactionArgsWrapper;
1313
use apollo_mempool_types::errors::MempoolError;
1414
use apollo_mempool_types::mempool_types::{AccountState, AddTransactionArgs, ValidationArgs};
15-
use apollo_metrics::metrics::HistogramValue;
1615
use apollo_network_types::network_types::BroadcastedMessageMetadata;
1716
use apollo_test_utils::{get_rng, GetTestInstance};
1817
use apollo_time::test_utils::FakeClock;
18+
use expect_test::expect;
1919
use metrics_exporter_prometheus::PrometheusBuilder;
2020
use mockall::predicate::eq;
2121
use pretty_assertions::assert_eq;
@@ -1201,7 +1201,7 @@ fn test_register_metrics() {
12011201
register_metrics();
12021202

12031203
let expected_metrics = MempoolMetrics::default();
1204-
expected_metrics.verify_metrics(&recorder);
1204+
assert_eq!(MempoolMetrics::from_recorder(&recorder), expected_metrics);
12051205
}
12061206

12071207
#[test]
@@ -1294,33 +1294,37 @@ fn metrics_correctness() {
12941294
fake_clock.advance(Duration::from_secs(20));
12951295
commit_block(&mut mempool, [("0x9", 1), ("0x3", 1)], []);
12961296

1297-
let expected_metrics = MempoolMetrics {
1298-
txs_received_invoke: 8,
1299-
txs_received_declare: 2,
1300-
txs_received_deploy_account: 0,
1301-
txs_committed: 3,
1302-
txs_dropped_expired: 1,
1303-
txs_dropped_rejected: 1,
1304-
txs_dropped_evicted: 1,
1305-
pool_size: 3,
1306-
priority_queue_size: 2,
1307-
pending_queue_size: 1,
1308-
get_txs_size: 1,
1309-
delayed_declares_size: 1,
1310-
total_size_in_bytes: 1552,
1311-
evictions_count: 1,
1312-
transaction_time_spent_until_batched: HistogramValue {
1313-
sum: 2.0,
1314-
count: 1,
1315-
..Default::default()
1316-
},
1317-
transaction_time_spent_until_committed: HistogramValue {
1318-
sum: 42.0,
1319-
count: 3,
1320-
..Default::default()
1321-
},
1322-
};
1323-
expected_metrics.verify_metrics(&recorder);
1297+
let mut metrics = MempoolMetrics::from_recorder(&recorder);
1298+
metrics.transaction_time_spent_until_batched.histogram = Default::default();
1299+
metrics.transaction_time_spent_until_committed.histogram = Default::default();
1300+
expect![[r#"
1301+
MempoolMetrics {
1302+
txs_received_invoke: 8,
1303+
txs_received_declare: 2,
1304+
txs_received_deploy_account: 0,
1305+
txs_committed: 3,
1306+
txs_dropped_expired: 1,
1307+
txs_dropped_rejected: 1,
1308+
txs_dropped_evicted: 1,
1309+
pool_size: 3,
1310+
priority_queue_size: 2,
1311+
pending_queue_size: 1,
1312+
get_txs_size: 1,
1313+
delayed_declares_size: 1,
1314+
total_size_in_bytes: 1552,
1315+
transaction_time_spent_until_batched: HistogramValue {
1316+
sum: 2.0,
1317+
count: 1,
1318+
histogram: {},
1319+
},
1320+
transaction_time_spent_until_committed: HistogramValue {
1321+
sum: 42.0,
1322+
count: 3,
1323+
histogram: {},
1324+
},
1325+
}
1326+
"#]]
1327+
.assert_debug_eq(&metrics);
13241328
}
13251329

13261330
#[rstest]

crates/apollo_mempool/src/test_utils.rs

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ pub fn get_txs_and_assert_expected(
294294
assert_eq!(txs, expected_txs);
295295
}
296296

297-
#[derive(Default)]
297+
#[derive(Default, Debug, PartialEq)]
298298
pub struct MempoolMetrics {
299299
pub txs_received_invoke: u64,
300300
pub txs_received_declare: u64,
@@ -309,54 +309,73 @@ pub struct MempoolMetrics {
309309
pub get_txs_size: u64,
310310
pub delayed_declares_size: u64,
311311
pub total_size_in_bytes: u64,
312-
pub evictions_count: u64,
313312
pub transaction_time_spent_until_batched: HistogramValue,
314313
pub transaction_time_spent_until_committed: HistogramValue,
315314
}
316315

317316
impl MempoolMetrics {
318-
pub fn verify_metrics(&self, recorder: &PrometheusRecorder) {
317+
pub fn from_recorder(recorder: &PrometheusRecorder) -> Self {
319318
let metrics = &recorder.handle().render();
320-
MEMPOOL_TRANSACTIONS_RECEIVED.assert_eq(
321-
metrics,
322-
self.txs_received_invoke,
323-
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::Invoke.into())],
324-
);
325-
MEMPOOL_TRANSACTIONS_RECEIVED.assert_eq(
326-
metrics,
327-
self.txs_received_declare,
328-
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::Declare.into())],
329-
);
330-
MEMPOOL_TRANSACTIONS_RECEIVED.assert_eq(
331-
metrics,
332-
self.txs_received_deploy_account,
333-
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::DeployAccount.into())],
334-
);
335-
MEMPOOL_TRANSACTIONS_COMMITTED.assert_eq(metrics, self.txs_committed);
336-
MEMPOOL_TRANSACTIONS_DROPPED.assert_eq(
337-
metrics,
338-
self.txs_dropped_expired,
339-
&[(LABEL_NAME_DROP_REASON, DropReason::Expired.into())],
340-
);
341-
MEMPOOL_TRANSACTIONS_DROPPED.assert_eq(
342-
metrics,
343-
self.txs_dropped_rejected,
344-
&[(LABEL_NAME_DROP_REASON, DropReason::Rejected.into())],
345-
);
346-
MEMPOOL_TRANSACTIONS_DROPPED.assert_eq(
347-
metrics,
348-
self.txs_dropped_evicted,
349-
&[(LABEL_NAME_DROP_REASON, DropReason::Evicted.into())],
350-
);
351-
MEMPOOL_POOL_SIZE.assert_eq(metrics, self.pool_size);
352-
MEMPOOL_PRIORITY_QUEUE_SIZE.assert_eq(metrics, self.priority_queue_size);
353-
MEMPOOL_PENDING_QUEUE_SIZE.assert_eq(metrics, self.pending_queue_size);
354-
MEMPOOL_GET_TXS_SIZE.assert_eq(metrics, self.get_txs_size);
355-
MEMPOOL_DELAYED_DECLARES_SIZE.assert_eq(metrics, self.delayed_declares_size);
356-
MEMPOOL_TOTAL_SIZE_BYTES.assert_eq(metrics, self.total_size_in_bytes);
357-
TRANSACTION_TIME_SPENT_UNTIL_BATCHED
358-
.assert_eq(metrics, &self.transaction_time_spent_until_batched);
359-
TRANSACTION_TIME_SPENT_UNTIL_COMMITTED
360-
.assert_eq(metrics, &self.transaction_time_spent_until_committed);
319+
Self {
320+
txs_received_invoke: MEMPOOL_TRANSACTIONS_RECEIVED
321+
.parse_numeric_metric::<u64>(
322+
metrics,
323+
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::Invoke.into())],
324+
)
325+
.unwrap(),
326+
txs_received_declare: MEMPOOL_TRANSACTIONS_RECEIVED
327+
.parse_numeric_metric::<u64>(
328+
metrics,
329+
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::Declare.into())],
330+
)
331+
.unwrap(),
332+
txs_received_deploy_account: MEMPOOL_TRANSACTIONS_RECEIVED
333+
.parse_numeric_metric::<u64>(
334+
metrics,
335+
&[(LABEL_NAME_TX_TYPE, RpcTransactionLabelValue::DeployAccount.into())],
336+
)
337+
.unwrap(),
338+
txs_committed: MEMPOOL_TRANSACTIONS_COMMITTED
339+
.parse_numeric_metric::<u64>(metrics)
340+
.unwrap(),
341+
txs_dropped_expired: MEMPOOL_TRANSACTIONS_DROPPED
342+
.parse_numeric_metric::<u64>(
343+
metrics,
344+
&[(LABEL_NAME_DROP_REASON, DropReason::Expired.into())],
345+
)
346+
.unwrap(),
347+
txs_dropped_rejected: MEMPOOL_TRANSACTIONS_DROPPED
348+
.parse_numeric_metric::<u64>(
349+
metrics,
350+
&[(LABEL_NAME_DROP_REASON, DropReason::Rejected.into())],
351+
)
352+
.unwrap(),
353+
txs_dropped_evicted: MEMPOOL_TRANSACTIONS_DROPPED
354+
.parse_numeric_metric::<u64>(
355+
metrics,
356+
&[(LABEL_NAME_DROP_REASON, DropReason::Evicted.into())],
357+
)
358+
.unwrap(),
359+
pool_size: MEMPOOL_POOL_SIZE.parse_numeric_metric::<u64>(metrics).unwrap(),
360+
priority_queue_size: MEMPOOL_PRIORITY_QUEUE_SIZE
361+
.parse_numeric_metric::<u64>(metrics)
362+
.unwrap(),
363+
pending_queue_size: MEMPOOL_PENDING_QUEUE_SIZE
364+
.parse_numeric_metric::<u64>(metrics)
365+
.unwrap(),
366+
get_txs_size: MEMPOOL_GET_TXS_SIZE.parse_numeric_metric::<u64>(metrics).unwrap(),
367+
delayed_declares_size: MEMPOOL_DELAYED_DECLARES_SIZE
368+
.parse_numeric_metric::<u64>(metrics)
369+
.unwrap(),
370+
total_size_in_bytes: MEMPOOL_TOTAL_SIZE_BYTES
371+
.parse_numeric_metric::<u64>(metrics)
372+
.unwrap(),
373+
transaction_time_spent_until_batched: TRANSACTION_TIME_SPENT_UNTIL_BATCHED
374+
.parse_histogram_metric(metrics)
375+
.unwrap(),
376+
transaction_time_spent_until_committed: TRANSACTION_TIME_SPENT_UNTIL_COMMITTED
377+
.parse_histogram_metric(metrics)
378+
.unwrap(),
379+
}
361380
}
362381
}

crates/apollo_metrics/src/metrics/histograms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl MetricHistogram {
7777
}
7878

7979
#[cfg(any(feature = "testing", test))]
80-
pub(crate) fn parse_histogram_metric(&self, metrics_as_string: &str) -> Option<HistogramValue> {
80+
pub fn parse_histogram_metric(&self, metrics_as_string: &str) -> Option<HistogramValue> {
8181
parse_histogram_metric(metrics_as_string, self.get_name(), None)
8282
}
8383

0 commit comments

Comments
 (0)