mithril_aggregator/file_uploaders/
interface.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use async_trait::async_trait;
use mithril_common::{entities::FileUri, StdResult};
use std::{path::Path, time::Duration};

/// Policy for retrying file uploads.
#[derive(Debug, PartialEq, Clone)]
pub struct FileUploadRetryPolicy {
    /// Number of attempts to upload a file.
    pub attempts: usize,
    /// Delay between two attempts.
    pub delay_between_attempts: Duration,
}

impl FileUploadRetryPolicy {
    /// Create a policy that never retries.
    pub fn never() -> Self {
        Self {
            attempts: 1,
            delay_between_attempts: Duration::from_secs(0),
        }
    }
}

impl Default for FileUploadRetryPolicy {
    /// Create a default retry policy.
    fn default() -> Self {
        Self {
            attempts: 3,
            delay_between_attempts: Duration::from_secs(5),
        }
    }
}

/// FileUploader represents a file uploader interactor.
/// It retries the upload operation according to the retry policy.
#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait FileUploader: Sync + Send {
    /// Try to upload once.
    async fn upload_without_retry(&self, filepath: &Path) -> StdResult<FileUri>;

    /// Get the retry policy for this uploader.
    fn retry_policy(&self) -> FileUploadRetryPolicy {
        FileUploadRetryPolicy::never()
    }

    /// Upload a file with retries according to the retry policy.
    async fn upload(&self, filepath: &Path) -> StdResult<FileUri> {
        let retry_policy = self.retry_policy();

        let mut nb_attempts = 0;
        loop {
            nb_attempts += 1;
            match self.upload_without_retry(filepath).await {
                Ok(result) => return Ok(result),
                Err(_) if nb_attempts >= retry_policy.attempts => {
                    return Err(anyhow::anyhow!(
                        "Upload failed after {} attempts",
                        nb_attempts
                    ));
                }
                _ => tokio::time::sleep(retry_policy.delay_between_attempts).await,
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{path::PathBuf, time::Instant};

    use super::*;
    use anyhow::anyhow;
    use mockall::{mock, predicate::eq};

    mock! {
        TestFileUploaderWithDefaultRetryPolicy {
        }
        #[async_trait]
        impl FileUploader for TestFileUploaderWithDefaultRetryPolicy {
            async fn upload_without_retry(&self, filepath: &Path) -> StdResult<FileUri>;
        }
    }

    mock! {
        TestFileUploader {
        }

        #[async_trait]
        impl FileUploader for TestFileUploader {
            async fn upload_without_retry(&self, filepath: &Path) -> StdResult<FileUri>;
            fn retry_policy(&self) -> FileUploadRetryPolicy;
        }
    }

    #[tokio::test]
    async fn upload_return_the_result_of_upload_without_retry() {
        let mut uploader = MockTestFileUploaderWithDefaultRetryPolicy::new();
        uploader
            .expect_upload_without_retry()
            .with(eq(PathBuf::from("file_to_upload")))
            .times(1)
            .returning(|_| Ok(FileUri("file_uploaded".to_string())));

        let file_uploaded = uploader.upload(Path::new("file_to_upload")).await.unwrap();
        assert_eq!(FileUri("file_uploaded".to_string()), file_uploaded);
    }

    #[tokio::test]
    async fn when_upload_fails_do_not_retry_by_default() {
        let mut uploader = MockTestFileUploaderWithDefaultRetryPolicy::new();
        uploader
            .expect_upload_without_retry()
            .with(eq(PathBuf::from("file_to_upload")))
            .times(1)
            .returning(|_| Err(anyhow!("Failure while uploading...")));

        uploader
            .upload(Path::new("file_to_upload"))
            .await
            .expect_err("Should fail on upload");
    }

    #[tokio::test]
    async fn should_retry_if_fail() {
        let mut uploader = MockTestFileUploader::new();

        uploader
            .expect_retry_policy()
            .returning(|| FileUploadRetryPolicy {
                attempts: 50,
                delay_between_attempts: Duration::ZERO,
            });

        uploader
            .expect_upload_without_retry()
            .with(eq(PathBuf::from("file_to_upload")))
            .times(2)
            .returning(|_| Err(anyhow!("Failure while uploading...")));
        uploader
            .expect_upload_without_retry()
            .with(eq(PathBuf::from("file_to_upload")))
            .times(1)
            .returning(|_| Ok(FileUri("file_uploaded".to_string())));

        let file_uploaded = uploader.upload(Path::new("file_to_upload")).await.unwrap();
        assert_eq!(FileUri("file_uploaded".to_string()), file_uploaded);
    }

    #[tokio::test]
    async fn should_recall_a_failing_inner_uploader_up_to_the_limit() {
        let mut uploader = MockTestFileUploader::new();

        uploader
            .expect_retry_policy()
            .returning(|| FileUploadRetryPolicy {
                attempts: 4,
                delay_between_attempts: Duration::ZERO,
            });

        uploader
            .expect_upload_without_retry()
            .with(eq(PathBuf::from("file_to_upload")))
            .times(4)
            .returning(|_| Err(anyhow!("Failure while uploading...")));

        uploader
            .upload(&PathBuf::from("file_to_upload"))
            .await
            .expect_err("An error should be returned when all retries are done");
    }

    #[tokio::test]
    async fn should_delay_between_retries() {
        let mut uploader = MockTestFileUploader::new();

        let delay = Duration::from_millis(50);
        uploader
            .expect_retry_policy()
            .returning(move || FileUploadRetryPolicy {
                attempts: 4,
                delay_between_attempts: delay,
            });

        uploader
            .expect_upload_without_retry()
            .times(4)
            .returning(move |_| Err(anyhow!("Failure while uploading...")));

        let start = Instant::now();
        uploader
            .upload(&PathBuf::from("file_to_upload"))
            .await
            .expect_err("An error should be returned when all retries are done");
        let duration = start.elapsed();

        assert!(
            duration >= delay * 3,
            "Duration should be at least 3 times the delay ({}ms) but was {}ms",
            delay.as_millis() * 3,
            duration.as_millis()
        );
        assert!(
            duration < delay * 4,
            "Duration should be less than 4 times the delay ({}ms) but was {}ms",
            delay.as_millis() * 4,
            duration.as_millis()
        );
    }
}