descartes-tower 0.1.1

tower API bindings for DesCartes
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
//! Tower Service trait integration for DES components.
//!
//! This module provides Tower Service implementations that run within discrete event
//! simulations, enabling testing of real-world service architectures with deterministic,
//! reproducible results.
//!
//! # Overview
//!
//! All Tower middleware is adapted to use simulation time instead of wall-clock time:
//!
//! - **Deterministic Timing**: Operations use simulation time for reproducible results
//! - **Event-Driven**: Service operations are scheduled as discrete events
//! - **Full Compatibility**: Works with standard Tower middleware and utilities
//!
//! # Quick Start
//!
//! ```rust,no_run
//! use descartes_tower::{DesServiceBuilder, DesTimeoutLayer, DesRateLimitLayer};
//! use descartes_core::Simulation;
//! use ::tower::ServiceBuilder;
//! use std::time::Duration;
//!
//! # fn example() -> Result<(), descartes_tower::ServiceError> {
//! let mut simulation = Simulation::default();
//! descartes_tokio::runtime::install(&mut simulation);
//!
//! // Create a base service
//! let base_service = DesServiceBuilder::new("api-server".to_string())
//!     .thread_capacity(5)
//!     .service_time(Duration::from_millis(100))
//!     .build(&mut simulation)?;
//!
//! // Add middleware layers
//! let service = ServiceBuilder::new()
//!     .layer(DesTimeoutLayer::new(Duration::from_secs(5)))
//!     .layer(DesRateLimitLayer::new(10.0, 20))
//!     .service(base_service);
//! # Ok(())
//! # }
//! ```
//!
//! # Available Middleware
//!
//! - [`DesService`] / [`DesServiceBuilder`]: Base service with configurable capacity and timing
//! - [`DesRateLimit`] / [`DesRateLimitLayer`]: Token bucket rate limiting
//! - [`DesConcurrencyLimit`] / [`DesConcurrencyLimitLayer`]: Per-service concurrency control
//! - [`DesGlobalConcurrencyLimit`]: Shared concurrency limits across services
//! - [`DesCircuitBreaker`] / [`DesCircuitBreakerLayer`]: Failure detection and recovery
//! - [`DesTimeout`] / [`DesTimeoutLayer`]: Request timeouts using simulation events
//! - [`DesRetry`] / [`DesRetryLayer`]: Retry with configurable backoff
//! - [`DesLoadBalancer`]: Round-robin, random, and least-connections strategies
//! - [`DesHedge`] / [`DesHedgeLayer`]: Request hedging for tail latency reduction
//!
//! # Usage Patterns
//!
//! ## Basic Service Creation
//!
//! ```rust,no_run
//! use descartes_tower::{DesServiceBuilder, ServiceError};
//! use descartes_core::Simulation;
//! use std::time::Duration;
//!
//! # fn example() -> Result<(), ServiceError> {
//! let mut simulation = Simulation::default();
//! descartes_tokio::runtime::install(&mut simulation);
//!
//! let service = DesServiceBuilder::new("web-server".to_string())
//!     .thread_capacity(10)
//!     .service_time(Duration::from_millis(50))
//!     .build(&mut simulation)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Middleware Composition
//!
//! ```rust,no_run
//! use descartes_tower::*;
//! use ::tower::ServiceBuilder;
//! use std::time::Duration;
//!
//! # fn middleware_example() -> Result<(), ServiceError> {
//! # let mut simulation = descartes_core::Simulation::default();
//! # descartes_tokio::runtime::install(&mut simulation);
//! let base_service = DesServiceBuilder::new("api-server".to_string())
//!     .thread_capacity(5)
//!     .service_time(Duration::from_millis(100))
//!     .build(&mut simulation)?;
//!
//! let service = ServiceBuilder::new()
//!     .layer(DesTimeoutLayer::new(Duration::from_secs(5)))
//!     .layer(DesRateLimitLayer::new(10.0, 20))
//!     .layer(DesConcurrencyLimitLayer::new(3))
//!     .layer(DesRetryLayer::new(DesRetryPolicy::new(3)))
//!     .service(base_service);
//! # Ok(())
//! # }
//! ```
//!
//! ## Load Balancing Multiple Services
//!
//! ```rust,no_run
//! use descartes_tower::*;
//!
//! # fn load_balancing_example() -> Result<(), ServiceError> {
//! # let mut simulation = descartes_core::Simulation::default();
//! # descartes_tokio::runtime::install(&mut simulation);
//! let services = (0..3).map(|i| {
//!     DesServiceBuilder::new(format!("server-{}", i))
//!         .thread_capacity(5)
//!         .service_time(std::time::Duration::from_millis(100))
//!         .build(&mut simulation)
//! }).collect::<Result<Vec<_>, _>>()?;
//!
//! let load_balancer = DesLoadBalancer::round_robin(services);
//! # Ok(())
//! # }
//! ```
//!
//! # Performance Characteristics
//!
//! - **Service Time**: Configurable processing time per request
//! - **Queue Delays**: Automatic backpressure when capacity is exceeded
//! - **Middleware Overhead**: Minimal simulation overhead per layer
//!
//! # Testing Scenarios
//!
//! ## Load Testing
//! ```rust,no_run
//! # use descartes_tower::*;
//! # use std::time::Duration;
//! # fn load_test_example() {
//! # let mut simulation = descartes_core::Simulation::default();
//! # descartes_tokio::runtime::install(&mut simulation);
//! // Simulate high load with rate limiting
//! let service = DesServiceBuilder::new("load-test".to_string())
//!     .thread_capacity(2)
//!     .service_time(Duration::from_millis(200))
//!     .build(&mut simulation).unwrap();
//!
//! let rate_limited = DesRateLimit::new(service, 100.0, 50);
//! # }
//! ```
//!
//! ## Failure Testing
//! ```rust,no_run
//! # use descartes_tower::*;
//! # use std::time::Duration;
//! # fn failure_test_example() {
//! # let mut simulation = descartes_core::Simulation::default();
//! # descartes_tokio::runtime::install(&mut simulation);
//! // Test circuit breaker behavior
//! let service = DesServiceBuilder::new("failure-test".to_string())
//!     .thread_capacity(1)
//!     .service_time(Duration::from_millis(100))
//!     .build(&mut simulation).unwrap();
//!
//! let circuit_breaker = DesCircuitBreaker::new(
//!     service,
//!     3,  // Failure threshold
//!     Duration::from_secs(10),  // Recovery timeout
//! );
//! # }
//! ```

