|
| 1 | +use std::net::IpAddr; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use aquatic_udp_protocol::AnnounceEvent; |
| 5 | +use bittorrent_primitives::info_hash::InfoHash; |
| 6 | +use bittorrent_tracker_core::announce_handler::PeersWanted; |
| 7 | +use bittorrent_tracker_core::container::TrackerCoreContainer; |
| 8 | +use tokio::task::yield_now; |
| 9 | +use torrust_tracker_configuration::Core; |
| 10 | +use torrust_tracker_primitives::core::{AnnounceData, ScrapeData}; |
| 11 | +use torrust_tracker_primitives::peer::Peer; |
| 12 | +use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; |
| 13 | +use torrust_tracker_torrent_repository::container::TorrentRepositoryContainer; |
| 14 | + |
| 15 | +pub struct TestEnv { |
| 16 | + pub torrent_repository_container: Arc<TorrentRepositoryContainer>, |
| 17 | + pub tracker_core_container: Arc<TrackerCoreContainer>, |
| 18 | +} |
| 19 | + |
| 20 | +impl TestEnv { |
| 21 | + #[must_use] |
| 22 | + pub async fn started(core_config: Core) -> Self { |
| 23 | + let test_env = TestEnv::new(core_config); |
| 24 | + test_env.start().await; |
| 25 | + test_env |
| 26 | + } |
| 27 | + |
| 28 | + #[must_use] |
| 29 | + pub fn new(core_config: Core) -> Self { |
| 30 | + let core_config = Arc::new(core_config); |
| 31 | + |
| 32 | + let torrent_repository_container = Arc::new(TorrentRepositoryContainer::initialize( |
| 33 | + core_config.tracker_usage_statistics.into(), |
| 34 | + )); |
| 35 | + |
| 36 | + let tracker_core_container = Arc::new(TrackerCoreContainer::initialize_from( |
| 37 | + &core_config, |
| 38 | + &torrent_repository_container, |
| 39 | + )); |
| 40 | + |
| 41 | + Self { |
| 42 | + torrent_repository_container, |
| 43 | + tracker_core_container, |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + pub async fn start(&self) { |
| 48 | + let mut jobs = vec![]; |
| 49 | + |
| 50 | + let job = torrust_tracker_torrent_repository::statistics::event::listener::run_event_listener( |
| 51 | + self.torrent_repository_container.event_bus.receiver(), |
| 52 | + &self.torrent_repository_container.stats_repository, |
| 53 | + ); |
| 54 | + |
| 55 | + jobs.push(job); |
| 56 | + |
| 57 | + let job = bittorrent_tracker_core::statistics::event::listener::run_event_listener( |
| 58 | + self.torrent_repository_container.event_bus.receiver(), |
| 59 | + &self.tracker_core_container.db_torrent_repository, |
| 60 | + ); |
| 61 | + |
| 62 | + jobs.push(job); |
| 63 | + |
| 64 | + // Give the event listeners some time to start |
| 65 | + // todo: they should notify when they are ready |
| 66 | + tokio::time::sleep(std::time::Duration::from_millis(100)).await; |
| 67 | + } |
| 68 | + |
| 69 | + pub async fn announce_peer_started( |
| 70 | + &mut self, |
| 71 | + mut peer: Peer, |
| 72 | + remote_client_ip: &IpAddr, |
| 73 | + info_hash: &InfoHash, |
| 74 | + ) -> AnnounceData { |
| 75 | + peer.event = AnnounceEvent::Started; |
| 76 | + |
| 77 | + let announce_data = self |
| 78 | + .tracker_core_container |
| 79 | + .announce_handler |
| 80 | + .announce(info_hash, &mut peer, remote_client_ip, &PeersWanted::AsManyAsPossible) |
| 81 | + .await |
| 82 | + .unwrap(); |
| 83 | + |
| 84 | + // Give time to the event listeners to process the event |
| 85 | + yield_now().await; |
| 86 | + |
| 87 | + announce_data |
| 88 | + } |
| 89 | + |
| 90 | + pub async fn announce_peer_completed( |
| 91 | + &mut self, |
| 92 | + mut peer: Peer, |
| 93 | + remote_client_ip: &IpAddr, |
| 94 | + info_hash: &InfoHash, |
| 95 | + ) -> AnnounceData { |
| 96 | + peer.event = AnnounceEvent::Completed; |
| 97 | + |
| 98 | + let announce_data = self |
| 99 | + .tracker_core_container |
| 100 | + .announce_handler |
| 101 | + .announce(info_hash, &mut peer, remote_client_ip, &PeersWanted::AsManyAsPossible) |
| 102 | + .await |
| 103 | + .unwrap(); |
| 104 | + |
| 105 | + // Give time to the event listeners to process the event |
| 106 | + yield_now().await; |
| 107 | + |
| 108 | + announce_data |
| 109 | + } |
| 110 | + |
| 111 | + pub async fn scrape(&self, info_hash: &InfoHash) -> ScrapeData { |
| 112 | + self.tracker_core_container |
| 113 | + .scrape_handler |
| 114 | + .scrape(&vec![*info_hash]) |
| 115 | + .await |
| 116 | + .unwrap() |
| 117 | + } |
| 118 | + |
| 119 | + pub async fn increase_number_of_downloads(&mut self, peer: Peer, remote_client_ip: &IpAddr, info_hash: &InfoHash) { |
| 120 | + let _announce_data = self.announce_peer_started(peer, remote_client_ip, info_hash).await; |
| 121 | + let announce_data = self.announce_peer_completed(peer, remote_client_ip, info_hash).await; |
| 122 | + |
| 123 | + assert_eq!(announce_data.stats.downloads(), 1); |
| 124 | + } |
| 125 | + |
| 126 | + pub async fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> { |
| 127 | + self.torrent_repository_container |
| 128 | + .swarms |
| 129 | + .get_swarm_metadata(info_hash) |
| 130 | + .await |
| 131 | + .unwrap() |
| 132 | + } |
| 133 | + |
| 134 | + pub async fn remove_swarm(&self, info_hash: &InfoHash) { |
| 135 | + self.torrent_repository_container.swarms.remove(info_hash).await.unwrap(); |
| 136 | + } |
| 137 | +} |
0 commit comments