|
| 1 | +use crate::statistics::event::{Event, UdpResponseKind}; |
| 2 | +use crate::statistics::repository::Repository; |
| 3 | + |
| 4 | +pub async fn handle_event(event: Event, stats_repository: &Repository) { |
| 5 | + match event { |
| 6 | + // UDP |
| 7 | + Event::UdpRequestAborted => { |
| 8 | + stats_repository.increase_udp_requests_aborted().await; |
| 9 | + } |
| 10 | + Event::UdpRequestBanned => { |
| 11 | + stats_repository.increase_udp_requests_banned().await; |
| 12 | + } |
| 13 | + |
| 14 | + // UDP4 |
| 15 | + Event::Udp4Request { kind } => { |
| 16 | + stats_repository.increase_udp4_requests().await; |
| 17 | + match kind { |
| 18 | + UdpResponseKind::Connect => { |
| 19 | + stats_repository.increase_udp4_connections().await; |
| 20 | + } |
| 21 | + UdpResponseKind::Announce => { |
| 22 | + stats_repository.increase_udp4_announces().await; |
| 23 | + } |
| 24 | + UdpResponseKind::Scrape => { |
| 25 | + stats_repository.increase_udp4_scrapes().await; |
| 26 | + } |
| 27 | + UdpResponseKind::Error => {} |
| 28 | + } |
| 29 | + } |
| 30 | + Event::Udp4Response { |
| 31 | + kind, |
| 32 | + req_processing_time, |
| 33 | + } => { |
| 34 | + stats_repository.increase_udp4_responses().await; |
| 35 | + |
| 36 | + match kind { |
| 37 | + UdpResponseKind::Connect => { |
| 38 | + stats_repository |
| 39 | + .recalculate_udp_avg_connect_processing_time_ns(req_processing_time) |
| 40 | + .await; |
| 41 | + } |
| 42 | + UdpResponseKind::Announce => { |
| 43 | + stats_repository |
| 44 | + .recalculate_udp_avg_announce_processing_time_ns(req_processing_time) |
| 45 | + .await; |
| 46 | + } |
| 47 | + UdpResponseKind::Scrape => { |
| 48 | + stats_repository |
| 49 | + .recalculate_udp_avg_scrape_processing_time_ns(req_processing_time) |
| 50 | + .await; |
| 51 | + } |
| 52 | + UdpResponseKind::Error => {} |
| 53 | + } |
| 54 | + } |
| 55 | + Event::Udp4Error => { |
| 56 | + stats_repository.increase_udp4_errors().await; |
| 57 | + } |
| 58 | + |
| 59 | + // UDP6 |
| 60 | + Event::Udp6Request => { |
| 61 | + stats_repository.increase_udp6_requests().await; |
| 62 | + } |
| 63 | + Event::Udp6Response { |
| 64 | + kind: _, |
| 65 | + req_processing_time: _, |
| 66 | + } => { |
| 67 | + stats_repository.increase_udp6_responses().await; |
| 68 | + } |
| 69 | + Event::Udp6Error => { |
| 70 | + stats_repository.increase_udp6_errors().await; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + tracing::debug!("stats: {:?}", stats_repository.get_stats().await); |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use crate::statistics::event::handler::handle_event; |
| 80 | + use crate::statistics::event::{Event, UdpResponseKind}; |
| 81 | + use crate::statistics::repository::Repository; |
| 82 | + |
| 83 | + #[tokio::test] |
| 84 | + async fn should_increase_the_udp_abort_counter_when_it_receives_a_udp_abort_event() { |
| 85 | + let stats_repository = Repository::new(); |
| 86 | + |
| 87 | + handle_event(Event::UdpRequestAborted, &stats_repository).await; |
| 88 | + let stats = stats_repository.get_stats().await; |
| 89 | + assert_eq!(stats.udp_requests_aborted, 1); |
| 90 | + } |
| 91 | + #[tokio::test] |
| 92 | + async fn should_increase_the_udp_ban_counter_when_it_receives_a_udp_banned_event() { |
| 93 | + let stats_repository = Repository::new(); |
| 94 | + |
| 95 | + handle_event(Event::UdpRequestBanned, &stats_repository).await; |
| 96 | + let stats = stats_repository.get_stats().await; |
| 97 | + assert_eq!(stats.udp_requests_banned, 1); |
| 98 | + } |
| 99 | + |
| 100 | + #[tokio::test] |
| 101 | + async fn should_increase_the_udp4_requests_counter_when_it_receives_a_udp4_request_event() { |
| 102 | + let stats_repository = Repository::new(); |
| 103 | + |
| 104 | + handle_event( |
| 105 | + Event::Udp4Request { |
| 106 | + kind: UdpResponseKind::Connect, |
| 107 | + }, |
| 108 | + &stats_repository, |
| 109 | + ) |
| 110 | + .await; |
| 111 | + |
| 112 | + let stats = stats_repository.get_stats().await; |
| 113 | + |
| 114 | + assert_eq!(stats.udp4_requests, 1); |
| 115 | + } |
| 116 | + |
| 117 | + #[tokio::test] |
| 118 | + async fn should_increase_the_udp4_responses_counter_when_it_receives_a_udp4_response_event() { |
| 119 | + let stats_repository = Repository::new(); |
| 120 | + |
| 121 | + handle_event( |
| 122 | + Event::Udp4Response { |
| 123 | + kind: crate::statistics::event::UdpResponseKind::Announce, |
| 124 | + req_processing_time: std::time::Duration::from_secs(1), |
| 125 | + }, |
| 126 | + &stats_repository, |
| 127 | + ) |
| 128 | + .await; |
| 129 | + |
| 130 | + let stats = stats_repository.get_stats().await; |
| 131 | + |
| 132 | + assert_eq!(stats.udp4_responses, 1); |
| 133 | + } |
| 134 | + |
| 135 | + #[tokio::test] |
| 136 | + async fn should_increase_the_udp4_errors_counter_when_it_receives_a_udp4_error_event() { |
| 137 | + let stats_repository = Repository::new(); |
| 138 | + |
| 139 | + handle_event(Event::Udp4Error, &stats_repository).await; |
| 140 | + |
| 141 | + let stats = stats_repository.get_stats().await; |
| 142 | + |
| 143 | + assert_eq!(stats.udp4_errors_handled, 1); |
| 144 | + } |
| 145 | + |
| 146 | + #[tokio::test] |
| 147 | + async fn should_increase_the_udp6_requests_counter_when_it_receives_a_udp6_request_event() { |
| 148 | + let stats_repository = Repository::new(); |
| 149 | + |
| 150 | + handle_event(Event::Udp6Request, &stats_repository).await; |
| 151 | + |
| 152 | + let stats = stats_repository.get_stats().await; |
| 153 | + |
| 154 | + assert_eq!(stats.udp6_requests, 1); |
| 155 | + } |
| 156 | + |
| 157 | + #[tokio::test] |
| 158 | + async fn should_increase_the_udp6_response_counter_when_it_receives_a_udp6_response_event() { |
| 159 | + let stats_repository = Repository::new(); |
| 160 | + |
| 161 | + handle_event( |
| 162 | + Event::Udp6Response { |
| 163 | + kind: crate::statistics::event::UdpResponseKind::Announce, |
| 164 | + req_processing_time: std::time::Duration::from_secs(1), |
| 165 | + }, |
| 166 | + &stats_repository, |
| 167 | + ) |
| 168 | + .await; |
| 169 | + |
| 170 | + let stats = stats_repository.get_stats().await; |
| 171 | + |
| 172 | + assert_eq!(stats.udp6_responses, 1); |
| 173 | + } |
| 174 | + #[tokio::test] |
| 175 | + async fn should_increase_the_udp6_errors_counter_when_it_receives_a_udp6_error_event() { |
| 176 | + let stats_repository = Repository::new(); |
| 177 | + |
| 178 | + handle_event(Event::Udp6Error, &stats_repository).await; |
| 179 | + |
| 180 | + let stats = stats_repository.get_stats().await; |
| 181 | + |
| 182 | + assert_eq!(stats.udp6_errors_handled, 1); |
| 183 | + } |
| 184 | +} |
0 commit comments