descartes-core 0.1.1

Core DES framework
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Request and response data models for distributed system simulation
//!
//! This module provides types for modeling requests, attempts, and responses in
//! distributed systems. It distinguishes between logical requests (from the client's
//! perspective) and individual request attempts (actual network/server interactions).

use crate::SimTime;
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Unique identifier for logical requests
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RequestId(pub u64);

impl std::fmt::Display for RequestId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Request({})", self.0)
    }
}

/// Unique identifier for individual request attempts
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RequestAttemptId(pub u64);

impl std::fmt::Display for RequestAttemptId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "AttemptID({})", self.0)
    }
}

/// A logical request from a client - may involve multiple attempts
///
/// A Request represents the client's intent to perform an operation. It may
/// result in multiple RequestAttempts if retries are configured.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Request {
    /// Unique identifier for this request
    pub id: RequestId,
    /// Simulation time when the request was created
    pub created_at: SimTime,
    /// Request payload data
    pub payload: Vec<u8>,
    /// List of attempt IDs associated with this request
    pub attempts: Vec<RequestAttemptId>,
    /// Simulation time when the request completed (if completed)
    pub completed_at: Option<SimTime>,
    /// Final status of the request
    pub status: RequestStatus,
}

impl Request {
    /// Create a new request
    pub fn new(id: RequestId, created_at: SimTime, payload: Vec<u8>) -> Self {
        Self {
            id,
            created_at,
            payload,
            attempts: Vec::new(),
            completed_at: None,
            status: RequestStatus::Pending,
        }
    }

    /// Calculate the end-to-end latency of the request
    ///
    /// Returns the duration from request creation to completion, or None if
    /// the request has not yet completed.
    pub fn latency(&self) -> Option<Duration> {
        self.completed_at
            .map(|completed| completed.duration_since(self.created_at))
    }

    /// Get the number of attempts made for this request
    pub fn attempt_count(&self) -> usize {
        self.attempts.len()
    }

    /// Check if the request has completed
    pub fn is_completed(&self) -> bool {
        self.completed_at.is_some()
    }

    /// Check if the request was successful
    pub fn is_successful(&self) -> bool {
        matches!(self.status, RequestStatus::Success)
    }

    /// Add an attempt to this request
    pub fn add_attempt(&mut self, attempt_id: RequestAttemptId) {
        self.attempts.push(attempt_id);
    }

    /// Mark the request as completed with the given status
    pub fn complete(&mut self, completed_at: SimTime, status: RequestStatus) {
        self.completed_at = Some(completed_at);
        self.status = status;
    }
}

/// Status of a logical request
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RequestStatus {
    /// Request is still pending
    Pending,
    /// Request completed successfully
    Success,
    /// Request failed with a reason
    Failed { reason: String },
    /// All retry attempts exhausted
    Exhausted,
}

/// A single attempt to fulfill a request - may timeout or fail
///
/// A RequestAttempt represents one actual interaction with a server or service.
/// A single Request may have multiple attempts if retries are configured.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RequestAttempt {
    /// Unique identifier for this attempt
    pub id: RequestAttemptId,
    /// ID of the parent request
    pub request_id: RequestId,
    /// Attempt number (1-indexed)
    pub attempt_number: usize,
    /// Simulation time when the attempt started
    pub started_at: SimTime,
    /// Simulation time when the attempt completed (if completed)
    pub completed_at: Option<SimTime>,
    /// Status of this attempt
    pub status: AttemptStatus,
    /// Request payload data
    pub payload: Vec<u8>,
}

impl RequestAttempt {
    /// Create a new request attempt
    pub fn new(
        id: RequestAttemptId,
        request_id: RequestId,
        attempt_number: usize,
        started_at: SimTime,
        payload: Vec<u8>,
    ) -> Self {
        Self {
            id,
            request_id,
            attempt_number,
            started_at,
            completed_at: None,
            status: AttemptStatus::Pending,
            payload,
        }
    }

    /// Calculate the duration of this attempt
    ///
    /// Returns the duration from attempt start to completion, or None if
    /// the attempt has not yet completed.
    pub fn duration(&self) -> Option<Duration> {
        self.completed_at
            .map(|completed| completed.duration_since(self.started_at))
    }

