Skip to content

Commit b05bccd

Browse files
committed
refactor: [#1524] integration tests in tracker-core
1 parent f3d3495 commit b05bccd

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

packages/tracker-core/tests/integration.rs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bittorrent_primitives::info_hash::InfoHash;
77
use bittorrent_tracker_core::announce_handler::PeersWanted;
88
use bittorrent_tracker_core::container::TrackerCoreContainer;
99
use torrust_tracker_configuration::Core;
10+
use torrust_tracker_primitives::core::AnnounceData;
1011
use torrust_tracker_primitives::peer::Peer;
1112
use torrust_tracker_primitives::DurationSinceUnixEpoch;
1213
use torrust_tracker_test_helpers::configuration::ephemeral_sqlite_database;
@@ -55,44 +56,63 @@ fn remote_client_ip() -> IpAddr {
5556
IpAddr::V4(Ipv4Addr::from_str("126.0.0.1").unwrap())
5657
}
5758

58-
#[tokio::test]
59-
async fn test_announce_and_scrape_requests() {
59+
fn initialize() -> (Arc<Core>, Arc<TrackerCoreContainer>, InfoHash, Peer) {
6060
let config = Arc::new(ephemeral_configuration());
6161

6262
let torrent_repository_container = Arc::new(TorrentRepositoryContainer::initialize(config.tracker_usage_statistics.into()));
6363

64-
let container = TrackerCoreContainer::initialize_from(&config, &torrent_repository_container);
64+
let container = Arc::new(TrackerCoreContainer::initialize_from(&config, &torrent_repository_container));
6565

6666
let info_hash = sample_info_hash();
6767

68-
let mut peer = sample_peer();
68+
let peer = sample_peer();
6969

70-
// Announce
70+
(config, container, info_hash, peer)
71+
}
7172

72-
// First announce: download started
73+
async fn announce_peer_started(container: &Arc<TrackerCoreContainer>, peer: &mut Peer, info_hash: &InfoHash) -> AnnounceData {
7374
peer.event = AnnounceEvent::Started;
74-
let announce_data = container
75+
76+
container
7577
.announce_handler
76-
.announce(&info_hash, &mut peer, &remote_client_ip(), &PeersWanted::AsManyAsPossible)
78+
.announce(info_hash, peer, &remote_client_ip(), &PeersWanted::AsManyAsPossible)
7779
.await
78-
.unwrap();
79-
80-
// NOTICE: you don't get back the peer making the request.
81-
assert_eq!(announce_data.peers.len(), 0);
82-
assert_eq!(announce_data.stats.downloaded, 0);
80+
.unwrap()
81+
}
8382

84-
// Second announce: download completed
83+
async fn _announce_peer_completed(container: &Arc<TrackerCoreContainer>, peer: &mut Peer, info_hash: &InfoHash) -> AnnounceData {
8584
peer.event = AnnounceEvent::Completed;
86-
let announce_data = container
85+
86+
container
8787
.announce_handler
88-
.announce(&info_hash, &mut peer, &remote_client_ip(), &PeersWanted::AsManyAsPossible)
88+
.announce(info_hash, peer, &remote_client_ip(), &PeersWanted::AsManyAsPossible)
8989
.await
90-
.unwrap();
90+
.unwrap()
91+
}
92+
93+
#[tokio::test]
94+
async fn it_should_handle_the_announce_request() {
95+
let (_config, container, info_hash, mut peer) = initialize();
96+
97+
let announce_data = announce_peer_started(&container, &mut peer, &info_hash).await;
98+
99+
assert_eq!(announce_data, AnnounceData::default());
100+
}
101+
102+
#[tokio::test]
103+
async fn it_should_not_return_the_peer_making_the_announce_request() {
104+
let (_config, container, info_hash, mut peer) = initialize();
105+
106+
let announce_data = announce_peer_started(&container, &mut peer, &info_hash).await;
91107

92108
assert_eq!(announce_data.peers.len(), 0);
93-
assert_eq!(announce_data.stats.downloaded, 1);
109+
}
110+
111+
#[tokio::test]
112+
async fn it_should_handle_the_scrape_request() {
113+
let (_config, container, info_hash, mut peer) = initialize();
94114

95-
// Scrape
115+
let _announce_data = announce_peer_started(&container, &mut peer, &info_hash).await;
96116

97117
let scrape_data = container.scrape_handler.scrape(&vec![info_hash]).await.unwrap();
98118

0 commit comments

Comments
 (0)