forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_banned.rs
More file actions
82 lines (71 loc) · 2.73 KB
/
Copy pathrequest_banned.rs
File metadata and controls
82 lines (71 loc) · 2.73 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
use torrust_tracker_metrics::label::LabelSet;
use torrust_tracker_metrics::metric_name;
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use crate::event::ConnectionContext;
use crate::statistics::repository::Repository;
use crate::statistics::UDP_TRACKER_SERVER_REQUESTS_BANNED_TOTAL;
pub async fn handle_event(context: ConnectionContext, stats_repository: &Repository, now: DurationSinceUnixEpoch) {
match stats_repository
.increase_counter(
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_BANNED_TOTAL),
&LabelSet::from(context),
now,
)
.await
{
Ok(()) => {}
Err(err) => tracing::error!("Failed to increase the counter: {}", err),
};
}
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use torrust_tracker_clock::clock::Time;
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
use crate::event::{ConnectionContext, Event};
use crate::statistics::event::handler::handle_event;
use crate::statistics::repository::Repository;
use crate::CurrentClock;
#[tokio::test]
async fn should_increase_the_number_of_banned_requests_when_it_receives_a_udp_request_banned_event() {
let stats_repository = Repository::new();
handle_event(
Event::UdpRequestBanned {
context: ConnectionContext::new(
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
ServiceBinding::new(
Protocol::UDP,
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 196)), 6969),
)
.unwrap(),
),
},
&stats_repository,
CurrentClock::now(),
)
.await;
let stats = stats_repository.get_stats().await;
assert_eq!(stats.udp_requests_banned_total(), 1);
}
#[tokio::test]
async fn should_increase_the_udp_ban_counter_when_it_receives_a_udp_banned_event() {
let stats_repository = Repository::new();
handle_event(
Event::UdpRequestBanned {
context: ConnectionContext::new(
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
ServiceBinding::new(
Protocol::UDP,
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 196)), 6969),
)
.unwrap(),
),
},
&stats_repository,
CurrentClock::now(),
)
.await;
let stats = stats_repository.get_stats().await;
assert_eq!(stats.udp_requests_banned_total(), 1);
}
}