Skip to content

Commit 7d3c4ad

Browse files
author
Shrey Patel
committed
Split MediaStore trait from EventCacheStore.
Rename `EventCacheStoreMedia` to `MediaStoreInner`. Move relevant tests into `MediaStoreIntegrationTests`.
1 parent 472b934 commit 7d3c4ad

File tree

10 files changed

+519
-497
lines changed

10 files changed

+519
-497
lines changed

crates/matrix-sdk-base/src/event_cache/store/integration_tests.rs

Lines changed: 6 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,17 @@ use matrix_sdk_common::{
2929
};
3030
use matrix_sdk_test::{ALICE, DEFAULT_TEST_ROOM_ID, event_factory::EventFactory};
3131
use ruma::{
32-
EventId, RoomId,
33-
api::client::media::get_content_thumbnail::v3::Method,
34-
event_id,
32+
EventId, RoomId, event_id,
3533
events::{
36-
AnyMessageLikeEvent, AnyTimelineEvent,
37-
relation::RelationType,
38-
room::{MediaSource, message::RoomMessageEventContentWithoutRelation},
34+
AnyMessageLikeEvent, AnyTimelineEvent, relation::RelationType,
35+
room::message::RoomMessageEventContentWithoutRelation,
3936
},
40-
mxc_uri,
4137
push::Action,
42-
room_id, uint,
38+
room_id,
4339
};
4440

45-
use super::{DynEventCacheStore, media::IgnoreMediaRetentionPolicy};
46-
use crate::{
47-
event_cache::{Gap, store::DEFAULT_CHUNK_CAPACITY},
48-
media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings},
49-
};
41+
use super::DynEventCacheStore;
42+
use crate::event_cache::{Gap, store::DEFAULT_CHUNK_CAPACITY};
5043

