mithril_aggregator/file_uploaders/
dumb_uploader.rs

1use anyhow::anyhow;
2use async_trait::async_trait;
3use mithril_common::{StdResult, entities::FileUri};
4use std::{path::Path, sync::RwLock};
5
6use crate::file_uploaders::{FileUploadRetryPolicy, FileUploader};
7
8/// Dummy uploader for test purposes.
9///
10/// It actually does NOT upload any file but keep the history of all the files it
11/// was asked to upload. This is intended to by used by integration tests.
12pub struct DumbUploader {
13    uploads_history: RwLock<Vec<FileUri>>,
14    retry_policy: FileUploadRetryPolicy,
15}
16
17impl DumbUploader {
18    /// Create a new instance with a custom retry policy.
19    pub fn new(retry_policy: FileUploadRetryPolicy) -> Self {
20        Self {
21            uploads_history: RwLock::new(Vec::new()),
22            retry_policy,
23        }
24    }
25
26    /// Return the last upload that was triggered.
27    pub fn get_last_upload(&self) -> StdResult<Option<FileUri>> {
28        let last_upload = self.get_last_n_uploads(1)?;
29        Ok(last_upload.first().cloned())
30    }
31
32    /// Return the last `n` uploads that were triggered in anti-chronological order.
33    pub fn get_last_n_uploads(&self, n: usize) -> StdResult<Vec<FileUri>> {
34        let value = self
35            .uploads_history
36            .read()
37            .map_err(|e| anyhow!(e.to_string()).context("Error while reading filepath location"))?;
38
39        Ok(value.iter().rev().take(n).cloned().collect::<Vec<FileUri>>())
40    }
41}
42
43impl Default for DumbUploader {
44    fn default() -> Self {
45        Self::new(FileUploadRetryPolicy::never())
46    }
47}
48
49#[async_trait]
50impl FileUploader for DumbUploader {
51    /// Upload a file
52    async fn upload_without_retry(&self, filepath: &Path) -> StdResult<FileUri> {
53        let mut uploads_history = self
54            .uploads_history
55            .write()
56            .map_err(|e| anyhow!("Error while saving filepath location: {e}"))?;
57
58        let location = FileUri(filepath.to_string_lossy().to_string());
59        uploads_history.push(location.clone());
60
61        Ok(location)
62    }
63
64    fn retry_policy(&self) -> FileUploadRetryPolicy {
65        self.retry_policy.clone()
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use std::time::Duration;
72
73    use super::*;
74
75    #[tokio::test]
76    async fn test_dumb_uploader() {
77        let uploader = DumbUploader::default();
78        assert!(
79            uploader
80                .get_last_upload()
81                .expect("uploader should not fail")
82                .is_none()
83        );
84        let res = uploader
85            .upload(Path::new("/tmp/whatever"))
86            .await
87            .expect("uploading with a dumb uploader should not fail");
88        assert_eq!(res, FileUri("/tmp/whatever".to_string()));
89        assert_eq!(
90            Some(FileUri("/tmp/whatever".to_string())),
91            uploader
92                .get_last_upload()
93                .expect("getting dumb uploader last value after a fake download should not fail")
94        );
95    }
96
97    #[tokio::test]
98    async fn get_history_of_multiple_uploads() {
99        let uploader = DumbUploader::default();
100        assert_eq!(
101            Vec::<FileUri>::new(),
102            uploader.get_last_n_uploads(usize::MAX).unwrap()
103        );
104
105        uploader.upload(Path::new("/tmp/whatever")).await.unwrap();
106        uploader.upload(Path::new("/tmp/whatever2")).await.unwrap();
107        uploader.upload(Path::new("/tmp/whatever3")).await.unwrap();
108
109        assert_eq!(
110            vec![
111                FileUri("/tmp/whatever3".to_string()),
112                FileUri("/tmp/whatever2".to_string()),
113                FileUri("/tmp/whatever".to_string()),
114            ],
115            uploader.get_last_n_uploads(usize::MAX).unwrap()
116        );
117
118        assert_eq!(
119            vec![
120                FileUri("/tmp/whatever3".to_string()),
121                FileUri("/tmp/whatever2".to_string()),
122            ],
123            uploader.get_last_n_uploads(2).unwrap()
124        );
125    }
126
127    #[tokio::test]
128    async fn retry_policy_from_file_uploader_trait_should_be_implemented() {
129        let expected_policy = FileUploadRetryPolicy {
130            attempts: 10,
131            delay_between_attempts: Duration::from_millis(123),
132        };
133
134        let uploader: Box<dyn FileUploader> = Box::new(DumbUploader::new(expected_policy.clone()));
135
136        assert_eq!(expected_policy, uploader.retry_policy());
137    }
138}