    /// Check if the attempt has completed
    pub fn is_completed(&self) -> bool {
        self.completed_at.is_some()
    }

    /// Check if the attempt was successful
    pub fn is_successful(&self) -> bool {
        matches!(self.status, AttemptStatus::Success)
    }

    /// Mark the attempt as completed with the given status
    pub fn complete(&mut self, completed_at: SimTime, status: AttemptStatus) {
        self.completed_at = Some(completed_at);
        self.status = status;
    }
}

/// Status of an individual request attempt
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AttemptStatus {
    /// Attempt is still pending
    Pending,
    /// Attempt completed successfully
    Success,
    /// Attempt timed out
    Timeout,
    /// Server returned an error
    ServerError,
    /// Request was rejected (e.g., by throttling or circuit breaker)
    Rejected,
}

/// Response to a request attempt
///
/// A Response is generated when a RequestAttempt completes, either successfully
/// or with an error.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response {
    /// ID of the attempt this response is for
    pub attempt_id: RequestAttemptId,
    /// ID of the original request
    pub request_id: RequestId,
    /// Simulation time when the response was generated
    pub completed_at: SimTime,
    /// Response payload data
    pub payload: Vec<u8>,
    /// Status of the response
    pub status: ResponseStatus,
}

impl Response {
    /// Create a successful response
    pub fn success(
        attempt_id: RequestAttemptId,
        request_id: RequestId,
        completed_at: SimTime,
        payload: Vec<u8>,
    ) -> Self {
        Self {
            attempt_id,
            request_id,
            completed_at,
            payload,
            status: ResponseStatus::Ok,
        }
    }

    /// Create an error response
    pub fn error(
        attempt_id: RequestAttemptId,
        request_id: RequestId,
        completed_at: SimTime,
        code: u32,
        message: String,
    ) -> Self {
        Self {
            attempt_id,
            request_id,
            completed_at,
            payload: Vec::new(),
            status: ResponseStatus::Error { code, message },
        }
    }

    /// Check if the response indicates success
    pub fn is_success(&self) -> bool {
        matches!(self.status, ResponseStatus::Ok)
    }

    /// Check if the response indicates an error
    pub fn is_error(&self) -> bool {
        matches!(self.status, ResponseStatus::Error { .. })
    }
}