use bytes::Bytes;
use http::{Response as HttpResponse, StatusCode};
use http_body::Body as HttpBody;
use std::pin::Pin;
use std::task::{Context, Poll};
use thiserror::Error;

// Re-export all the layers and services
pub mod circuit_breaker;
pub mod discovery;
pub mod hedge;
pub mod limit;
pub mod load_balancer;
pub mod retry;
pub mod service;
pub mod timeout;

pub use circuit_breaker::{DesCircuitBreaker, DesCircuitBreakerLayer};
pub use descartes_core::SchedulerHandle;
pub use discovery::wait_for_endpoint;
pub use hedge::{DesHedge, DesHedgeLayer};
pub use limit::{
    DesConcurrencyLimit, DesConcurrencyLimitLayer, DesGlobalConcurrencyLimit,
    DesGlobalConcurrencyLimitLayer, DesRateLimit, DesRateLimitLayer,
};
pub use load_balancer::{DesLoadBalanceStrategy, DesLoadBalancer, DesLoadBalancerLayer};
pub use retry::{
    exponential_backoff_layer, DesRetry, DesRetryLayer, DesRetryPolicy, ExponentialBackoff,
};
pub use service::{DesService, DesServiceBuilder, TowerSchedulerHandle};
pub use timeout::{DesTimeout, DesTimeoutLayer};

/// Errors that can occur in the DES Tower integration
#[derive(Debug, Error, Clone)]
pub enum ServiceError {
    #[error("Service is not ready to accept requests")]
    NotReady,
    #[error("Request was cancelled")]
    Cancelled,
    #[error("Service is overloaded")]
    Overloaded,
    #[error("Request timeout after {duration:?}")]
    Timeout { duration: std::time::Duration },
    #[error("Internal simulation error: {0}")]
    Internal(String),
    #[error("HTTP error: {0}")]
    Http(String), // Changed from http::Error to String for Clone compatibility
    #[error("Circuit breaker is in invalid state")]
    CircuitBreakerInvalidState,
    #[error("Rate limiter is in invalid state")]
    RateLimiterInvalidState,
    #[error("HTTP response builder error: {message}")]
    HttpResponseBuilder { message: String },
}

