architecturekit 0.14.0

Building blocks for DDD-based applications with CQRS and event sourcing, on top of the EventSourcingDB by the native web.
Documentation
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// These tests mirror TestAFailedWrite and TestAStallWhileWaitingForTheAnswer of
// ArchitectureKit for Go, for executing and for writing, which fail alike. The
// failures come from a fake (see fake), since the client SDK does not let a
// test make them itself.

use std::time::Duration;

use eventsourcingdb::{Client, error::ClientError};

use super::{
    cause_of,
    commands::{Increment, KitWrite, SOURCE, chain, counter_decider},
    fake::{Answer, Api, Fake, Reply, dead_client},
};
use crate::{
    Categorized, Category, DomainError, Error, ExecuteError, Precondition, Store, Transient,
};

const READ_EVENTS: &str = "/api/v1/read-events";
const WRITE_EVENTS: &str = "/api/v1/write-events";

fn unconditionally() -> Vec<Precondition> {
    vec![Precondition::unconditionally()]
}

/// Writes a remark with the given note to /test in the given way, through a
/// database that refuses it.
async fn refused(write: KitWrite, client: Client, note: &str) -> Error<DomainError> {
    let store = Store::builder(client, SOURCE).build();

    write
        .remark(&store, "/test", note, unconditionally)
        .await
        .expect_err("the database refuses the write")
}

/// Asserts that a write failed with an unknown outcome, which belongs to no
/// category.
fn assert_outcome_unknown(failure: &Error<DomainError>, name: &str) {
    assert!(
        matches!(failure, Error::OutcomeUnknown(_)),
        "{name}: {failure:?}"
    );
    assert_eq!(
        failure.category(),
        None,
        "{name}: an unknown outcome belongs to no category"
    );
}

#[tokio::test]
async fn keeps_its_category_if_the_database_can_not_be_reached() {
    for write in KitWrite::ALL {
        // Executing reads before it writes, so its database has to answer the
        // read, and go away before the write.
        let client = match write {
            KitWrite::Execute => {
                Fake::new(Api::V1)
                    .vanishing_after(READ_EVENTS)
                    .client()
                    .await
            }
            KitWrite::Write => dead_client().await,
        };

        let failure = refused(write, client, "a remark").await;

        assert!(
            matches!(failure, Error::Transient(_)),
            "{write:?}: trying again can not store the events twice: {failure:?}"
        );
        assert!(
            failure
                .to_string()
                .starts_with(&format!("transient failure: {}", write.doing("/test"))),
            "{write:?}: {failure}"
        );
        assert!(
            chain(&failure)
                .to_lowercase()
                .contains("connection refused"),
            "{write:?}: {}",
            chain(&failure)
        );
    }
}

#[tokio::test]
async fn leaves_the_outcome_unknown_if_the_database_hung_up_while_the_request_was_sent() {
    // The client reports a write as certainly not stored only if it could not
    // connect, by the same rule in every SDK, so a request that was cut off
    // after connecting, but before all of it was sent, leaves the outcome
    // open, although the database can not have stored it.
    for write in KitWrite::ALL {
        let client = Fake::new(Api::V1)
            .route(WRITE_EVENTS, Answer::HangUpOnHeaders)
            .client()
            .await;

        let failure = refused(write, client, &"x".repeat(16 << 20)).await;

        assert_outcome_unknown(&failure, &format!("{write:?}"));
    }
}

