|
| 1 | +use std::sync::atomic::{AtomicU32, Ordering}; |
| 2 | +use std::sync::Arc; |
| 3 | +use std::time::{Duration, Instant}; |
| 4 | + |
| 5 | +use pinecone_grpc::retry::{retry_on_transient, RetryConfig, ThrottleCallback}; |
| 6 | +use tonic::Status; |
| 7 | + |
| 8 | +#[tokio::test] |
| 9 | +async fn callback_fires_per_retry_attempt() { |
| 10 | + let call_count = Arc::new(AtomicU32::new(0)); |
| 11 | + let count = call_count.clone(); |
| 12 | + let cb_call_count = Arc::new(AtomicU32::new(0)); |
| 13 | + let cb_count = cb_call_count.clone(); |
| 14 | + |
| 15 | + let on_throttle: ThrottleCallback = Arc::new(move |_h: String| { |
| 16 | + cb_count.fetch_add(1, Ordering::SeqCst); |
| 17 | + }); |
| 18 | + |
| 19 | + let config = RetryConfig { |
| 20 | + max_retries: 3, |
| 21 | + initial_backoff: Duration::from_millis(1), |
| 22 | + max_backoff: Duration::from_millis(5), |
| 23 | + on_throttle: Some(on_throttle), |
| 24 | + ..RetryConfig::default() |
| 25 | + }; |
| 26 | + |
| 27 | + let result = retry_on_transient(&config, || { |
| 28 | + let count = count.clone(); |
| 29 | + async move { |
| 30 | + let n = count.fetch_add(1, Ordering::SeqCst); |
| 31 | + if n < 3 { |
| 32 | + Err(Status::resource_exhausted("throttled")) |
| 33 | + } else { |
| 34 | + Ok::<(), Status>(()) |
| 35 | + } |
| 36 | + } |
| 37 | + }) |
| 38 | + .await; |
| 39 | + |
| 40 | + assert!(result.is_ok()); |
| 41 | + // 3 retryable failures before success → callback fires 3 times (once per failure) |
| 42 | + assert_eq!(cb_call_count.load(Ordering::SeqCst), 3); |
| 43 | +} |
| 44 | + |
| 45 | +#[tokio::test] |
| 46 | +async fn pushback_smear_produces_delays_within_range() { |
| 47 | + let pushback_ms: u64 = 20; |
| 48 | + |
| 49 | + let config = RetryConfig { |
| 50 | + max_retries: 1, |
| 51 | + initial_backoff: Duration::from_millis(1), |
| 52 | + max_backoff: Duration::from_millis(200), |
| 53 | + ..RetryConfig::default() |
| 54 | + }; |
| 55 | + |
| 56 | + let start = Instant::now(); |
| 57 | + let _ = retry_on_transient(&config, || async { |
| 58 | + let mut s = Status::resource_exhausted("throttled"); |
| 59 | + s.metadata_mut().insert( |
| 60 | + "grpc-retry-pushback-ms", |
| 61 | + pushback_ms.to_string().parse().unwrap(), |
| 62 | + ); |
| 63 | + Err::<(), Status>(s) |
| 64 | + }) |
| 65 | + .await; |
| 66 | + let elapsed = start.elapsed(); |
| 67 | + |
| 68 | + // smear_pushback(20ms, 200ms) returns uniform(20ms, 30ms), well under cap. |
| 69 | + // Lower bound: must wait at least pushback ms. |
| 70 | + // Upper bound: pushback + pushback/2 + generous CI slack (200ms). |
| 71 | + assert!( |
| 72 | + elapsed >= Duration::from_millis(pushback_ms), |
| 73 | + "elapsed {:?} should be >= pushback {}ms", |
| 74 | + elapsed, |
| 75 | + pushback_ms |
| 76 | + ); |
| 77 | + assert!( |
| 78 | + elapsed < Duration::from_millis(pushback_ms + pushback_ms / 2 + 200), |
| 79 | + "elapsed {:?} exceeded expected ceiling {}ms", |
| 80 | + elapsed, |
| 81 | + pushback_ms + pushback_ms / 2 + 200 |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +#[tokio::test] |
| 86 | +async fn callback_exception_does_not_break_retry() { |
| 87 | + // Verify that a callback which handles its own failure silently (matching the |
| 88 | + // transport.rs pattern for Python exceptions: |
| 89 | + // `if let Err(_) = py_cb.call1(py, (h,)) { /* log and ignore */ }`) |
| 90 | + // does not prevent retry from succeeding. |
| 91 | + let call_count = Arc::new(AtomicU32::new(0)); |
| 92 | + let count = call_count.clone(); |
| 93 | + let cb_call_count = Arc::new(AtomicU32::new(0)); |
| 94 | + let cb_count = cb_call_count.clone(); |
| 95 | + |
| 96 | + let on_throttle: ThrottleCallback = Arc::new(move |_h: String| { |
| 97 | + cb_count.fetch_add(1, Ordering::SeqCst); |
| 98 | + // Simulate a callback that raises (e.g. Python ValueError) — error is discarded, |
| 99 | + // as transport.rs does with `if let Err(e) = py_cb.call1(py, (h,)) {}`. |
| 100 | + let _discarded: Result<(), &str> = Err("ValueError: simulated throttle error"); |
| 101 | + }); |
| 102 | + |
| 103 | + let config = RetryConfig { |
| 104 | + max_retries: 2, |
| 105 | + initial_backoff: Duration::from_millis(1), |
| 106 | + max_backoff: Duration::from_millis(5), |
| 107 | + on_throttle: Some(on_throttle), |
| 108 | + host: "test-index.svc.pinecone.io".into(), |
| 109 | + ..RetryConfig::default() |
| 110 | + }; |
| 111 | + |
| 112 | + let result = retry_on_transient(&config, || { |
| 113 | + let count = count.clone(); |
| 114 | + async move { |
| 115 | + let n = count.fetch_add(1, Ordering::SeqCst); |
| 116 | + if n < 2 { |
| 117 | + Err(Status::resource_exhausted("throttled")) |
| 118 | + } else { |
| 119 | + Ok::<(), Status>(()) |
| 120 | + } |
| 121 | + } |
| 122 | + }) |
| 123 | + .await; |
| 124 | + |
| 125 | + assert!( |
| 126 | + result.is_ok(), |
| 127 | + "retry should succeed despite callback raising" |
| 128 | + ); |
| 129 | + assert_eq!( |
| 130 | + cb_call_count.load(Ordering::SeqCst), |
| 131 | + 2, |
| 132 | + "callback should fire on each retryable error" |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +#[tokio::test] |
| 137 | +async fn host_string_received_by_callback() { |
| 138 | + let expected_host = "my-index-abc123.svc.pinecone.io"; |
| 139 | + let received_hosts: Arc<std::sync::Mutex<Vec<String>>> = |
| 140 | + Arc::new(std::sync::Mutex::new(Vec::new())); |
| 141 | + let hosts_clone = received_hosts.clone(); |
| 142 | + |
| 143 | + let on_throttle: ThrottleCallback = Arc::new(move |h: String| { |
| 144 | + hosts_clone.lock().unwrap().push(h); |
| 145 | + }); |
| 146 | + |
| 147 | + let config = RetryConfig { |
| 148 | + max_retries: 2, |
| 149 | + initial_backoff: Duration::from_millis(1), |
| 150 | + max_backoff: Duration::from_millis(5), |
| 151 | + on_throttle: Some(on_throttle), |
| 152 | + host: expected_host.to_string(), |
| 153 | + ..RetryConfig::default() |
| 154 | + }; |
| 155 | + |
| 156 | + let _ = retry_on_transient(&config, || async { |
| 157 | + Err::<(), Status>(Status::resource_exhausted("throttled")) |
| 158 | + }) |
| 159 | + .await; |
| 160 | + |
| 161 | + let hosts = received_hosts.lock().unwrap(); |
| 162 | + assert!( |
| 163 | + !hosts.is_empty(), |
| 164 | + "callback should have been invoked at least once" |
| 165 | + ); |
| 166 | + assert!( |
| 167 | + hosts.iter().all(|h| h == expected_host), |
| 168 | + "callback received unexpected host strings: {:?}", |
| 169 | + hosts |
| 170 | + ); |
| 171 | +} |
0 commit comments