Skip to content

Commit 92f049e

Browse files
committed
tests: add tests for HTTP tracker core events comparison
This test has been added becuase this code was not working: ```rust let mut announced_peer = peer_copy; announced_peer.peer_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); let mut added_peer = peer; added_peer.peer_addr = SocketAddr::new( IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)), 8080, ); let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new(); http_stats_event_sender_mock .expect_send_event() .with(eq(Event::TcpAnnounce { connection: ConnectionContext::new( IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), Some(8080), server_service_binding, ), announced_peer: peer, added_peer: peer, })) .times(1) .returning(|_| Box::pin(future::ready(Some(Ok(1))))); ``` using the same events: Event sent: TcpAnnounce { connection: ConnectionContext { client: ClientConnectionContext { ip_addr: 127.0.0.1, port: Some(8080) }, server: ServerConnectionContext { service_binding: ServiceBinding { protocol: HTTP, bind_address: 127.0.0.1:7070 } } }, announced_peer: Peer { peer_id: PeerId([45, 113, 66, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]), peer_addr: 127.0.0.1:8080, updated: 1745316858.487824645s, uploaded: NumberOfBytes(I64(0)), downloaded: NumberOfBytes(I64(0)), left: NumberOfBytes(I64(0)), event: Started }, added_peer: Peer { peer_id: PeerId([45, 113, 66, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]), peer_addr: [6969:6969:6969:6969:6969:6969:6969:6969]:8080, updated: 1745316858.487824645s, uploaded: NumberOfBytes(I64(0)), downloaded: NumberOfBytes(I64(0)), left: NumberOfBytes(I64(0)), event: Started } } Event expected in the mock: TcpAnnounce { connection: ConnectionContext { client: ClientConnectionContext { ip_addr: 127.0.0.1, port: Some(8080) }, server: ServerConnectionContext { service_binding: ServiceBinding { protocol: HTTP, bind_address: 127.0.0.1:7070 } } }, announced_peer: Peer { peer_id: PeerId([45, 113, 66, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]), peer_addr: 127.0.0.1:8080, updated: 1745316858.487824645s, uploaded: NumberOfBytes(I64(0)), downloaded: NumberOfBytes(I64(0)), left: NumberOfBytes(I64(0)), event: Started }, added_peer: Peer { peer_id: PeerId([45, 113, 66, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]), peer_addr: [6969:6969:6969:6969:6969:6969:6969:6969]:8080, updated: 1745316858.487824645s, uploaded: NumberOfBytes(I64(0)), downloaded: NumberOfBytes(I64(0)), left: NumberOfBytes(I64(0)), event: Started } } That's one of the reasons why the expectation was changed. The other reason is the only relevant part for the peer in the test is the updated peer address.
1 parent 4566ad5 commit 92f049e

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

  • packages/http-tracker-core/src/event

packages/http-tracker-core/src/event/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,44 @@ impl From<ConnectionContext> for LabelSet {
9494
])
9595
}
9696
}
97+
98+
#[cfg(test)]
99+
pub mod test {
100+
101+
use torrust_tracker_primitives::peer::Peer;
102+
use torrust_tracker_primitives::service_binding::Protocol;
103+
104+
#[test]
105+
fn events_should_be_comparable() {
106+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
107+
108+
use torrust_tracker_primitives::service_binding::ServiceBinding;
109+
110+
use crate::event::{ConnectionContext, Event};
111+
112+
let event1 = Event::TcpAnnounce {
113+
connection: ConnectionContext::new(
114+
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
115+
Some(8080),
116+
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
117+
),
118+
announced_peer: Peer::default(),
119+
added_peer: Peer::default(),
120+
};
121+
122+
let event2 = Event::TcpAnnounce {
123+
connection: ConnectionContext::new(
124+
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)),
125+
Some(8080),
126+
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
127+
),
128+
announced_peer: Peer::default(),
129+
added_peer: Peer::default(),
130+
};
131+
132+
let event1_clone = event1.clone();
133+
134+
assert!(event1 == event1_clone);
135+
assert!(event1 != event2);
136+
}
137+
}

0 commit comments

Comments
 (0)