#[tokio::test]
async fn keeps_its_category_if_the_database_refused_it_before_writing() {
    for (name, status, reason, category) in [
        (
            "a bad request is permanent",
            400,
            "bad request",
            Category::Permanent,
        ),
        (
            "a rejected API token is permanent",
            401,
            "unauthorized",
            Category::Permanent,
        ),
        (
            "a forbidden request is permanent",
            403,
            "forbidden",
            Category::Permanent,
        ),
        (
            "an unknown route is permanent",
            404,
            "not found",
            Category::Permanent,
        ),
        (
            "a failed precondition is a conflict",
            409,
            "state conflict: precondition failed",
            Category::Transient,
        ),
        (
            "a schema violation is permanent",
            409,
            "schema conflict: event does not match",
            Category::Permanent,
        ),
        (
            "a request that is too large is permanent",
            413,
            "too large",
            Category::Permanent,
        ),
        (
            "a media type that is not supported is permanent",
            415,
            "unsupported",
            Category::Permanent,
        ),
        (
            "a request that can not be processed is permanent",
            422,
            "unprocessable",
            Category::Permanent,
        ),
        (
            "too many requests are transient",
            429,
            "slow down",
            Category::Transient,
        ),
        (
            "a database that shuts down is transient",
            503,
            "server is shutting down",
            Category::Transient,
        ),
        (
            "a database without a valid license is transient",
            507,
            "insufficient storage",
            Category::Transient,
        ),
    ] {
        for write in KitWrite::ALL {
            let client = Fake::new(Api::V1)
                .route(WRITE_EVENTS, Answer::Reply(Reply::status(status, reason)))
                .client()
                .await;

            let failure = refused(write, client, "a remark").await;

            assert_eq!(
                failure.category(),
                Some(category),
                "{write:?}, {name}: {failure:?}"
            );
            assert_eq!(
                matches!(failure, Error::Transient(Transient::Conflict(_))),
                name.ends_with("is a conflict"),
                "{write:?}, {name}: {failure:?}"
            );
            let Some(ClientError::DbApiError(answer)) = cause_of(&failure) else {
                panic!("{write:?}, {name}: the source has to be the answer, got {failure:?}");
            };
            assert_eq!(answer.status, status, "{write:?}, {name}");
            assert_eq!(answer.description, reason, "{write:?}, {name}");
        }
    }
}