/// A simple HTTP body type for our simulation
#[derive(Debug, Clone)]
pub struct SimBody {
    data: Bytes,
}

impl SimBody {
    pub fn new(data: impl Into<Bytes>) -> Self {
        Self { data: data.into() }
    }

    pub fn empty() -> Self {
        Self { data: Bytes::new() }
    }

    pub fn from_static(data: &'static str) -> Self {
        Self {
            data: Bytes::from_static(data.as_bytes()),
        }
    }

    pub fn data(&self) -> &Bytes {
        &self.data
    }
}

impl HttpBody for SimBody {
    type Data = Bytes;
    type Error = std::convert::Infallible;

    fn poll_frame(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
    ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
        let this = self.get_mut();
        if this.data.is_empty() {
            Poll::Ready(None)
        } else {
            let data = std::mem::take(&mut this.data);
            Poll::Ready(Some(Ok(http_body::Frame::data(data))))
        }
    }
}

/// Convert DES Response to HTTP response
pub(crate) fn response_to_http(
    response: descartes_core::Response,
) -> Result<HttpResponse<SimBody>, ServiceError> {
    use descartes_core::ResponseStatus;

    match response.status {
        ResponseStatus::Ok => {
            let body = if response.payload.is_empty() {
                SimBody::from_static("OK")
            } else {
                SimBody::new(response.payload)
            };

            HttpResponse::builder()
                .status(StatusCode::OK)
                .body(body)
                .map_err(|e| ServiceError::HttpResponseBuilder {
                    message: e.to_string(),
                })
        }
        ResponseStatus::Error { code, message } => {
            let status =
                StatusCode::from_u16(code as u16).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);

            HttpResponse::builder()
                .status(status)
                .body(SimBody::new(message))
                .map_err(|e| ServiceError::HttpResponseBuilder {
                    message: e.to_string(),
                })
        }
    }
}