/// Status of a response
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ResponseStatus {
    /// Response indicates success
    Ok,
    /// Response indicates an error
    Error {
        /// Error code
        code: u32,
        /// Error message
        message: String,
    },
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_request_creation() {
        let id = RequestId(1);
        let created_at = SimTime::from_millis(100);
        let payload = vec![1, 2, 3];

        let request = Request::new(id, created_at, payload.clone());

        assert_eq!(request.id, id);
        assert_eq!(request.created_at, created_at);
        assert_eq!(request.payload, payload);
        assert_eq!(request.attempts.len(), 0);
        assert_eq!(request.completed_at, None);
        assert_eq!(request.status, RequestStatus::Pending);
        assert!(!request.is_completed());
        assert!(!request.is_successful());
    }

    #[test]
    fn test_request_latency() {
        let id = RequestId(1);
        let created_at = SimTime::from_millis(100);
        let completed_at = SimTime::from_millis(250);
        let payload = vec![1, 2, 3];

        let mut request = Request::new(id, created_at, payload);
        assert_eq!(request.latency(), None);

        request.complete(completed_at, RequestStatus::Success);
        assert_eq!(request.latency(), Some(Duration::from_millis(150)));
        assert!(request.is_completed());
        assert!(request.is_successful());
    }

    #[test]
    fn test_request_attempts() {
        let id = RequestId(1);
        let created_at = SimTime::from_millis(100);
        let payload = vec![1, 2, 3];

        let mut request = Request::new(id, created_at, payload);
        assert_eq!(request.attempt_count(), 0);

        request.add_attempt(RequestAttemptId(1));
        assert_eq!(request.attempt_count(), 1);

        request.add_attempt(RequestAttemptId(2));
        assert_eq!(request.attempt_count(), 2);
    }

    #[test]
    fn test_request_attempt_creation() {
        let id = RequestAttemptId(1);
        let request_id = RequestId(1);
        let started_at = SimTime::from_millis(100);
        let payload = vec![1, 2, 3];

        let attempt = RequestAttempt::new(id, request_id, 1, started_at, payload.clone());

        assert_eq!(attempt.id, id);
        assert_eq!(attempt.request_id, request_id);
        assert_eq!(attempt.attempt_number, 1);
        assert_eq!(attempt.started_at, started_at);
        assert_eq!(attempt.completed_at, None);
        assert_eq!(attempt.status, AttemptStatus::Pending);
        assert_eq!(attempt.payload, payload);
        assert!(!attempt.is_completed());
        assert!(!attempt.is_successful());
    }

    #[test]
    fn test_request_attempt_duration() {
        let id = RequestAttemptId(1);
        let request_id = RequestId(1);
        let started_at = SimTime::from_millis(100);
        let completed_at = SimTime::from_millis(150);
        let payload = vec![1, 2, 3];

        let mut attempt = RequestAttempt::new(id, request_id, 1, started_at, payload);
        assert_eq!(attempt.duration(), None);

        attempt.complete(completed_at, AttemptStatus::Success);
        assert_eq!(attempt.duration(), Some(Duration::from_millis(50)));
        assert!(attempt.is_completed());
        assert!(attempt.is_successful());
    }

    #[test]
    fn test_attempt_status_variants() {
        let id = RequestAttemptId(1);
        let request_id = RequestId(1);
        let started_at = SimTime::from_millis(100);
        let completed_at = SimTime::from_millis(150);
        let payload = vec![1, 2, 3];

        let mut attempt = RequestAttempt::new(id, request_id, 1, started_at, payload);

        // Test timeout
        attempt.complete(completed_at, AttemptStatus::Timeout);
        assert_eq!(attempt.status, AttemptStatus::Timeout);
        assert!(!attempt.is_successful());

        // Test server error
        attempt.status = AttemptStatus::ServerError;
        assert_eq!(attempt.status, AttemptStatus::ServerError);
        assert!(!attempt.is_successful());

        // Test rejected
        attempt.status = AttemptStatus::Rejected;
        assert_eq!(attempt.status, AttemptStatus::Rejected);
        assert!(!attempt.is_successful());
    }

    #[test]
    fn test_response_success() {
        let attempt_id = RequestAttemptId(1);
        let request_id = RequestId(1);
        let completed_at = SimTime::from_millis(150);
        let payload = vec![4, 5, 6];

        let response = Response::success(attempt_id, request_id, completed_at, payload.clone());

        assert_eq!(response.attempt_id, attempt_id);
        assert_eq!(response.request_id, request_id);
        assert_eq!(response.completed_at, completed_at);
        assert_eq!(response.payload, payload);
        assert_eq!(response.status, ResponseStatus::Ok);
        assert!(response.is_success());
        assert!(!response.is_error());
    }

    #[test]
    fn test_response_error() {
        let attempt_id = RequestAttemptId(1);
        let request_id = RequestId(1);
        let completed_at = SimTime::from_millis(150);
        let code = 500;
        let message = "Internal Server Error".to_string();

        let response = Response::error(attempt_id, request_id, completed_at, code, message.clone());

        assert_eq!(response.attempt_id, attempt_id);
        assert_eq!(response.request_id, request_id);
        assert_eq!(response.completed_at, completed_at);
        assert!(response.payload.is_empty());
        assert_eq!(
            response.status,
            ResponseStatus::Error {
                code,
                message: message.clone()
            }
        );
        assert!(!response.is_success());
        assert!(response.is_error());
    }

    #[test]
    fn test_request_status_variants() {
        assert_eq!(RequestStatus::Pending, RequestStatus::Pending);
        assert_eq!(RequestStatus::Success, RequestStatus::Success);
        assert_eq!(
            RequestStatus::Failed {
                reason: "timeout".to_string()
            },
            RequestStatus::Failed {
                reason: "timeout".to_string()
            }
        );
        assert_eq!(RequestStatus::Exhausted, RequestStatus::Exhausted);
    }
}