#[tokio::test]
async fn leaves_the_outcome_unknown_if_the_request_has_left_but_no_answer_says_that_nothing_was_written()
 {
    const WRITTEN: &str = r#"[{"specversion":"1.0","id":"0","source":"https://thenativeweb.io"}]"#;

    // Unlike the client SDK for Go, the one for Rust reports an error that
    // does not come from an EventSourcingDB as the answer itself, and keeps to
    // itself where it came from, so only an answer that reports success names
    // its origin.
    for (name, answer, detail) in [
        ("the connection breaks", Answer::HangUp, None),
        (
            "the answer is cut off",
            Answer::CutOff(Reply::status(200, WRITTEN)),
            None,
        ),
        (
            "the answer can not be decoded",
            Answer::Reply(Reply::status(200, r#"{"these are":"no events"}"#)),
            None,
        ),
        (
            "the database fails with 500, which may follow storing the events",
            Answer::Reply(Reply::status(
                500,
                "failed to write events: failed to flush the write-ahead log",
            )),
            Some("failed to flush the write-ahead log"),
        ),
        (
            "the answer is 502, which the database never sends",
            Answer::Reply(Reply::status(502, "bad gateway")),
            Some("bad gateway"),
        ),
        (
            "the answer is 504, which the database never sends",
            Answer::Reply(Reply::status(504, "gateway timeout")),
            Some("gateway timeout"),
        ),
        (
            "the answer does not come from an EventSourcingDB, such as a 502 of a proxy",
            Answer::Reply(Reply::foreign(502)),
            Some("bad gateway"),
        ),
        (
            "the answer does not come from an EventSourcingDB, such as a 504 of a proxy",
            Answer::Reply(Reply::foreign(504)),
            Some("bad gateway"),
        ),
        (
            "the answer does not come from an EventSourcingDB, although it reports success",
            Answer::Reply(Reply::foreign(200)),
            Some("the answer does not come from an EventSourcingDB"),
        ),
    ] {
        for write in KitWrite::ALL {
            let client = Fake::new(Api::V1)
                .route(WRITE_EVENTS, answer.clone())
                .client()
                .await;

            let failure = refused(write, client, "a remark").await;

            assert_outcome_unknown(&failure, &format!("{write:?}, {name}"));
            assert!(
                failure
                    .to_string()
                    .starts_with(&format!("outcome unknown: {}", write.doing("/test"))),
                "{write:?}, {name}: the message names the write after the outcome: {failure}"
            );
            if let Some(detail) = detail {
                assert!(
                    chain(&failure).contains(detail),
                    "{write:?}, {name}: {}",
                    chain(&failure)
                );
            }
        }
    }
}

#[tokio::test]
async fn keeps_the_error_of_the_client_reachable_when_the_outcome_is_unknown() {
    for write in KitWrite::ALL {
        let refusing = Fake::new(Api::V1)
            .route(WRITE_EVENTS, Answer::Reply(Reply::status(500, "failed")))
            .client()
            .await;
        // A proxy that reports success is no EventSourcingDB to the client.
        let proxied = Fake::new(Api::V1)
            .route(WRITE_EVENTS, Answer::Reply(Reply::foreign(200)))
            .client()
            .await;

        let failure = refused(write, refusing, "a remark").await;
        let Some(ClientError::DbApiError(answer)) = cause_of(&failure) else {
            panic!("{write:?}: the source has to be the answer, got {failure:?}");
        };
        assert_eq!(answer.status, 500, "{write:?}");
        assert_eq!(answer.description, "failed", "{write:?}");

        let failure = refused(write, proxied, "a remark").await;
        assert_outcome_unknown(&failure, &format!("{write:?}"));
        assert!(
            matches!(cause_of(&failure), Some(ClientError::InvalidServerHeader)),
            "{write:?}: {failure:?}"
        );
    }
}

#[tokio::test]
async fn follows_a_redirect_of_a_proxy_as_the_client_does() {
    // The client follows 301, 302, and 303 with a GET, which the database
    // refuses before writing. Unlike the client SDK for Go, the one for Rust
    // does not send the body once more for 307 and 308, and reports the
    // redirect itself, which does not come from an EventSourcingDB, so whether
    // the proxy passed the write on is unknown.
    for (status, is_unknown, answered) in [
        (301, false, 405),
        (302, false, 405),
        (303, false, 405),
        (307, true, 307),
        (308, true, 308),
    ] {
        for write in KitWrite::ALL {
            let client = Fake::new(Api::V1)
                .route(
                    WRITE_EVENTS,
                    Answer::Reply(Reply::redirect(status, "/moved/write-events")),
                )
                .route(
                    "/moved/write-events",
                    Answer::ToPost(Reply::status(500, "failed")),
                )
                .client()
                .await;

            let failure = refused(write, client, "a remark").await;
            let Some(ClientError::DbApiError(answer)) = cause_of(&failure) else {
                panic!("{write:?}, {status}: the source has to be the answer, got {failure:?}");
            };

            if is_unknown {
                assert_outcome_unknown(&failure, &format!("{write:?}, {status}"));
            } else {
                assert!(
                    matches!(failure, Error::Permanent(_)),
                    "{write:?}, {status}: the database refused the GET before writing: {failure:?}"
                );
            }
            assert_eq!(answer.status, answered, "{write:?}, {status}");
        }
    }
}

#[tokio::test]
async fn is_never_tried_again_with_an_unknown_outcome_not_even_by_a_store_that_decides_again_on_conflicts()
 {
    let (url, requests) = Fake::new(Api::V1)
        .route(WRITE_EVENTS, Answer::HangUp)
        .start()
        .await;
    let store = Store::builder(Client::new(url, "secret"), SOURCE)
        .conflict_retries(3)
        .build();

    let failure = store
        .execute(
            &counter_decider(),
            Increment::new("/test", 1).on_state_read(),
        )
        .await
        .expect_err("the connection breaks");

    assert!(
        matches!(failure, ExecuteError::OutcomeUnknown(_)),
        "{failure:?}"
    );
    assert_eq!(
        requests.to(WRITE_EVENTS),
        1,
        "trying again may store the events twice"
    );
}

#[tokio::test]
async fn leaves_the_outcome_unknown_after_a_stall_while_waiting_for_the_answer() {
    // The client ends a request on which nothing has arrived for its stall
    // timeout. A write that waits that long for its answer has left, so
    // whether the database stored the events is unknown.
    for write in KitWrite::ALL {
        let (url, _) = Fake::new(Api::V1)
            .route(WRITE_EVENTS, Answer::Stall(Duration::from_secs(5)))
            .start()
            .await;
        let client = Client::builder(url, "secret")
            .stall_timeout(Duration::from_millis(200))
            .build();

        let failure = refused(write, client, "a remark").await;

        assert_outcome_unknown(&failure, &format!("{write:?}"));
        assert!(
            matches!(cause_of(&failure), Some(ClientError::ConnectionStalled)),
            "{write:?}: the source is the stall: {failure:?}"
        );
        assert!(chain(&failure).contains("stalled"), "{write:?}");
    }
}