-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfuture.rs
More file actions
766 lines (678 loc) · 28.7 KB
/
future.rs
File metadata and controls
766 lines (678 loc) · 28.7 KB
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
use crate::RUNTIME;
use crate::argconv::*;
use crate::cass_error::CassError;
use crate::cass_error::CassErrorMessage;
use crate::cass_error::ToCassError;
use crate::execution_error::CassErrorResult;
use crate::prepared::CassPrepared;
use crate::query_result::CassResult;
use crate::types::*;
use crate::uuid::CassUuid;
use futures::future;
use std::future::Future;
use std::mem;
use std::os::raw::c_void;
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use tokio::task::JoinHandle;
use tokio::time::Duration;
pub enum CassResultValue {
Empty,
QueryResult(Arc<CassResult>),
QueryError(Arc<CassErrorResult>),
Prepared(Arc<CassPrepared>),
}
type CassFutureError = (CassError, String);
pub type CassFutureResult = Result<CassResultValue, CassFutureError>;
pub type CassFutureCallback = Option<
unsafe extern "C" fn(future: CassBorrowedSharedPtr<CassFuture, CMut>, data: *mut c_void),
>;
struct BoundCallback {
pub cb: CassFutureCallback,
pub data: *mut c_void,
}
// *mut c_void is not Send, so Rust will have to take our word
// that we won't screw something up
unsafe impl Send for BoundCallback {}
impl BoundCallback {
fn invoke(self, fut_ptr: CassBorrowedSharedPtr<CassFuture, CMut>) {
unsafe {
self.cb.unwrap()(fut_ptr, self.data);
}
}
}
/// State of the execution of the [CassFuture],
/// together with a join handle of the tokio task that is executing it.
struct CassFutureState {
execution_state: CassFutureExecution,
/// Presence of this handle while `execution_state` is not `Completed` indicates
/// that no thread is currently blocked on the future. This means that it might
/// not be executed (especially in case of the current-thread executor).
/// Absence means that some thread has blocked on the future, so it is necessarily
/// being executed.
join_handle: Option<JoinHandle<()>>,
}
/// State of the execution of the [CassFuture].
enum CassFutureExecution {
RunningWithoutCallback,
RunningWithCallback { callback: BoundCallback },
Completed(CassFutureCompleted),
}
impl CassFutureExecution {
fn completed(&self) -> bool {
match self {
Self::Completed(_) => true,
Self::RunningWithCallback { .. } | Self::RunningWithoutCallback => false,
}
}
/// Sets callback for the [CassFuture]. If the future has not completed yet,
/// the callback will be invoked once the future is completed, by the executor thread.
/// If the future has already completed, the callback will be invoked immediately.
unsafe fn set_callback(
mut state_lock: MutexGuard<CassFutureState>,
fut_ptr: CassBorrowedSharedPtr<CassFuture, CMut>,
cb: CassFutureCallback,
data: *mut c_void,
) -> CassError {
let bound_cb = BoundCallback { cb, data };
match state_lock.execution_state {
Self::RunningWithoutCallback => {
// Store the callback.
state_lock.execution_state = Self::RunningWithCallback { callback: bound_cb };
CassError::CASS_OK
}
Self::RunningWithCallback { .. } =>
// Another callback has been already set.
{
CassError::CASS_ERROR_LIB_CALLBACK_ALREADY_SET
}
Self::Completed { .. } => {
// The value is already available, we need to call the callback ourselves.
mem::drop(state_lock);
bound_cb.invoke(fut_ptr);
CassError::CASS_OK
}
}
}
/// Sets the [CassFuture] as completed. This function is called by the executor thread
/// once it completes the underlying Rust future. If there's a callback set,
/// it will be invoked immediately.
fn complete(
mut state_lock: MutexGuard<CassFutureState>,
value: CassFutureResult,
cass_fut: &Arc<CassFuture>,
) {
let prev_state = mem::replace(
&mut state_lock.execution_state,
Self::Completed(CassFutureCompleted::new(value)),
);
// This is because we mustn't hold the lock while invoking the callback.
mem::drop(state_lock);
let maybe_cb = match prev_state {
Self::RunningWithoutCallback => None,
Self::RunningWithCallback { callback } => Some(callback),
Self::Completed { .. } => unreachable!(
"Exactly one dedicated tokio task is expected to execute and complete the CassFuture."
),
};
if let Some(bound_cb) = maybe_cb {
let fut_ptr = ArcFFI::as_ptr::<CMut>(cass_fut);
// Safety: pointer is valid, because we get it from arc allocation.
bound_cb.invoke(fut_ptr);
}
}
}
/// The result of a completed [CassFuture].
struct CassFutureCompleted {
/// The result of the future, either a value or an error.
value: CassFutureResult,
/// Just a cache for the error message. Needed because the C API exposes a pointer to the
/// error message, and we need to keep it alive until the future is freed.
/// Initially, it's `None`, and it is set to `Some` when the error message is requested
/// by `cass_future_error_message()`.
cached_err_string: Option<String>,
}
impl CassFutureCompleted {
fn new(value: CassFutureResult) -> Self {
Self {
value,
cached_err_string: None,
}
}
}
/// The C-API representation of a future. Implemented as a wrapper around a Rust future
/// that can be awaited and has a callback mechanism. It's **eager** in a way that
/// its execution starts possibly immediately (unless the executor thread pool is nempty,
/// which is the case for the current-thread executor).
pub struct CassFuture {
state: Mutex<CassFutureState>,
wait_for_value: Condvar,
}
impl FFI for CassFuture {
type Origin = FromArc;
}
/// An error that can appear during `cass_future_wait_timed`.
enum FutureError {
TimeoutError,
InvalidDuration,
}
/// The timeout appeared when we tried to await `JoinHandle`.
/// This errors contains the original handle, so it can be awaited later again.
struct JoinHandleTimeout(JoinHandle<()>);
impl CassFuture {
pub fn make_raw(
fut: impl Future<Output = CassFutureResult> + Send + 'static,
) -> CassOwnedSharedPtr<CassFuture, CMut> {
Self::new_from_future(fut).into_raw()
}
pub fn new_from_future(
fut: impl Future<Output = CassFutureResult> + Send + 'static,
) -> Arc<CassFuture> {
let cass_fut = Arc::new(CassFuture {
state: Mutex::new(CassFutureState {
join_handle: None,
execution_state: CassFutureExecution::RunningWithoutCallback,
}),
wait_for_value: Condvar::new(),
});
let cass_fut_clone = Arc::clone(&cass_fut);
let join_handle = RUNTIME.spawn(async move {
let r = fut.await;
let guard = cass_fut_clone.state.lock().unwrap();
CassFutureExecution::complete(guard, r, &cass_fut_clone);
cass_fut_clone.wait_for_value.notify_all();
});
{
let mut lock = cass_fut.state.lock().unwrap();
lock.join_handle = Some(join_handle);
}
cass_fut
}
pub fn new_ready(r: CassFutureResult) -> Arc<Self> {
Arc::new(CassFuture {
state: Mutex::new(CassFutureState {
join_handle: None,
execution_state: CassFutureExecution::Completed(CassFutureCompleted::new(r)),
}),
wait_for_value: Condvar::new(),
})
}
pub fn with_waited_result<T>(&self, f: impl FnOnce(&mut CassFutureResult) -> T) -> T {
self.with_waited_state(|s| f(&mut s.value))
}
/// Awaits the future until completion.
///
/// There are two possible cases:
/// - noone is currently working on the future -> we take the ownership
/// of JoinHandle (future) and we poll it until completion.
/// - some other thread is working on the future -> we wait on the condition
/// variable to get an access to the future's state. Once we are notified,
/// there are two cases:
/// - JoinHandle is consumed -> some other thread already resolved the future.
/// We can return.
/// - JoinHandle is Some -> some other thread was working on the future, but
/// timed out (see [CassFuture::with_waited_state_timed]). We need to
/// take the ownership of the handle, and complete the work.
fn with_waited_state<T>(&self, f: impl FnOnce(&mut CassFutureCompleted) -> T) -> T {
let mut guard = self.state.lock().unwrap();
loop {
let handle = guard.join_handle.take();
if let Some(handle) = handle {
mem::drop(guard);
// unwrap: JoinError appears only when future either panic'ed or canceled.
RUNTIME.block_on(handle).unwrap();
guard = self.state.lock().unwrap();
} else {
guard = self
.wait_for_value
.wait_while(guard, |state| {
!state.execution_state.completed() && state.join_handle.is_none()
})
// unwrap: Error appears only when mutex is poisoned.
.unwrap();
if guard.join_handle.is_some() {
// join_handle was none, and now it isn't - some other thread must
// have timed out and returned the handle. We need to take over
// the work of completing the future. To do that, we go into
// another iteration so that we land in the branch with block_on.
continue;
}
}
// If we had ended up in either the handle's or with the condvar's `if` branch,
// we awaited the future and it is now completed.
let completed = match &mut guard.execution_state {
CassFutureExecution::RunningWithoutCallback
| CassFutureExecution::RunningWithCallback { .. } => unreachable!(),
CassFutureExecution::Completed(completed) => completed,
};
return f(completed);
}
}
fn with_waited_result_timed<T>(
&self,
f: impl FnOnce(&mut CassFutureResult) -> T,
timeout_duration: Duration,
) -> Result<T, FutureError> {
self.with_waited_state_timed(|s| f(&mut s.value), timeout_duration)
}
/// Tries to await the future with a given timeout.
///
/// There are two possible cases:
/// - noone is currently working on the future -> we take the ownership
/// of JoinHandle (future) and we try to poll it with given timeout.
/// If we timed out, we need to return the unfinished JoinHandle, so
/// some other thread can complete the future later.
/// - some other thread is working on the future -> we wait on the condition
/// variable to get an access to the future's state.
/// Once we are notified (before the timeout), there are two cases.
/// - JoinHandle is consumed -> some other thread already resolved the future.
/// We can return.
/// - JoinHandle is Some -> some other thread was working on the future, but
/// timed out (see [CassFuture::with_waited_state_timed]). We need to
/// take the ownership of the handle, and continue the work.
fn with_waited_state_timed<T>(
&self,
f: impl FnOnce(&mut CassFutureCompleted) -> T,
timeout_duration: Duration,
) -> Result<T, FutureError> {
let mut guard = self.state.lock().unwrap();
let deadline = tokio::time::Instant::now()
.checked_add(timeout_duration)
.ok_or(FutureError::InvalidDuration)?;
loop {
let handle = guard.join_handle.take();
if let Some(handle) = handle {
mem::drop(guard);
// Need to wrap it with async{} block, so the timeout is lazily executed inside the runtime.
// See mention about panics: https://docs.rs/tokio/latest/tokio/time/fn.timeout.html.
let timed = async {
let sleep_future = tokio::time::sleep_until(deadline);
tokio::pin!(sleep_future);
let value = future::select(handle, sleep_future).await;
match value {
future::Either::Left((result, _)) => Ok(result),
future::Either::Right((_, handle)) => Err(JoinHandleTimeout(handle)),
}
};
match RUNTIME.block_on(timed) {
Err(JoinHandleTimeout(returned_handle)) => {
// We timed out. so we can't finish waiting for the future.
// The problem is that if current thread executor is used,
// then no one will run this future - other threads will
// go into the branch with condvar and wait there.
// To fix that:
// - Return the join handle, so that next thread can take it
// - Signal one thread, so that if all other consumers are
// already waiting on condvar, one of them wakes up and
// picks up the work.
guard = self.state.lock().unwrap();
guard.join_handle = Some(returned_handle);
self.wait_for_value.notify_one();
return Err(FutureError::TimeoutError);
}
// unwrap: JoinError appears only when future either panic'ed or canceled.
Ok(result) => result.unwrap(),
};
guard = self.state.lock().unwrap();
} else {
let remaining_timeout = deadline.duration_since(tokio::time::Instant::now());
let (guard_result, timeout_result) = self
.wait_for_value
.wait_timeout_while(guard, remaining_timeout, |state| {
!state.execution_state.completed() && state.join_handle.is_none()
})
// unwrap: Error appears only when mutex is poisoned.
.unwrap();
if timeout_result.timed_out() {
return Err(FutureError::TimeoutError);
}
guard = guard_result;
if guard.join_handle.is_some() {
// join_handle was none, and now it isn't - some other thread must
// have timed out and returned the handle. We need to take over
// the work of completing the future. To do that, we go into
// another iteration so that we land in the branch with block_on.
continue;
}
}
// If we had ended up in either the handle's or with the condvar's `if` branch
// and we didn't return `TimeoutError`, we awaited the future and it is now completed.
let completed = match &mut guard.execution_state {
CassFutureExecution::RunningWithoutCallback
| CassFutureExecution::RunningWithCallback { .. } => unreachable!(),
CassFutureExecution::Completed(completed) => completed,
};
return Ok(f(completed));
}
}
pub unsafe fn set_callback(
&self,
self_ptr: CassBorrowedSharedPtr<CassFuture, CMut>,
cb: CassFutureCallback,
data: *mut c_void,
) -> CassError {
let lock = self.state.lock().unwrap();
unsafe { CassFutureExecution::set_callback(lock, self_ptr, cb, data) }
}
fn into_raw(self: Arc<Self>) -> CassOwnedSharedPtr<Self, CMut> {
ArcFFI::into_ptr(self)
}
}
// Do not remove; this asserts that `CassFuture` implements Send + Sync,
// which is required by the cpp-driver (saying that `CassFuture` is thread-safe).
#[allow(unused)]
trait CheckSendSync: Send + Sync {}
impl CheckSendSync for CassFuture {}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_set_callback(
future_raw: CassBorrowedSharedPtr<CassFuture, CMut>,
callback: CassFutureCallback,
data: *mut ::std::os::raw::c_void,
) -> CassError {
let Some(future) = ArcFFI::as_ref(future_raw.borrow()) else {
tracing::error!("Provided null future pointer to cass_future_set_callback!");
return CassError::CASS_ERROR_LIB_BAD_PARAMS;
};
unsafe { future.set_callback(future_raw.borrow(), callback, data) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_wait(future_raw: CassBorrowedSharedPtr<CassFuture, CMut>) {
let Some(future) = ArcFFI::as_ref(future_raw) else {
tracing::error!("Provided null future pointer to cass_future_wait!");
return;
};
future.with_waited_result(|_| ());
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_wait_timed(
future_raw: CassBorrowedSharedPtr<CassFuture, CMut>,
timeout_us: cass_duration_t,
) -> cass_bool_t {
let Some(future) = ArcFFI::as_ref(future_raw) else {
tracing::error!("Provided null future pointer to cass_future_wait_timed!");
return cass_false;
};
future
.with_waited_result_timed(|_| (), Duration::from_micros(timeout_us))
.is_ok() as cass_bool_t
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_ready(
future_raw: CassBorrowedSharedPtr<CassFuture, CMut>,
) -> cass_bool_t {
let Some(future) = ArcFFI::as_ref(future_raw) else {
tracing::error!("Provided null future pointer to cass_future_ready!");
return cass_false;
};
let state_guard = future.state.lock().unwrap();
state_guard.execution_state.completed() as cass_bool_t
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_error_code(
future_raw: CassBorrowedSharedPtr<CassFuture, CMut>,
) -> CassError {
let Some(future) = ArcFFI::as_ref(future_raw) else {
tracing::error!("Provided null future pointer to cass_future_error_code!");
return CassError::CASS_ERROR_LIB_BAD_PARAMS;
};
future.with_waited_result(|r: &mut CassFutureResult| match r {
Ok(CassResultValue::QueryError(err)) => err.to_cass_error(),
Err((err, _)) => *err,
_ => CassError::CASS_OK,
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_error_message(
future: CassBorrowedSharedPtr<CassFuture, CMut>,
message: *mut *const ::std::os::raw::c_char,
message_length: *mut size_t,
) {
let Some(future) = ArcFFI::as_ref(future) else {
tracing::error!("Provided null future pointer to cass_future_error_message!");
return;
};
future.with_waited_state(|completed: &mut CassFutureCompleted| {
let msg = completed
.cached_err_string
.get_or_insert_with(|| match &completed.value {
Ok(CassResultValue::QueryError(err)) => err.msg(),
Err((_, s)) => s.msg(),
_ => "".to_string(),
});
unsafe { write_str_to_c(msg.as_str(), message, message_length) };
});
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_free(future_raw: CassOwnedSharedPtr<CassFuture, CMut>) {
ArcFFI::free(future_raw);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_get_result(
future_raw: CassBorrowedSharedPtr<CassFuture, CMut>,
) -> CassOwnedSharedPtr<CassResult, CConst> {
let Some(future) = ArcFFI::as_ref(future_raw) else {
tracing::error!("Provided null future pointer to cass_future_get_result!");
return ArcFFI::null();
};
future
.with_waited_result(|r: &mut CassFutureResult| -> Option<Arc<CassResult>> {
match r.as_ref().ok()? {
CassResultValue::QueryResult(qr) => Some(Arc::clone(qr)),
_ => None,
}
})
.map_or(ArcFFI::null(), ArcFFI::into_ptr)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_get_error_result(
future_raw: CassBorrowedSharedPtr<CassFuture, CMut>,
) -> CassOwnedSharedPtr<CassErrorResult, CConst> {
let Some(future) = ArcFFI::as_ref(future_raw) else {
tracing::error!("Provided null future pointer to cass_future_get_error_result!");
return ArcFFI::null();
};
future
.with_waited_result(|r: &mut CassFutureResult| -> Option<Arc<CassErrorResult>> {
match r.as_ref().ok()? {
CassResultValue::QueryError(qr) => Some(Arc::clone(qr)),
_ => None,
}
})
.map_or(ArcFFI::null(), ArcFFI::into_ptr)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_get_prepared(
future_raw: CassBorrowedSharedPtr<CassFuture, CMut>,
) -> CassOwnedSharedPtr<CassPrepared, CConst> {
let Some(future) = ArcFFI::as_ref(future_raw) else {
tracing::error!("Provided null future pointer to cass_future_get_prepared!");
return ArcFFI::null();
};
future
.with_waited_result(|r: &mut CassFutureResult| -> Option<Arc<CassPrepared>> {
match r.as_ref().ok()? {
CassResultValue::Prepared(p) => Some(Arc::clone(p)),
_ => None,
}
})
.map_or(ArcFFI::null(), ArcFFI::into_ptr)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_future_tracing_id(
future: CassBorrowedSharedPtr<CassFuture, CMut>,
tracing_id: *mut CassUuid,
) -> CassError {
let Some(future) = ArcFFI::as_ref(future) else {
tracing::error!("Provided null future pointer to cass_future_tracing_id!");
return CassError::CASS_ERROR_LIB_BAD_PARAMS;
};
future.with_waited_result(|r: &mut CassFutureResult| match r {
Ok(CassResultValue::QueryResult(result)) => match result.tracing_id {
Some(id) => {
unsafe { *tracing_id = CassUuid::from(id) };
CassError::CASS_OK
}
None => CassError::CASS_ERROR_LIB_NO_TRACING_ID,
},
_ => CassError::CASS_ERROR_LIB_INVALID_FUTURE_TYPE,
})
}
#[cfg(test)]
mod tests {
use crate::testing::{assert_cass_error_eq, assert_cass_future_error_message_eq};
use super::*;
use std::{
os::raw::c_char,
thread::{self},
time::Duration,
};
// This is not a particularly smart test, but if some thread is granted access the value
// before it is truly computed, then weird things should happen, even a segfault.
// In the incorrect implementation that inspired this test to be written, this test
// results with unwrap on a PoisonError on the CassFuture's mutex.
#[test]
#[ntest::timeout(100)]
fn cass_future_thread_safety() {
const ERROR_MSG: &str = "NOBODY EXPECTED SPANISH INQUISITION";
let fut = async {
tokio::time::sleep(Duration::from_millis(10)).await;
Err((CassError::CASS_OK, ERROR_MSG.into()))
};
let cass_fut = CassFuture::make_raw(fut);
struct PtrWrapper(CassBorrowedSharedPtr<'static, CassFuture, CMut>);
unsafe impl Send for PtrWrapper {}
unsafe {
// transmute to erase the lifetime to 'static, so the reference
// can be passed to an async block.
let static_cass_fut_ref = std::mem::transmute::<
CassBorrowedSharedPtr<'_, CassFuture, CMut>,
CassBorrowedSharedPtr<'static, CassFuture, CMut>,
>(cass_fut.borrow());
let wrapped_cass_fut = PtrWrapper(static_cass_fut_ref);
let handle = thread::spawn(move || {
let wrapper = &wrapped_cass_fut;
let PtrWrapper(cass_fut) = wrapper;
assert_cass_future_error_message_eq!(cass_fut, Some(ERROR_MSG));
});
assert_cass_future_error_message_eq!(cass_fut, Some(ERROR_MSG));
handle.join().unwrap();
cass_future_free(cass_fut);
}
}
// This test makes sure that the future resolves even if timeout happens.
#[test]
#[ntest::timeout(200)]
fn cass_future_resolves_after_timeout() {
const ERROR_MSG: &str = "NOBODY EXPECTED SPANISH INQUISITION";
const HUNDRED_MILLIS_IN_MICROS: u64 = 100 * 1000;
let fut = async move {
tokio::time::sleep(Duration::from_micros(HUNDRED_MILLIS_IN_MICROS)).await;
Err((CassError::CASS_OK, ERROR_MSG.into()))
};
let cass_fut = CassFuture::make_raw(fut);
unsafe {
// This should timeout on tokio::time::timeout.
let timed_result =
cass_future_wait_timed(cass_fut.borrow(), HUNDRED_MILLIS_IN_MICROS / 5);
assert_eq!(0, timed_result);
// This should timeout as well.
let timed_result =
cass_future_wait_timed(cass_fut.borrow(), HUNDRED_MILLIS_IN_MICROS / 5);
assert_eq!(0, timed_result);
// Verify that future eventually resolves, even though timeouts occurred before.
assert_cass_future_error_message_eq!(cass_fut, Some(ERROR_MSG));
cass_future_free(cass_fut);
}
}
// This test checks whether the future callback is executed correctly when:
// - a future is awaited indefinitely
// - a future is awaited, after the timeout appeared (_wait_timed)
// - a future is not awaited. We simply sleep, and let the tokio runtime resolve
// the future, and execute its callback
#[test]
#[ntest::timeout(600)]
#[allow(clippy::disallowed_methods)]
fn test_cass_future_callback() {
const ERROR_MSG: &str = "NOBODY EXPECTED SPANISH INQUISITION";
const HUNDRED_MILLIS_IN_MICROS: u64 = 100 * 1000;
let create_future_and_flag = || {
unsafe extern "C" fn mark_flag_cb(
_fut: CassBorrowedSharedPtr<CassFuture, CMut>,
data: *mut c_void,
) {
let flag = data as *mut bool;
unsafe {
*flag = true;
}
}
let fut = async move {
tokio::time::sleep(Duration::from_micros(HUNDRED_MILLIS_IN_MICROS)).await;
Err((CassError::CASS_OK, ERROR_MSG.into()))
};
let cass_fut = CassFuture::make_raw(fut);
let flag = Box::new(false);
let flag_ptr = Box::into_raw(flag);
unsafe {
assert_cass_error_eq!(
cass_future_set_callback(
cass_fut.borrow(),
Some(mark_flag_cb),
flag_ptr as *mut c_void,
),
CassError::CASS_OK
)
};
(cass_fut, flag_ptr)
};
// Callback executed after awaiting.
{
let (cass_fut, flag_ptr) = create_future_and_flag();
unsafe { cass_future_wait(cass_fut.borrow()) };
unsafe {
assert_cass_future_error_message_eq!(cass_fut, Some(ERROR_MSG));
}
assert!(unsafe { *flag_ptr });
unsafe { cass_future_free(cass_fut) };
let _ = unsafe { Box::from_raw(flag_ptr) };
}
// Future awaited via `assert_cass_future_error_message_eq`.
{
let (cass_fut, flag_ptr) = create_future_and_flag();
unsafe {
assert_cass_future_error_message_eq!(cass_fut, Some(ERROR_MSG));
}
assert!(unsafe { *flag_ptr });
unsafe { cass_future_free(cass_fut) };
let _ = unsafe { Box::from_raw(flag_ptr) };
}
// Callback executed after timeouts.
{
let (cass_fut, flag_ptr) = create_future_and_flag();
// This should timeout on tokio::time::timeout.
let timed_result =
unsafe { cass_future_wait_timed(cass_fut.borrow(), HUNDRED_MILLIS_IN_MICROS / 5) };
assert_eq!(0, timed_result);
// This should timeout as well.
let timed_result =
unsafe { cass_future_wait_timed(cass_fut.borrow(), HUNDRED_MILLIS_IN_MICROS / 5) };
assert_eq!(0, timed_result);
// Await and check result.
unsafe {
assert_cass_future_error_message_eq!(cass_fut, Some(ERROR_MSG));
}
assert!(unsafe { *flag_ptr });
unsafe { cass_future_free(cass_fut) };
let _ = unsafe { Box::from_raw(flag_ptr) };
}
// Don't await the future. Just sleep.
{
let (cass_fut, flag_ptr) = create_future_and_flag();
RUNTIME.block_on(async {
tokio::time::sleep(Duration::from_micros(HUNDRED_MILLIS_IN_MICROS + 10 * 1000))
.await
});
assert!(unsafe { *flag_ptr });
unsafe { cass_future_free(cass_fut) };
let _ = unsafe { Box::from_raw(flag_ptr) };
}
}
}