forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate_stats_fixed_ports.rs
More file actions
130 lines (104 loc) · 4.45 KB
/
Copy pathaggregate_stats_fixed_ports.rs
File metadata and controls
130 lines (104 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! Aggregate statistics integration test — fixed-port multi-instance scenarios.
//!
//! This binary starts a tracker with two HTTP and two UDP listeners on distinct
//! fixed ports, all with `tracker_usage_statistics = true`. Scenario functions
//! verify that aggregate statistics correctly count announces from all enabled
//! listeners.
//!
//! ```text
//! cargo test --test aggregate_stats_fixed_ports
//! ```
mod common;
use torrust_clock::clock;
/// This code needs to be copied into each crate.
/// Working version, for production.
#[cfg(not(test))]
#[allow(dead_code)]
pub(crate) type CurrentClock = clock::Working;
/// Stopped version, for testing.
#[cfg(test)]
#[allow(dead_code)]
pub(crate) type CurrentClock = clock::Stopped;
/// Configuration: two HTTP listeners on distinct fixed ports,
/// all with `tracker_usage_statistics = true`.
// NOTE: The fixed-port enabled/disabled HTTP aggregate-statistics test (count == 1)
// is blocked by #2039 — the shared HTTP event bus prevents per-instance metrics
// suppression. The bootstrap fix correctly assigns different containers, but the
// HTTP stats layer still counts both. This test proves both listeners start;
// the per-instance filtering test will be added when #2039 lands.
const FIXED_PORT_CONFIG: &str = r#"
[metadata]
app = "torrust-tracker"
purpose = "configuration"
schema_version = "2.0.0"
[logging]
threshold = "off"
[core]
listed = false
private = false
[core.database]
driver = "sqlite3"
path = "{STORAGE_PATH}/sqlite3.db"
[[http_trackers]]
bind_address = "0.0.0.0:17091"
tracker_usage_statistics = true
[[http_trackers]]
bind_address = "0.0.0.0:17092"
tracker_usage_statistics = true
[[udp_trackers]]
bind_address = "0.0.0.0:17093"
tracker_usage_statistics = true
[[udp_trackers]]
bind_address = "0.0.0.0:17094"
tracker_usage_statistics = true
[http_api]
bind_address = "127.0.0.1:0"
[http_api.access_tokens]
admin = "MyAccessToken"
[health_check_api]
bind_address = "127.0.0.2:0"
"#;
#[tokio::test]
async fn aggregate_stats_scenarios() {
let workspace = common::EphemeralTrackerWorkspace::new(FIXED_PORT_CONFIG);
let (app_container, _jobs) = common::start_tracker_with_config(&workspace).await;
http_trackers_on_fixed_ports_should_aggregate_announces_from_both_listeners(&app_container).await;
udp_trackers_on_fixed_ports_should_aggregate_announces_from_both_listeners(&app_container).await;
}
/// Both HTTP listeners are on distinct fixed ports. Announces to both
/// should be counted in the aggregate HTTP statistics.
async fn http_trackers_on_fixed_ports_should_aggregate_announces_from_both_listeners(
app_container: &std::sync::Arc<torrust_tracker_lib::container::AppContainer>,
) {
let tracker_urls = common::http_tracker_urls(app_container).await;
assert_eq!(tracker_urls.len(), 2, "expected two HTTP trackers");
let api_url = common::http_api_url(app_container).await.expect("expected an HTTP API URL");
let info_hash = [
0x9c, 0x8b, 0x22, 0x13, 0xe3, 0x0b, 0xff, 0x21, 0x2b, 0x0c, 0x36, 0x0d, 0x26, 0xf9, 0xa0, 0x21, 0x31, 0x64, 0x22, 0x00,
];
let peer_id = *b"-qB00000000000000001";
for url in &tracker_urls {
common::http_announce(url, &info_hash, &peer_id, 17548).await;
}
let global_stats = common::get_tracker_statistics(&api_url, "MyAccessToken").await;
assert_eq!(global_stats.tcp4_announces_handled, 2);
}
/// Both UDP listeners are on distinct fixed ports. Announces to both
/// should be counted in the aggregate UDP statistics.
async fn udp_trackers_on_fixed_ports_should_aggregate_announces_from_both_listeners(
app_container: &std::sync::Arc<torrust_tracker_lib::container::AppContainer>,
) {
let udp_urls = common::udp_tracker_urls(app_container).await;
assert_eq!(udp_urls.len(), 2, "expected two UDP trackers");
let api_url = common::http_api_url(app_container).await.expect("expected an HTTP API URL");
let info_hash = [
0x9c, 0x8b, 0x22, 0x13, 0xe3, 0x0b, 0xff, 0x21, 0x2b, 0x0c, 0x36, 0x0d, 0x26, 0xf9, 0xa0, 0x21, 0x31, 0x64, 0x22, 0x00,
];
let peer_id = *b"-qB00000000000000001";
for url in &udp_urls {
let addr = common::udp_socket_addr(url);
common::udp_announce(addr, &info_hash, &peer_id, 17548).await;
}
let global_stats = common::get_tracker_statistics(&api_url, "MyAccessToken").await;
assert_eq!(global_stats.udp4_announces_handled, 2);
}