/// Serialize HTTP request to bytes (simplified)
pub(crate) fn serialize_http_request(req: &http::Request<SimBody>) -> Vec<u8> {
    // Simple serialization - in practice you might use a more sophisticated format
    let method = req.method().as_str();
    let uri = req.uri().to_string();
    let headers = req
        .headers()
        .iter()
        .map(|(k, v)| format!("{}: {}", k, v.to_str().unwrap_or("")))
        .collect::<Vec<_>>()
        .join("\r\n");

    // Include the body content
    let body_data = req.body().data();
    let mut result = format!("{method} {uri} HTTP/1.1\r\n{headers}\r\n\r\n").into_bytes();
    result.extend_from_slice(body_data);
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use descartes_core::Simulation;
    use http::{Method, Request};
    use std::future::Future;
    use std::pin::Pin;
    use std::task::{Context, Poll, Waker};
    use std::time::Duration;
    use tower::Service;

    // Helper to create a no-op waker for testing
    fn noop_waker() -> Waker {
        use std::task::{RawWaker, RawWakerVTable};

        fn noop(_: *const ()) {}
        fn clone(_: *const ()) -> RawWaker {
            RawWaker::new(std::ptr::null(), &VTABLE)
        }

        const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, noop, noop, noop);
        let raw_waker = RawWaker::new(std::ptr::null(), &VTABLE);
        unsafe { Waker::from_raw(raw_waker) }
    }

    #[test]
    fn test_descartes_service_basic() {
        let mut simulation = Simulation::default();

        // Build the service
        let mut service = DesServiceBuilder::new("test-server".to_string())
            .thread_capacity(2)
            .service_time(std::time::Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        // Create a test request
        let request = http::Request::builder()
            .method(Method::GET)
            .uri("/test")
            .body(SimBody::from_static("test body"))
            .unwrap();

        // Check service is ready
        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);
        assert!(matches!(service.poll_ready(&mut cx), Poll::Ready(Ok(()))));

        // Make the request
        let mut response_future = service.call(request);

        // Run simulation steps to process the request
        for _ in 0..20 {
            if !simulation.step() {
                break;
            }
        }

        // The response should be ready now
        let response = match Pin::new(&mut response_future).poll(&mut cx) {
            Poll::Ready(Ok(response)) => response,
            Poll::Ready(Err(e)) => panic!("Request failed: {e:?}"),
            Poll::Pending => panic!("Response should be ready after simulation steps"),
        };

        assert_eq!(response.status(), StatusCode::OK);
    }

    #[test]
    fn test_descartes_rate_limit_layer() {
        let mut simulation = Simulation::default();
        descartes_tokio::runtime::install(&mut simulation);

        // Create base service
        let base_service = DesServiceBuilder::new("rate-limit-test".to_string())
            .thread_capacity(5)
            .service_time(Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        // Wrap with rate limiter (2 requests per second, burst of 3)
        let mut rate_limit_service = DesRateLimit::new(
            base_service,
            2.0, // 2 requests per second
            3,   // burst capacity
        );

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // Send burst of requests
        let mut futures = Vec::new();
        for i in 0..5 {
            let req = Request::builder()
                .method(Method::GET)
                .uri(format!("/rate-limit-test/{i}"))
                .body(SimBody::empty())
                .unwrap();
            futures.push(rate_limit_service.call(req));
        }

        // Run simulation
        for _ in 0..100 {
            if !simulation.step() {
                break;
            }
        }

        // Check results - first 3 should succeed (burst), others should be rate limited
        let mut successes = 0;
        let mut rate_limited = 0;

        for mut future in futures {
            match Pin::new(&mut future).poll(&mut cx) {
                Poll::Ready(Ok(response)) => {
                    if response.status() == StatusCode::OK {
                        successes += 1;
                    }
                }
                Poll::Ready(Err(ServiceError::Overloaded)) => {
                    rate_limited += 1;
                }
                Poll::Ready(Err(e)) => panic!("Unexpected error: {e:?}"),
                Poll::Pending => {
                    // Might be rate limited
                    rate_limited += 1;
                }
            }
        }

        println!("Rate limit test - Successes: {successes}, Rate limited: {rate_limited}");

        // Should allow burst capacity (3) and rate limit the rest (2)
        assert!(successes <= 3, "Should not exceed burst capacity");
        assert!(rate_limited >= 2, "Should rate limit excess requests");
    }

    #[test]
    fn test_descartes_concurrency_limit_basic() {
        let mut simulation = Simulation::default();

        // Create base service
        let base_service = DesServiceBuilder::new("basic-concurrency-test".to_string())
            .thread_capacity(5)
            .service_time(Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        // Wrap with concurrency limiter (limit to 1 concurrent request)
        let mut concurrency_service = DesConcurrencyLimit::new(base_service, 1);

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // First request should be ready
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));

        let req1 = Request::builder()
            .method(Method::GET)
            .uri("/test1")
            .body(SimBody::empty())
            .unwrap();
        let future1 = concurrency_service.call(req1);

        // Second request should be blocked
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Pending
        ));

        // Complete the first request
        for _ in 0..100 {
            if !simulation.step() {
                break;
            }
        }

        // Check if first request completed
        let mut future1 = future1;
        let result = Pin::new(&mut future1).poll(&mut cx);
        assert!(matches!(result, Poll::Ready(Ok(_))));

        // Now service should be ready again
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
    }

    #[test]
    fn test_descartes_concurrency_limit_backpressure() {
        let mut simulation = Simulation::default();

        // Create base service with high capacity but slow processing
        let base_service = DesServiceBuilder::new("backpressure-test".to_string())
            .thread_capacity(10)
            .service_time(Duration::from_millis(200)) // Slow service
            .build(&mut simulation)
            .unwrap();

        // Wrap with concurrency limiter (limit to 2 concurrent requests)
        let mut concurrency_service = DesConcurrencyLimit::new(base_service, 2);

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // Test backpressure: first 2 should be ready, 3rd should not
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        let req1 = Request::builder()
            .method(Method::GET)
            .uri("/test1")
            .body(SimBody::empty())
            .unwrap();
        let future1 = concurrency_service.call(req1);

        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        let req2 = Request::builder()
            .method(Method::GET)
            .uri("/test2")
            .body(SimBody::empty())
            .unwrap();
        let future2 = concurrency_service.call(req2);

        // Third request should be blocked by concurrency limit
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Pending
        ));

        // Run simulation partially to start processing
        for _ in 0..10 {
            if !simulation.step() {
                break;
            }
        }

        // Still should be blocked since requests are still processing
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Pending
        ));

        // Complete the simulation
        for _ in 0..300 {
            if !simulation.step() {
                break;
            }
        }

        // Poll the futures to completion to release their slots
        let futures = vec![future1, future2];
        let mut completed = 0;
        for mut future in futures {
            if let Poll::Ready(Ok(_)) = Pin::new(&mut future).poll(&mut cx) {
                completed += 1;
            }
        }
        assert_eq!(completed, 2, "Both requests should have completed");

        // Now should be ready again after slots are released
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
    }

    #[test]
    fn test_descartes_concurrency_limit_sequential_processing() {
        let mut simulation = Simulation::default();

        // Create base service with capacity 1 to force sequential processing
        let base_service = DesServiceBuilder::new("sequential-test".to_string())
            .thread_capacity(1)
            .service_time(Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        // Concurrency limit of 1 should enforce strict sequential processing
        let mut concurrency_service = DesConcurrencyLimit::new(base_service, 1);

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // Process requests one by one to test sequential behavior
        for i in 0..3 {
            // Each request should be ready
            assert!(matches!(
                concurrency_service.poll_ready(&mut cx),
                Poll::Ready(Ok(()))
            ));

            let req = Request::builder()
                .method(Method::GET)
                .uri(format!("/sequential/{i}"))
                .body(SimBody::empty())
                .unwrap();
            let mut future = concurrency_service.call(req);

            // After calling, service should not be ready for next request
            assert!(matches!(
                concurrency_service.poll_ready(&mut cx),
                Poll::Pending
            ));

            // Run simulation to complete this request
            for _ in 0..100 {
                if !simulation.step() {
                    break;
                }
            }

            // Poll the future to completion to release the slot
            assert!(matches!(
                Pin::new(&mut future).poll(&mut cx),
                Poll::Ready(Ok(_))
            ));
        }

        // After all requests are processed, service should be ready again
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
    }

    #[test]
    fn test_descartes_global_concurrency_limit_shared_state() {
        let mut simulation = Simulation::default();

        // Create shared global concurrency state with limit of 2
        let global_state =
            crate::tower::limit::global_concurrency::GlobalConcurrencyLimitState::new(2);

        // Create two services sharing the same global limit
        let service1 = DesServiceBuilder::new("global-service-1".to_string())
            .thread_capacity(5)
            .service_time(Duration::from_millis(100))
            .build(&mut simulation)
            .unwrap();

        let service2 = DesServiceBuilder::new("global-service-2".to_string())
            .thread_capacity(5)
            .service_time(Duration::from_millis(100))
            .build(&mut simulation)
            .unwrap();

        let mut global_service1 = DesGlobalConcurrencyLimit::new(service1, global_state.clone());
        let mut global_service2 = DesGlobalConcurrencyLimit::new(service2, global_state.clone());

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // Test that global limit is enforced across services

        // Service 1 should be able to take first slot
        assert!(matches!(
            global_service1.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        let req1 = Request::builder()
            .method(Method::GET)
            .uri("/global-1")
            .body(SimBody::empty())
            .unwrap();
        let future1 = global_service1.call(req1);

        // Service 2 should be able to take second slot
        assert!(matches!(
            global_service2.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        let req2 = Request::builder()
            .method(Method::GET)
            .uri("/global-2")
            .body(SimBody::empty())
            .unwrap();
        let future2 = global_service2.call(req2);

        // Now both services should be blocked by global limit
        assert!(matches!(global_service1.poll_ready(&mut cx), Poll::Pending));
        assert!(matches!(global_service2.poll_ready(&mut cx), Poll::Pending));

        // Verify global state shows we're at capacity
        assert_eq!(global_state.current_concurrency(), 2);
        assert_eq!(global_state.max_concurrency(), 2);

        // Run simulation to complete requests
        for _ in 0..200 {
            if !simulation.step() {
                break;
            }
        }

        // Poll futures to completion to release their slots
        let futures = vec![future1, future2];
        let mut completed = 0;
        for mut future in futures {
            if let Poll::Ready(Ok(_)) = Pin::new(&mut future).poll(&mut cx) {
                completed += 1;
            }
        }
        assert_eq!(completed, 2, "Both requests should have completed");

        // Global state should be back to 0 after futures are polled
        assert_eq!(global_state.current_concurrency(), 0);

        // After completion, services should be ready again (this will acquire new slots)
        assert!(matches!(
            global_service1.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        assert!(matches!(
            global_service2.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
    }

    #[test]
    fn test_descartes_global_concurrency_limit_fairness() {
        let mut simulation = Simulation::default();

        // Create shared global state with limit of 1 to test fairness
        let global_state =
            crate::tower::limit::global_concurrency::GlobalConcurrencyLimitState::new(1);

        // Create three services sharing the same global limit
        let service1 = DesServiceBuilder::new("fair-service-1".to_string())
            .thread_capacity(2)
            .service_time(Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        let service2 = DesServiceBuilder::new("fair-service-2".to_string())
            .thread_capacity(2)
            .service_time(Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        let service3 = DesServiceBuilder::new("fair-service-3".to_string())
            .thread_capacity(2)
            .service_time(Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        let mut global_service1 = DesGlobalConcurrencyLimit::new(service1, global_state.clone());
        let mut global_service2 = DesGlobalConcurrencyLimit::new(service2, global_state.clone());
        let mut global_service3 = DesGlobalConcurrencyLimit::new(service3, global_state.clone());

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        let mut completed_requests = 0;

        // Process requests sequentially across services
        for round in 0..3 {
            // Service 1 gets a turn
            assert!(matches!(
                global_service1.poll_ready(&mut cx),
                Poll::Ready(Ok(()))
            ));
            let req = Request::builder()
                .method(Method::GET)
                .uri(format!("/fair-1-{round}"))
                .body(SimBody::empty())
                .unwrap();
            let future = global_service1.call(req);

            // Other services should be blocked
            assert!(matches!(global_service2.poll_ready(&mut cx), Poll::Pending));
            assert!(matches!(global_service3.poll_ready(&mut cx), Poll::Pending));

            // Complete this request
            for _ in 0..100 {
                if !simulation.step() {
                    break;
                }
            }

            // Verify completion
            if let Poll::Ready(Ok(_)) = Pin::new(&mut { future }).poll(&mut cx) {
                completed_requests += 1;
            }
        }

        assert_eq!(completed_requests, 3, "All requests should complete fairly");
        assert_eq!(
            global_state.current_concurrency(),
            0,
            "Global state should be clean"
        );
    }

    #[test]
    fn test_concurrency_limit_precise_tracking() {
        let mut simulation = Simulation::default();

        // Create base service with high capacity
        let base_service = DesServiceBuilder::new("precise-tracking-test".to_string())
            .thread_capacity(10)
            .service_time(Duration::from_millis(100))
            .build(&mut simulation)
            .unwrap();

        // Wrap with concurrency limiter (limit to 3 concurrent requests)
        let mut concurrency_service = DesConcurrencyLimit::new(base_service, 3);

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // Test precise concurrency tracking

        // Initially should be 0 concurrent requests
        assert_eq!(concurrency_service.current_concurrency(), 0);
        assert_eq!(concurrency_service.max_concurrency(), 3);

        // Acquire first slot
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        let req1 = Request::builder()
            .method(Method::GET)
            .uri("/precise-1")
            .body(SimBody::empty())
            .unwrap();
        let future1 = concurrency_service.call(req1);
        assert_eq!(
            concurrency_service.current_concurrency(),
            1,
            "Should have 1 active request"
        );

        // Acquire second slot
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        let req2 = Request::builder()
            .method(Method::GET)
            .uri("/precise-2")
            .body(SimBody::empty())
            .unwrap();
        let future2 = concurrency_service.call(req2);
        assert_eq!(
            concurrency_service.current_concurrency(),
            2,
            "Should have 2 active requests"
        );

        // Acquire third slot
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        let req3 = Request::builder()
            .method(Method::GET)
            .uri("/precise-3")
            .body(SimBody::empty())
            .unwrap();
        let future3 = concurrency_service.call(req3);
        assert_eq!(
            concurrency_service.current_concurrency(),
            3,
            "Should have 3 active requests (at capacity)"
        );

        // Fourth request should be blocked
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Pending
        ));
        assert_eq!(
            concurrency_service.current_concurrency(),
            3,
            "Should still be at capacity"
        );

        // Run simulation to complete first request
        for _ in 0..150 {
            if !simulation.step() {
                break;
            }
        }

        // Poll first future to completion to release its slot
        let mut future1 = future1;
        assert!(matches!(
            Pin::new(&mut future1).poll(&mut cx),
            Poll::Ready(Ok(_))
        ));
        assert_eq!(
            concurrency_service.current_concurrency(),
            2,
            "Should have 2 active requests after completion"
        );

        // Now fourth request should be able to proceed
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
        let req4 = Request::builder()
            .method(Method::GET)
            .uri("/precise-4")
            .body(SimBody::empty())
            .unwrap();
        let future4 = concurrency_service.call(req4);
        assert_eq!(
            concurrency_service.current_concurrency(),
            3,
            "Should be back at capacity with new request"
        );

        // Complete remaining requests
        for _ in 0..200 {
            if !simulation.step() {
                break;
            }
        }

        // Poll all remaining futures to completion
        let futures = vec![future2, future3, future4];
        let mut completed = 0;
        for mut future in futures {
            if let Poll::Ready(Ok(_)) = Pin::new(&mut future).poll(&mut cx) {
                completed += 1;
            }
        }
        assert_eq!(completed, 3, "All remaining requests should complete");
        assert_eq!(
            concurrency_service.current_concurrency(),
            0,
            "Should have 0 active requests after all complete"
        );

        // Service should be ready for new requests
        assert!(matches!(
            concurrency_service.poll_ready(&mut cx),
            Poll::Ready(Ok(()))
        ));
    }

    #[test]
    fn test_tower_layer_composition() {
        let mut simulation = Simulation::default();
        descartes_tokio::runtime::install(&mut simulation);

        // Create base service
        let base_service = DesServiceBuilder::new("layer-composition-test".to_string())
            .thread_capacity(5)
            .service_time(Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        // Use Layer trait for composable middleware
        use tower::ServiceBuilder;

        let mut service = ServiceBuilder::new()
            // Add rate limiting layer (5 requests per second, burst of 10)
            .layer(DesRateLimitLayer::new(5.0, 10))
            // Add concurrency limiting layer (max 2 concurrent requests)
            .layer(DesConcurrencyLimitLayer::new(2))
            .service(base_service);

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // Test that the composed service works
        assert!(matches!(service.poll_ready(&mut cx), Poll::Ready(Ok(()))));

        let req = Request::builder()
            .method(Method::GET)
            .uri("/composed")
            .body(SimBody::empty())
            .unwrap();
        let mut future = service.call(req);

        // Run simulation
        for _ in 0..200 {
            if !simulation.step() {
                break;
            }
        }

        // Check response
        let result = Pin::new(&mut future).poll(&mut cx);
        match result {
            Poll::Ready(Ok(_)) => {
                // Success! Layer composition works
            }
            Poll::Ready(Err(e)) => {
                panic!("Composed service failed with error: {e:?}");
            }
            Poll::Pending => {
                panic!("Composed service still pending after simulation steps");
            }
        }
    }

    #[test]
    fn test_timeout_layer_success() {
        use descartes_core::{Execute, Executor, SimTime};
        use std::cell::RefCell;
        use std::rc::Rc;
        use tower::{Layer, ServiceExt};

        let mut simulation = Simulation::default();
        descartes_tokio::runtime::install(&mut simulation);

        let base_service = DesServiceBuilder::new("timeout-success-test".to_string())
            .thread_capacity(5)
            .service_time(Duration::from_millis(1))
            .build(&mut simulation)
            .unwrap();

        let timeout_service = DesTimeoutLayer::new(Duration::from_millis(1000)).layer(base_service);

        let done: Rc<RefCell<Option<Result<http::Response<SimBody>, ServiceError>>>> =
            Rc::new(RefCell::new(None));
        let done_clone = done.clone();

        descartes_tokio::task::spawn_local(async move {
            let req = Request::builder()
                .method(Method::GET)
                .uri("/timeout-success")
                .body(SimBody::empty())
                .unwrap();
            let res = timeout_service.oneshot(req).await;
            *done_clone.borrow_mut() = Some(res);
        });

        Executor::timed(SimTime::from_duration(Duration::from_secs(1))).execute(&mut simulation);

        let res = done.borrow_mut().take().expect("request completed");
        assert!(res.is_ok(), "request should succeed before timeout");
    }

    #[test]
    fn test_timeout_layer_timeout() {
        use descartes_core::{Execute, Executor, SimTime};
        use std::cell::RefCell;
        use std::rc::Rc;
        use tower::{Layer, ServiceExt};

        let mut simulation = Simulation::default();
        descartes_tokio::runtime::install(&mut simulation);

        let base_service = DesServiceBuilder::new("timeout-test".to_string())
            .thread_capacity(5)
            .service_time(Duration::from_millis(200))
            .build(&mut simulation)
            .unwrap();

        let timeout_service = DesTimeoutLayer::new(Duration::from_millis(50)).layer(base_service);

        let done: Rc<RefCell<Option<Result<http::Response<SimBody>, ServiceError>>>> =
            Rc::new(RefCell::new(None));
        let done_clone = done.clone();

        descartes_tokio::task::spawn_local(async move {
            let req = Request::builder()
                .method(Method::GET)
                .uri("/timeout-test")
                .body(SimBody::empty())
                .unwrap();
            let res = timeout_service.oneshot(req).await;
            *done_clone.borrow_mut() = Some(res);
        });

        Executor::timed(SimTime::from_duration(Duration::from_secs(1))).execute(&mut simulation);

        let res = done.borrow_mut().take().expect("request completed");
        match res {
            Err(ServiceError::Timeout { duration }) => {
                assert_eq!(duration, Duration::from_millis(50))
            }
            Ok(_) => panic!("request should have timed out, not succeeded"),
            Err(e) => panic!("expected timeout error, got: {e:?}"),
        }
    }

    #[test]
    fn test_timeout_layer_resource_cleanup() {
        let mut simulation = Simulation::default();
        descartes_tokio::runtime::install(&mut simulation);

        // Create base service
        let base_service = DesServiceBuilder::new("cleanup-test".to_string())
            .thread_capacity(5)
            .service_time(Duration::from_millis(50))
            .build(&mut simulation)
            .unwrap();

        // Wrap with timeout layer
        use tower::Layer;
        let mut timeout_service =
            DesTimeoutLayer::new(Duration::from_millis(100)).layer(base_service);

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // Create and drop multiple futures to test cleanup
        for _ in 0..5 {
            assert!(matches!(
                timeout_service.poll_ready(&mut cx),
                Poll::Ready(Ok(()))
            ));

            let req = Request::builder()
                .method(Method::GET)
                .uri("/cleanup-test")
                .body(SimBody::empty())
                .unwrap();
            let future = timeout_service.call(req);

            // Drop the future immediately to test PinnedDrop cleanup
            drop(future);
        }

        // Run a few simulation steps to allow any cleanup to occur
        for _ in 0..10 {
            if !simulation.step() {
                break;
            }
        }

        // Test passes if no panics or resource leaks occur
        // The PinnedDrop implementation should clean up timeout components
    }

    #[test]
    fn test_circuit_breaker_failure_threshold() {
        let mut simulation = Simulation::default();
        descartes_tokio::runtime::install(&mut simulation);

        // Create a service that always fails
        let failing_service = FailingService;

        // Wrap with circuit breaker (failure threshold of 3, recovery timeout of 1 second)
        let mut circuit_breaker_service = DesCircuitBreaker::new(
            failing_service,
            3, // failure threshold
            Duration::from_secs(1),
        );

        let waker = noop_waker();
        let mut cx = Context::from_waker(&waker);

        // First 3 requests should be allowed and fail
        for i in 0..3 {
            assert!(matches!(
                circuit_breaker_service.poll_ready(&mut cx),
                Poll::Ready(Ok(()))
            ));

            let req = Request::builder()
                .method(Method::GET)
                .uri(format!("/fail/{i}"))
                .body(SimBody::empty())
                .unwrap();
            let mut future = circuit_breaker_service.call(req);

            // Poll the future to completion
            match Pin::new(&mut future).poll(&mut cx) {
                Poll::Ready(Err(ServiceError::Internal(_))) => {
                    // Expected failure from FailingService
                }
                other => panic!("Expected failure, got: {other:?}"),
            }
        }

        // 4th request should be rejected due to circuit breaker being open
        match circuit_breaker_service.poll_ready(&mut cx) {
            Poll::Ready(Err(ServiceError::Overloaded)) => {
                // Expected - circuit breaker is now open
            }
            other => panic!("Expected circuit breaker to be open, got: {other:?}"),
        }
    }

    // Helper service that always fails for testing circuit breaker
    #[derive(Clone)]
    struct FailingService;

    impl Service<Request<SimBody>> for FailingService {
        type Response = http::Response<SimBody>;
        type Error = ServiceError;
        type Future = std::future::Ready<Result<Self::Response, Self::Error>>;

        fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }

        fn call(&mut self, _req: Request<SimBody>) -> Self::Future {
            std::future::ready(Err(ServiceError::Internal("Always fails".to_string())))
        }
    }

    // Include other integration tests here...
}