5144
/// Create a test event with all data filled, for testing that linked chunk
5245
/// correctly stores event data.
@@ -118,12 +111,6 @@ pub fn check_test_event(event: &TimelineEvent, text: &str) {
118111
/// `event_cache_store_integration_tests!` macro.
119112
#[allow(async_fn_in_trait)]
120113
pub trait EventCacheStoreIntegrationTests {
121-
/// Test media content storage.
122-
async fn test_media_content(&self);
123-
124-
/// Test replacing a MXID.
125-
async fn test_replace_media_key(&self);
126-
127114
/// Test handling updates to a linked chunk and reloading these updates from
128115
/// the store.
129116
async fn test_handle_updates_and_rebuild_linked_chunk(&self);
@@ -163,190 +150,6 @@ pub trait EventCacheStoreIntegrationTests {
163150
}
164151

165152
impl EventCacheStoreIntegrationTests for DynEventCacheStore {
166-
async fn test_media_content(&self) {
167-
let uri = mxc_uri!("mxc://localhost/media");
168-
let request_file = MediaRequestParameters {
169-
source: MediaSource::Plain(uri.to_owned()),
170-
format: MediaFormat::File,
171-
};
172-
let request_thumbnail = MediaRequestParameters {
173-
source: MediaSource::Plain(uri.to_owned()),
174-
format: MediaFormat::Thumbnail(MediaThumbnailSettings::with_method(
175-
Method::Crop,
176-
uint!(100),
177-
uint!(100),
178-
)),
179-
};
180-
181-
let other_uri = mxc_uri!("mxc://localhost/media-other");
182-
let request_other_file = MediaRequestParameters {
183-
source: MediaSource::Plain(other_uri.to_owned()),
184-
format: MediaFormat::File,
185-
};
186-
187-
let content: Vec<u8> = "hello".into();
188-
let thumbnail_content: Vec<u8> = "world".into();
189-
let other_content: Vec<u8> = "foo".into();
190-
191-
// Media isn't present in the cache.
192-
assert!(
193-
self.get_media_content(&request_file).await.unwrap().is_none(),
194-
"unexpected media found"
195-
);
196-
assert!(
197-
self.get_media_content(&request_thumbnail).await.unwrap().is_none(),
198-
"media not found"
199-
);
200-
201-
// Let's add the media.
202-
self.add_media_content(&request_file, content.clone(), IgnoreMediaRetentionPolicy::No)
203-
.await
204-
.expect("adding media failed");
205-
206-
// Media is present in the cache.
207-
assert_eq!(
208-
self.get_media_content(&request_file).await.unwrap().as_ref(),
209-
Some(&content),
210-
"media not found though added"
211-
);
212-
assert_eq!(
213-
self.get_media_content_for_uri(uri).await.unwrap().as_ref(),
214-
Some(&content),
215-
"media not found by URI though added"
216-
);
217-
218-
// Let's remove the media.
219-
self.remove_media_content(&request_file).await.expect("removing media failed");
220-
221-
// Media isn't present in the cache.
222-
assert!(
223-
self.get_media_content(&request_file).await.unwrap().is_none(),
224-
"media still there after removing"
225-
);
226-
assert!(
227-
self.get_media_content_for_uri(uri).await.unwrap().is_none(),
228-
"media still found by URI after removing"
229-
);
230-
231-
// Let's add the media again.
232-
self.add_media_content(&request_file, content.clone(), IgnoreMediaRetentionPolicy::No)
233-
.await
234-
.expect("adding media again failed");
235-
236-
assert_eq!(
237-
self.get_media_content(&request_file).await.unwrap().as_ref(),
238-
Some(&content),
239-
"media not found after adding again"
240-
);
241-
242-
// Let's add the thumbnail media.
243-
self.add_media_content(
244-
&request_thumbnail,
245-
thumbnail_content.clone(),
246-
IgnoreMediaRetentionPolicy::No,
247-
)
248-
.await
249-
.expect("adding thumbnail failed");
250-
251-
// Media's thumbnail is present.
252-
assert_eq!(
253-
self.get_media_content(&request_thumbnail).await.unwrap().as_ref(),
254-
Some(&thumbnail_content),
255-
"thumbnail not found"
256-
);
257-
258-
// We get a file with the URI, we don't know which one.
259-
assert!(
260-
self.get_media_content_for_uri(uri).await.unwrap().is_some(),
261-
"media not found by URI though two where added"
262-
);
263-
264-
// Let's add another media with a different URI.
265-
self.add_media_content(
266-
&request_other_file,
267-
other_content.clone(),
268-
IgnoreMediaRetentionPolicy::No,
269-
)
270-
.await
271-
.expect("adding other media failed");
272-
273-
// Other file is present.
274-
assert_eq!(
275-
self.get_media_content(&request_other_file).await.unwrap().as_ref(),
276-
Some(&other_content),
277-
"other file not found"
278-
);
279-
assert_eq!(
280-
self.get_media_content_for_uri(other_uri).await.unwrap().as_ref(),
281-
Some(&other_content),
282-
"other file not found by URI"
283-
);
284-
285-
// Let's remove media based on URI.
286-
self.remove_media_content_for_uri(uri).await.expect("removing all media for uri failed");
287-
288-
assert!(
289-
self.get_media_content(&request_file).await.unwrap().is_none(),
290-
"media wasn't removed"
291-
);
292-
assert!(
293-
self.get_media_content(&request_thumbnail).await.unwrap().is_none(),
294-
"thumbnail wasn't removed"
295-
);
296-
assert!(
297-
self.get_media_content(&request_other_file).await.unwrap().is_some(),
298-
"other media was removed"
299-
);
300-
assert!(
301-
self.get_media_content_for_uri(uri).await.unwrap().is_none(),
302-
"media found by URI wasn't removed"
303-
);
304-
assert!(
305-
self.get_media_content_for_uri(other_uri).await.unwrap().is_some(),
306-
"other media found by URI was removed"
307-
);
308-
}
309-
310-
async fn test_replace_media_key(&self) {
311-
let uri = mxc_uri!("mxc://sendqueue.local/tr4n-s4ct-10n1-d");
312-
let req = MediaRequestParameters {
313-
source: MediaSource::Plain(uri.to_owned()),
314-
format: MediaFormat::File,
315-
};
316-
317-
let content = "hello".as_bytes().to_owned();
318-
319-
// Media isn't present in the cache.
320-
assert!(self.get_media_content(&req).await.unwrap().is_none(), "unexpected media found");
321-
322-
// Add the media.
323-
self.add_media_content(&req, content.clone(), IgnoreMediaRetentionPolicy::No)
324-
.await
325-
.expect("adding media failed");
326-
327-
// Sanity-check: media is found after adding it.
328-
assert_eq!(self.get_media_content(&req).await.unwrap().unwrap(), b"hello");
329-
330-
// Replacing a media request works.
331-
let new_uri = mxc_uri!("mxc://matrix.org/tr4n-s4ct-10n1-d");
332-
let new_req = MediaRequestParameters {
333-
source: MediaSource::Plain(new_uri.to_owned()),
334-
format: MediaFormat::File,
335-
};
336-
self.replace_media_key(&req, &new_req)
337-
.await
338-
.expect("replacing the media request key failed");
339-
340-
// Finding with the previous request doesn't work anymore.
341-
assert!(
342-
self.get_media_content(&req).await.unwrap().is_none(),
343-
"unexpected media found with the old key"
344-
);
345-
346-
// Finding with the new request does work.
347-
assert_eq!(self.get_media_content(&new_req).await.unwrap().unwrap(), b"hello");
348-
}
349-
350153
async fn test_handle_updates_and_rebuild_linked_chunk(&self) {
351154
let room_id = room_id!("!r0:matrix.org");
352155
let linked_chunk_id = LinkedChunkId::Room(room_id);
@@ -1333,20 +1136,6 @@ macro_rules! event_cache_store_integration_tests {
13331136

13341137
use super::get_event_cache_store;
13351138

1336-
#[async_test]
1337-
async fn test_media_content() {
1338-
let event_cache_store =
1339-
get_event_cache_store().await.unwrap().into_event_cache_store();
1340-
event_cache_store.test_media_content().await;
1341-
}
1342-
1343-
#[async_test]
1344-
async fn test_replace_media_key() {
1345-
let event_cache_store =
1346-
get_event_cache_store().await.unwrap().into_event_cache_store();
1347-
event_cache_store.test_replace_media_key().await;
1348-
}
1349-
13501139
#[async_test]
13511140
async fn test_handle_updates_and_rebuild_linked_chunk() {
13521141
let event_cache_store =

crates/matrix-sdk-base/src/event_cache/store/memory_store.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ use tracing::error;
3636

3737
use super::{
3838
EventCacheStore, EventCacheStoreError, Result, compute_filters_string, extract_event_relation,
39-
media::{EventCacheStoreMedia, IgnoreMediaRetentionPolicy, MediaRetentionPolicy, MediaService},
4039
};
4140
use crate::{
4241
event_cache::{Event, Gap},
43-
media::{MediaRequestParameters, UniqueKey as _},
42+
media::{
43+
MediaRequestParameters, UniqueKey as _,
44+
store::{IgnoreMediaRetentionPolicy, MediaRetentionPolicy, MediaService, MediaStoreInner},
45+
},
4446
};
4547

4648
/// In-memory, non-persistent implementation of the `EventCacheStore`.
@@ -372,7 +374,7 @@ impl EventCacheStore for MemoryStore {
372374

373375
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
374376
#[cfg_attr(not(target_family = "wasm"), async_trait)]
375-
impl EventCacheStoreMedia for MemoryStore {
377+
impl MediaStoreInner for MemoryStore {
376378
type Error = EventCacheStoreError;
377379

378380
async fn media_retention_policy_inner(
@@ -578,13 +580,13 @@ impl EventCacheStoreMedia for MemoryStore {
578580
#[cfg(test)]
579581
mod tests {
580582
use super::{MemoryStore, Result};
581-
use crate::event_cache_store_media_integration_tests;
583+
use crate::media_store_inner_integration_tests;
582584

583585
async fn get_event_cache_store() -> Result<MemoryStore> {
584586
Ok(MemoryStore::new())
585587
}
586588

587589
event_cache_store_integration_tests!();
588590
event_cache_store_integration_tests_time!();
589-
event_cache_store_media_integration_tests!(with_media_size_tests);
591+
media_store_inner_integration_tests!(with_media_size_tests);
590592
}

crates/matrix-sdk-base/src/event_cache/store/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use std::{fmt, ops::Deref, str::Utf8Error, sync::Arc};
2424
#[cfg(any(test, feature = "testing"))]
2525
#[macro_use]
2626
pub mod integration_tests;
27-
pub mod media;
2827
mod memory_store;
2928
mod traits;
3029

0 commit comments

Comments
 (0)