Skip to content

Commit 12f54e7

Browse files
committed
test: add tests for new torrent repository using SkipMap
1 parent 0989285 commit 12f54e7

File tree

3 files changed

+193
-82
lines changed

3 files changed

+193
-82
lines changed

packages/torrent-repository/src/repository/skip_map_mutex_std.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{EntryMutexStd, EntrySingle};
1515

1616
#[derive(Default, Debug)]
1717
pub struct CrossbeamSkipList<T> {
18-
torrents: SkipMap<InfoHash, T>,
18+
pub torrents: SkipMap<InfoHash, T>,
1919
}
2020

2121
impl Repository<EntryMutexStd> for CrossbeamSkipList<EntryMutexStd>

packages/torrent-repository/tests/common/repo.rs

+98-67
Original file line numberDiff line numberDiff line change
@@ -7,141 +7,172 @@ use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent
77
use torrust_tracker_torrent_repository::repository::{Repository as _, RepositoryAsync as _};
88
use torrust_tracker_torrent_repository::{
99
EntrySingle, TorrentsRwLockStd, TorrentsRwLockStdMutexStd, TorrentsRwLockStdMutexTokio, TorrentsRwLockTokio,
10-
TorrentsRwLockTokioMutexStd, TorrentsRwLockTokioMutexTokio,
10+
TorrentsRwLockTokioMutexStd, TorrentsRwLockTokioMutexTokio, TorrentsSkipMapMutexStd,
1111
};
1212

1313
#[derive(Debug)]
1414
pub(crate) enum Repo {
15-
Std(TorrentsRwLockStd),
16-
StdMutexStd(TorrentsRwLockStdMutexStd),
17-
StdMutexTokio(TorrentsRwLockStdMutexTokio),
18-
Tokio(TorrentsRwLockTokio),
19-
TokioMutexStd(TorrentsRwLockTokioMutexStd),
20-
TokioMutexTokio(TorrentsRwLockTokioMutexTokio),
15+
RwLockStd(TorrentsRwLockStd),
16+
RwLockStdMutexStd(TorrentsRwLockStdMutexStd),
17+
RwLockStdMutexTokio(TorrentsRwLockStdMutexTokio),
18+
RwLockTokio(TorrentsRwLockTokio),
19+
RwLockTokioMutexStd(TorrentsRwLockTokioMutexStd),
20+
RwLockTokioMutexTokio(TorrentsRwLockTokioMutexTokio),
21+
SkipMapMutexStd(TorrentsSkipMapMutexStd),
2122
}
2223

