Skip to content

Add unit and integration tests for monitoring module#281

Open
gimballock wants to merge 2 commits intostratum-mining:mainfrom
fossatmara:feat/monitoring-tests
Open

Add unit and integration tests for monitoring module#281
gimballock wants to merge 2 commits intostratum-mining:mainfrom
fossatmara:feat/monitoring-tests

Conversation

@gimballock
Copy link

@gimballock gimballock commented Feb 17, 2026

Add test coverage for the monitoring/metrics system at both the unit and integration level.

Changes

Commit 1: Unit tests for monitoring data layer and HTTP API (1e45c73)

Unit tests for the previously untested monitoring module in stratum-apps/src/monitoring/:

  • client.rs: Sv2ClientInfo accessors, to_metadata(), Sv2ClientsMonitoring default impls (get_sv2_client_by_id, get_sv2_clients_summary)
  • server.rs: ServerInfo accessors with Option hashrate handling, ServerMonitoring default impl (get_server_summary)
  • sv1.rs: Sv1ClientsMonitoring default impls (get_sv1_client_by_id, get_sv1_clients_summary including None-hashrate filtering)
  • prometheus_metrics.rs: Conditional metric registration per role type, Prometheus text encoding round-trip
  • http_server.rs: Pagination unit tests (effective_limit clamping, paginate edge cases), HTTP endpoint integration tests using tower::oneshot against mock monitoring sources for all 9 API routes and /metrics

Adds tower, http-body-util, hyper as dev-dependencies for the axum handler tests.

Commit 2: Integration test infrastructure and metrics assertions (1a6ef0e)

  • Config setters (pool/config.rs, translator/config.rs, jd-client/config.rs): Add set_monitoring_address() and set_monitoring_cache_refresh_secs() so tests can enable monitoring on dynamic ports with a fast 1-second cache refresh.

  • Test helpers (integration-tests/lib/mod.rs): Add start_pool_with_monitoring(), start_jdc_with_monitoring(), start_sv2_translator_with_monitoring() wrappers that configure monitoring and return the monitoring address.

  • Assertion helpers (integration-tests/lib/metrics_assert.rs): Async helpers (fetch_metrics, fetch_api, assert_api_health, assert_uptime, assert_metric_gte, assert_metric_eq, assert_metric_present, assert_metric_not_present) using spawn_blocking internally for the synchronous minreq HTTP calls. 11 unit tests for the parsing/assertion logic.

  • Metrics assertions augment 9 existing integration test functions across 8 test files. Each test waits 2 seconds for the SnapshotCache to refresh, then validates topology-appropriate metrics:

Test Metrics validated
sv2_mining_device_and_pool_success Pool: clients >= 1, uptime, health OK
pool_standard_channel_receives_share Pool: clients >= 1, shares accepted >= 1, uptime, health OK
translate_sv1_to_sv2_successfully Pool: clients >= 1, no server channels. tProxy: server channels present, SV1 clients >= 1, no SV2 clients, server shares accepted
test_extension_negotiation_with_tlv_in_submit_shares Pool: clients >= 1, uptime, health OK
jd_non_aggregated_tproxy_integration tProxy: server channels, SV1 clients >= 1. JDC: server channels, no SV2 clients. Pool: clients >= 1
jd_aggregated_tproxy_integration Pool: clients >= 1. tProxy: exactly 1 server channel (aggregated)
jdc_submit_shares_success Pool: clients >= 1, uptime, health OK
propagated_from_jdc_to_tp Pool: clients >= 1, shares accepted >= 1
propagated_from_jds_to_tp Pool: clients >= 1, shares accepted >= 1

Technical notes

  • Metric helpers use tokio::task::spawn_blocking to wrap synchronous minreq HTTP calls, preventing deadlocks on the single-threaded tokio test runtime
  • Tests use #[tokio::test] (current_thread) rather than multi_thread, because FallbackHandler::drop() in fallback_coordinator.rs panics if done() was never called — aborting translator/JDC tasks on a multi_thread worker triggers a double-panic SIGABRT
  • Cache refresh is set to 1 second in tests (vs 15s default), keeping the per-test overhead to ~2 seconds

@gimballock gimballock force-pushed the feat/monitoring-tests branch 2 times, most recently from eeb2a80 to b72e8aa Compare February 18, 2026 15:45
@gimballock
Copy link
Author

I added some additional tests, let me know if you see any areas that still need more coverage

@gimballock gimballock force-pushed the feat/monitoring-tests branch from b72e8aa to 5a6886b Compare February 24, 2026 14:29
@plebhash
Copy link
Member

PR title and description mention integration tests, but I don't see any on the diffs?

@gimballock gimballock force-pushed the feat/monitoring-tests branch from 5a6886b to d97bd44 Compare February 25, 2026 17:08
Cover the previously untested data-layer traits and HTTP API:

- client.rs: Sv2ClientInfo accessors, to_metadata(), Sv2ClientsMonitoring
  default impls (get_sv2_client_by_id, get_sv2_clients_summary)
- server.rs: ServerInfo accessors with Option<f32> hashrate handling,
  ServerMonitoring default impl (get_server_summary)
- sv1.rs: Sv1ClientsMonitoring default impls (get_sv1_client_by_id,
  get_sv1_clients_summary including None-hashrate filtering)
- prometheus_metrics.rs: conditional metric registration per role type,
  Prometheus text encoding round-trip
- http_server.rs: Pagination unit tests (effective_limit clamping,
  paginate edge cases), HTTP endpoint integration tests using
  tower::oneshot against mock monitoring sources for all 9 API
  routes and /metrics

Adds tower, http-body-util, hyper as dev-dependencies for the
axum handler tests.

Closes stratum-mining#161
@gimballock gimballock force-pushed the feat/monitoring-tests branch from 3c6753e to 1c6f706 Compare February 25, 2026 17:58
…ertions

Add config setters (set_monitoring_address, set_monitoring_cache_refresh_secs)
to PoolConfig, TranslatorConfig, and JobDeclaratorClientConfig so integration
tests can enable the monitoring HTTP server on dynamic ports with a fast
1-second cache refresh interval.

Create metrics_assert module with async helpers (fetch_metrics, fetch_api,
assert_api_health, assert_uptime, assert_metric_gte, assert_metric_eq,
assert_metric_present, assert_metric_not_present) that use spawn_blocking
internally to avoid deadlocking the tokio current_thread runtime with
synchronous minreq HTTP calls. Includes 11 unit tests for parsing/assertion
logic.

Add start_pool_with_monitoring, start_jdc_with_monitoring, and
start_sv2_translator_with_monitoring test helpers that return JoinHandle
and optional monitoring SocketAddr.

Augment 9 existing integration test functions across 8 test files with
metrics validation. Each test waits 2 seconds for the SnapshotCache to
refresh, then validates topology-appropriate metrics:
- Pool: sv2_clients_total, uptime, health endpoint, no sv2_server_channels
- tProxy: sv2_server_channels, sv1_clients_total, server shares accepted
- JDC: sv2_server_channels present, no sv2_clients_total

Tests use #[tokio::test] (current_thread runtime) rather than multi_thread,
because FallbackHandler::drop() panics if done() was never called -- aborting
translator/JDC tasks on a multi_thread worker triggers a double-panic SIGABRT.
@gimballock gimballock force-pushed the feat/monitoring-tests branch from 1c6f706 to 1a6ef0e Compare February 25, 2026 18:02
@gimballock
Copy link
Author

Updated the integration tests to validate metric invariant related to their basic detecting ability: when connected channel counts should be non-zero, uptime should be positive, share count should be positive if we have seen a share, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants