|
1 | 1 | use std::ffi::{CString, c_char};
|
| 2 | +use std::net::SocketAddr; |
| 3 | +use std::sync::Arc; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +use scylla::errors::{RequestAttemptError, RequestError}; |
| 7 | +use scylla::observability::history::{AttemptId, HistoryListener, RequestId, SpeculativeId}; |
| 8 | +use scylla::policies::retry::RetryDecision; |
2 | 9 |
|
3 | 10 | use crate::argconv::{BoxFFI, CMut, CassBorrowedExclusivePtr};
|
4 | 11 | use crate::cluster::CassCluster;
|
5 |
| -use crate::types::{cass_int32_t, cass_uint16_t, size_t}; |
| 12 | +use crate::statement::{BoundStatement, CassStatement}; |
| 13 | +use crate::types::{cass_int32_t, cass_uint16_t, cass_uint64_t, size_t}; |
6 | 14 |
|
7 | 15 | #[unsafe(no_mangle)]
|
8 | 16 | pub unsafe extern "C" fn testing_cluster_get_connect_timeout(
|
@@ -54,3 +62,57 @@ pub unsafe extern "C" fn testing_cluster_get_contact_points(
|
54 | 62 | pub unsafe extern "C" fn testing_free_contact_points(contact_points: *mut c_char) {
|
55 | 63 | let _ = unsafe { CString::from_raw(contact_points) };
|
56 | 64 | }
|
| 65 | + |
| 66 | +#[derive(Debug)] |
| 67 | +struct SleepingHistoryListener(Duration); |
| 68 | + |
| 69 | +impl HistoryListener for SleepingHistoryListener { |
| 70 | + fn log_request_start(&self) -> RequestId { |
| 71 | + RequestId(0) |
| 72 | + } |
| 73 | + |
| 74 | + fn log_request_success(&self, _request_id: RequestId) {} |
| 75 | + |
| 76 | + fn log_request_error(&self, _request_id: RequestId, _error: &RequestError) {} |
| 77 | + |
| 78 | + fn log_new_speculative_fiber(&self, _request_id: RequestId) -> SpeculativeId { |
| 79 | + SpeculativeId(0) |
| 80 | + } |
| 81 | + |
| 82 | + fn log_attempt_start( |
| 83 | + &self, |
| 84 | + _request_id: RequestId, |
| 85 | + _speculative_id: Option<SpeculativeId>, |
| 86 | + _node_addr: SocketAddr, |
| 87 | + ) -> AttemptId { |
| 88 | + // Sleep to simulate a delay in the request |
| 89 | + std::thread::sleep(self.0); |
| 90 | + AttemptId(0) |
| 91 | + } |
| 92 | + |
| 93 | + fn log_attempt_success(&self, _attempt_id: AttemptId) {} |
| 94 | + |
| 95 | + fn log_attempt_error( |
| 96 | + &self, |
| 97 | + _attempt_id: AttemptId, |
| 98 | + _error: &RequestAttemptError, |
| 99 | + _retry_decision: &RetryDecision, |
| 100 | + ) { |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +#[unsafe(no_mangle)] |
| 105 | +pub unsafe extern "C" fn testing_statement_set_sleeping_history_listener( |
| 106 | + statement_raw: CassBorrowedExclusivePtr<CassStatement, CMut>, |
| 107 | + sleep_time_ms: cass_uint64_t, |
| 108 | +) { |
| 109 | + let sleep_time = Duration::from_millis(sleep_time_ms); |
| 110 | + let history_listener = Arc::new(SleepingHistoryListener(sleep_time)); |
| 111 | + |
| 112 | + match &mut BoxFFI::as_mut_ref(statement_raw).unwrap().statement { |
| 113 | + BoundStatement::Simple(inner) => inner.query.set_history_listener(history_listener), |
| 114 | + BoundStatement::Prepared(inner) => Arc::make_mut(&mut inner.statement) |
| 115 | + .statement |
| 116 | + .set_history_listener(history_listener), |
| 117 | + } |
| 118 | +} |
0 commit comments