2324
impl Repo {
2425
pub(crate) async fn get(&self, key: &InfoHash) -> Option<EntrySingle> {
2526
match self {
26-
Repo::Std(repo) => repo.get(key),
27-
Repo::StdMutexStd(repo) => Some(repo.get(key)?.lock().unwrap().clone()),
28-
Repo::StdMutexTokio(repo) => Some(repo.get(key).await?.lock().await.clone()),
29-
Repo::Tokio(repo) => repo.get(key).await,
30-
Repo::TokioMutexStd(repo) => Some(repo.get(key).await?.lock().unwrap().clone()),
31-
Repo::TokioMutexTokio(repo) => Some(repo.get(key).await?.lock().await.clone()),
27+
Repo::RwLockStd(repo) => repo.get(key),
28+
Repo::RwLockStdMutexStd(repo) => Some(repo.get(key)?.lock().unwrap().clone()),
29+
Repo::RwLockStdMutexTokio(repo) => Some(repo.get(key).await?.lock().await.clone()),
30+
Repo::RwLockTokio(repo) => repo.get(key).await,
31+
Repo::RwLockTokioMutexStd(repo) => Some(repo.get(key).await?.lock().unwrap().clone()),
32+
Repo::RwLockTokioMutexTokio(repo) => Some(repo.get(key).await?.lock().await.clone()),
33+
Repo::SkipMapMutexStd(repo) => Some(repo.get(key)?.lock().unwrap().clone()),
3234
}
3335
}
36+
3437
pub(crate) async fn get_metrics(&self) -> TorrentsMetrics {
3538
match self {
36-
Repo::Std(repo) => repo.get_metrics(),
37-
Repo::StdMutexStd(repo) => repo.get_metrics(),
38-
Repo::StdMutexTokio(repo) => repo.get_metrics().await,
39-
Repo::Tokio(repo) => repo.get_metrics().await,
40-
Repo::TokioMutexStd(repo) => repo.get_metrics().await,
41-
Repo::TokioMutexTokio(repo) => repo.get_metrics().await,
39+
Repo::RwLockStd(repo) => repo.get_metrics(),
40+
Repo::RwLockStdMutexStd(repo) => repo.get_metrics(),
41+
Repo::RwLockStdMutexTokio(repo) => repo.get_metrics().await,
42+
Repo::RwLockTokio(repo) => repo.get_metrics().await,
43+
Repo::RwLockTokioMutexStd(repo) => repo.get_metrics().await,
44+
Repo::RwLockTokioMutexTokio(repo) => repo.get_metrics().await,
45+
Repo::SkipMapMutexStd(repo) => repo.get_metrics(),
4246
}
4347
}
48+
4449
pub(crate) async fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntrySingle)> {
4550
match self {
46-
Repo::Std(repo) => repo.get_paginated(pagination),
47-
Repo::StdMutexStd(repo) => repo
51+
Repo::RwLockStd(repo) => repo.get_paginated(pagination),
52+
Repo::RwLockStdMutexStd(repo) => repo
4853
.get_paginated(pagination)
4954
.iter()
5055
.map(|(i, t)| (*i, t.lock().expect("it should get a lock").clone()))
5156
.collect(),
52-
Repo::StdMutexTokio(repo) => {
57+
Repo::RwLockStdMutexTokio(repo) => {
5358
let mut v: Vec<(InfoHash, EntrySingle)> = vec![];
5459

5560
for (i, t) in repo.get_paginated(pagination).await {
5661
v.push((i, t.lock().await.clone()));
5762
}
5863
v
5964
}
60-
Repo::Tokio(repo) => repo.get_paginated(pagination).await,
61-
Repo::TokioMutexStd(repo) => repo
65+
Repo::RwLockTokio(repo) => repo.get_paginated(pagination).await,
66+
Repo::RwLockTokioMutexStd(repo) => repo
6267
.get_paginated(pagination)
6368
.await
6469
.iter()
6570
.map(|(i, t)| (*i, t.lock().expect("it should get a lock").clone()))
6671
.collect(),
67-
Repo::TokioMutexTokio(repo) => {
72+
Repo::RwLockTokioMutexTokio(repo) => {
6873
let mut v: Vec<(InfoHash, EntrySingle)> = vec![];
6974

7075
for (i, t) in repo.get_paginated(pagination).await {
7176
v.push((i, t.lock().await.clone()));
7277
}
7378
v
7479
}
80+
Repo::SkipMapMutexStd(repo) => repo
81+
.get_paginated(pagination)
82+
.iter()
83+
.map(|(i, t)| (*i, t.lock().expect("it should get a lock").clone()))
84+
.collect(),
7585
}
7686
}
87+
7788
pub(crate) async fn import_persistent(&self, persistent_torrents: &PersistentTorrents) {
7889
match self {
79-
Repo::Std(repo) => repo.import_persistent(persistent_torrents),
80-
Repo::StdMutexStd(repo) => repo.import_persistent(persistent_torrents),
81-
Repo::StdMutexTokio(repo) => repo.import_persistent(persistent_torrents).await,
82-
Repo::Tokio(repo) => repo.import_persistent(persistent_torrents).await,
83-
Repo::TokioMutexStd(repo) => repo.import_persistent(persistent_torrents).await,
84-
Repo::TokioMutexTokio(repo) => repo.import_persistent(persistent_torrents).await,
90+
Repo::RwLockStd(repo) => repo.import_persistent(persistent_torrents),
91+
Repo::RwLockStdMutexStd(repo) => repo.import_persistent(persistent_torrents),
92+
Repo::RwLockStdMutexTokio(repo) => repo.import_persistent(persistent_torrents).await,
93+
Repo::RwLockTokio(repo) => repo.import_persistent(persistent_torrents).await,
94+
Repo::RwLockTokioMutexStd(repo) => repo.import_persistent(persistent_torrents).await,
95+
Repo::RwLockTokioMutexTokio(repo) => repo.import_persistent(persistent_torrents).await,
96+
Repo::SkipMapMutexStd(repo) => repo.import_persistent(persistent_torrents),
8597
}
8698
}
99+
87100
pub(crate) async fn remove(&self, key: &InfoHash) -> Option<EntrySingle> {
88101
match self {
89-
Repo::Std(repo) => repo.remove(key),
90-
Repo::StdMutexStd(repo) => Some(repo.remove(key)?.lock().unwrap().clone()),
91-
Repo::StdMutexTokio(repo) => Some(repo.remove(key).await?.lock().await.clone()),
92-
Repo::Tokio(repo) => repo.remove(key).await,
93-
Repo::TokioMutexStd(repo) => Some(repo.remove(key).await?.lock().unwrap().clone()),
94-
Repo::TokioMutexTokio(repo) => Some(repo.remove(key).await?.lock().await.clone()),
102+
Repo::RwLockStd(repo) => repo.remove(key),
103+
Repo::RwLockStdMutexStd(repo) => Some(repo.remove(key)?.lock().unwrap().clone()),
104+
Repo::RwLockStdMutexTokio(repo) => Some(repo.remove(key).await?.lock().await.clone()),
105+
Repo::RwLockTokio(repo) => repo.remove(key).await,
106+
Repo::RwLockTokioMutexStd(repo) => Some(repo.remove(key).await?.lock().unwrap().clone()),
107+
Repo::RwLockTokioMutexTokio(repo) => Some(repo.remove(key).await?.lock().await.clone()),
108+
Repo::SkipMapMutexStd(repo) => Some(repo.remove(key)?.lock().unwrap().clone()),
95109
}
96110
}
111+
97112
pub(crate) async fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) {
98113
match self {
99-
Repo::Std(repo) => repo.remove_inactive_peers(current_cutoff),
100-
Repo::StdMutexStd(repo) => repo.remove_inactive_peers(current_cutoff),
101-
Repo::StdMutexTokio(repo) => repo.remove_inactive_peers(current_cutoff).await,
102-
Repo::Tokio(repo) => repo.remove_inactive_peers(current_cutoff).await,
103-
Repo::TokioMutexStd(repo) => repo.remove_inactive_peers(current_cutoff).await,
104-
Repo::TokioMutexTokio(repo) => repo.remove_inactive_peers(current_cutoff).await,
114+
Repo::RwLockStd(repo) => repo.remove_inactive_peers(current_cutoff),
115+
Repo::RwLockStdMutexStd(repo) => repo.remove_inactive_peers(current_cutoff),
116+
Repo::RwLockStdMutexTokio(repo) => repo.remove_inactive_peers(current_cutoff).await,
117+
Repo::RwLockTokio(repo) => repo.remove_inactive_peers(current_cutoff).await,
118+
Repo::RwLockTokioMutexStd(repo) => repo.remove_inactive_peers(current_cutoff).await,
119+
Repo::RwLockTokioMutexTokio(repo) => repo.remove_inactive_peers(current_cutoff).await,
120+
Repo::SkipMapMutexStd(repo) => repo.remove_inactive_peers(current_cutoff),
105121
}
106122
}
123+
107124
pub(crate) async fn remove_peerless_torrents(&self, policy: &TrackerPolicy) {
108125
match self {
109-
Repo::Std(repo) => repo.remove_peerless_torrents(policy),
110-
Repo::StdMutexStd(repo) => repo.remove_peerless_torrents(policy),
111-
Repo::StdMutexTokio(repo) => repo.remove_peerless_torrents(policy).await,
112-
Repo::Tokio(repo) => repo.remove_peerless_torrents(policy).await,
113-
Repo::TokioMutexStd(repo) => repo.remove_peerless_torrents(policy).await,
114-
Repo::TokioMutexTokio(repo) => repo.remove_peerless_torrents(policy).await,
126+
Repo::RwLockStd(repo) => repo.remove_peerless_torrents(policy),
127+
Repo::RwLockStdMutexStd(repo) => repo.remove_peerless_torrents(policy),
128+
Repo::RwLockStdMutexTokio(repo) => repo.remove_peerless_torrents(policy).await,
129+
Repo::RwLockTokio(repo) => repo.remove_peerless_torrents(policy).await,
130+
Repo::RwLockTokioMutexStd(repo) => repo.remove_peerless_torrents(policy).await,
131+
Repo::RwLockTokioMutexTokio(repo) => repo.remove_peerless_torrents(policy).await,
132+
Repo::SkipMapMutexStd(repo) => repo.remove_peerless_torrents(policy),
115133
}
116134
}
135+
117136
pub(crate) async fn update_torrent_with_peer_and_get_stats(
118137
&self,
119138
info_hash: &InfoHash,
120139
peer: &peer::Peer,
121140
) -> (bool, SwarmMetadata) {
122141
match self {
123-
Repo::Std(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer),
124-
Repo::StdMutexStd(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer),
125-
Repo::StdMutexTokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await,
126-
Repo::Tokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await,
127-
Repo::TokioMutexStd(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await,
128-
Repo::TokioMutexTokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await,
142+
Repo::RwLockStd(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer),
143+
Repo::RwLockStdMutexStd(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer),
144+
Repo::RwLockStdMutexTokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await,
145+
Repo::RwLockTokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await,
146+
Repo::RwLockTokioMutexStd(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await,
147+
Repo::RwLockTokioMutexTokio(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer).await,
148+
Repo::SkipMapMutexStd(repo) => repo.update_torrent_with_peer_and_get_stats(info_hash, peer),
129149
}
130150
}
151+
131152
pub(crate) async fn insert(&self, info_hash: &InfoHash, torrent: EntrySingle) -> Option<EntrySingle> {
132153
match self {
133-
Repo::Std(repo) => repo.write().insert(*info_hash, torrent),
134-
Repo::StdMutexStd(repo) => Some(repo.write().insert(*info_hash, torrent.into())?.lock().unwrap().clone()),
135-
Repo::StdMutexTokio(repo) => {
136-
let r = repo.write().insert(*info_hash, torrent.into());
137-
match r {
138-
Some(t) => Some(t.lock().await.clone()),
139-
None => None,
140-
}
154+
Repo::RwLockStd(repo) => {
155+
repo.write().insert(*info_hash, torrent);
141156
}
142-
Repo::Tokio(repo) => repo.write().await.insert(*info_hash, torrent),
143-
Repo::TokioMutexStd(repo) => Some(repo.write().await.insert(*info_hash, torrent.into())?.lock().unwrap().clone()),
144-
Repo::TokioMutexTokio(repo) => Some(repo.write().await.insert(*info_hash, torrent.into())?.lock().await.clone()),
145-
}
157+
Repo::RwLockStdMutexStd(repo) => {
158+
repo.write().insert(*info_hash, torrent.into());
159+
}
160+
Repo::RwLockStdMutexTokio(repo) => {
161+
repo.write().insert(*info_hash, torrent.into());
162+
}
163+
Repo::RwLockTokio(repo) => {
164+
repo.write().await.insert(*info_hash, torrent);
165+
}
166+
Repo::RwLockTokioMutexStd(repo) => {
167+
repo.write().await.insert(*info_hash, torrent.into());
168+
}
169+
Repo::RwLockTokioMutexTokio(repo) => {
170+
repo.write().await.insert(*info_hash, torrent.into());
171+
}
172+
Repo::SkipMapMutexStd(repo) => {
173+
repo.torrents.insert(*info_hash, torrent.into());
174+
}
175+
};
176+
self.get(info_hash).await
146177
}
147178
}

0 commit comments

Comments
 (0)