Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ SCYLLA_TEST_FILTER := $(subst ${SPACE},${EMPTY},ClusterTests.*\
:-SchemaMetadataTest.Integration_Cassandra_RegularMetadataNotMarkedVirtual\
:SchemaMetadataTest.Integration_Cassandra_VirtualMetadata\
:HeartbeatTests.Integration_Cassandra_HeartbeatFailed\
:TimestampTests.Integration_Cassandra_MonotonicTimestampGenerator\
:ExecutionProfileTest.Integration_Cassandra_RoundRobin\
:ExecutionProfileTest.Integration_Cassandra_TokenAwareRouting\
:ExecutionProfileTest.Integration_Cassandra_SpeculativeExecutionPolicy\
Expand Down Expand Up @@ -117,7 +116,6 @@ CASSANDRA_TEST_FILTER := $(subst ${SPACE},${EMPTY},ClusterTests.*\
:SchemaMetadataTest.Integration_Cassandra_RegularMetadataNotMarkedVirtual\
:SchemaMetadataTest.Integration_Cassandra_VirtualMetadata\
:HeartbeatTests.Integration_Cassandra_HeartbeatFailed\
:TimestampTests.Integration_Cassandra_MonotonicTimestampGenerator\
:ExecutionProfileTest.Integration_Cassandra_RoundRobin\
:ExecutionProfileTest.Integration_Cassandra_TokenAwareRouting\
:ExecutionProfileTest.Integration_Cassandra_SpeculativeExecutionPolicy\
Expand Down
2 changes: 2 additions & 0 deletions scylla-rust-wrapper/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,8 @@ pub mod integration_testing {
testing_retry_policy_ignoring_new,
testing_statement_set_recording_history_listener,
testing_statement_set_sleeping_history_listener,
testing_timestamp_gen_contains_timestamp,
testing_timestamp_gen_monotonic_new,
};

/// Stubs of functions that must be implemented for the integration tests to compile,
Expand Down
4 changes: 4 additions & 0 deletions scylla-rust-wrapper/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ pub unsafe extern "C" fn cass_cluster_set_timestamp_gen(
CassTimestampGen::Monotonic(monotonic_timestamp_generator) => {
Some(Arc::clone(monotonic_timestamp_generator) as _)
}
#[cfg(cpp_integration_testing)]
CassTimestampGen::RecordingMonotonic(recording_timestamp_generator) => {
Some(Arc::clone(recording_timestamp_generator) as _)
}
};

cluster.session_builder.config.timestamp_generator = rust_timestamp_gen;
Expand Down
42 changes: 40 additions & 2 deletions scylla-rust-wrapper/src/testing/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use scylla::policies::retry::RetryDecision;

use crate::argconv::{
ArcFFI, BoxFFI, CConst, CMut, CassBorrowedExclusivePtr, CassBorrowedSharedPtr,
CassOwnedSharedPtr,
CassOwnedExclusivePtr, CassOwnedSharedPtr,
};
use crate::cluster::CassCluster;
use crate::future::{CassFuture, CassResultValue};
Expand All @@ -19,7 +19,8 @@ use crate::retry_policy::CassRetryPolicy;
use crate::runtime::Runtime;
use crate::statements::batch::CassBatch;
use crate::statements::statement::{BoundStatement, CassStatement};
use crate::types::{cass_bool_t, cass_int32_t, cass_uint16_t, cass_uint64_t, size_t};
use crate::timestamp_generator::{CassTimestampGen, RecordingTimestampGenerator};
use crate::types::{cass_bool_t, cass_int32_t, cass_int64_t, cass_uint16_t, cass_uint64_t, size_t};

#[unsafe(no_mangle)]
pub unsafe extern "C" fn testing_cluster_get_connect_timeout(
Expand Down Expand Up @@ -378,6 +379,43 @@ pub unsafe extern "C" fn testing_retry_policy_ignoring_new()
))))
}

/// Creates a new monotonic timestamp generator that records all generated timestamps.
/// This is useful for testing purposes, allowing us to verify which timestamps were
/// generated during query execution.
/// The recorded timestamps can later be queried using `testing_timestamp_gen_contains_timestamp`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn testing_timestamp_gen_monotonic_new()
-> CassOwnedExclusivePtr<CassTimestampGen, CMut> {
BoxFFI::into_ptr(Box::new(CassTimestampGen::RecordingMonotonic(Arc::new(
RecordingTimestampGenerator::new(),
))))
}

/// Checks whether the given timestamp was generated by the recording monotonic
/// timestamp generator. Returns `cass_true` if found, `cass_false` otherwise.
///
/// If the provided generator is not a recording monotonic generator, returns `cass_false`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn testing_timestamp_gen_contains_timestamp(
timestamp_gen_raw: CassBorrowedExclusivePtr<CassTimestampGen, CMut>,
timestamp: cass_int64_t,
) -> cass_bool_t {
let Some(timestamp_gen) = BoxFFI::as_ref(timestamp_gen_raw) else {
return 0;
};

match timestamp_gen {
CassTimestampGen::RecordingMonotonic(recording) => {
if recording.contains(timestamp) {
1
} else {
0
}
}
_ => 0,
}
}

/// Stubs of functions that must be implemented for the integration tests
/// or examples to compile, but the proper implementation is not needed for
/// the tests/examples to run, and at the same time the functions are not
Expand Down
36 changes: 36 additions & 0 deletions scylla-rust-wrapper/src/timestamp_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,48 @@ use std::sync::Arc;
use std::time::Duration;

use scylla::policies::timestamp_generator::MonotonicTimestampGenerator;
#[cfg(cpp_integration_testing)]
use scylla::policies::timestamp_generator::TimestampGenerator;

use crate::argconv::{BoxFFI, CMut, CassOwnedExclusivePtr, FFI, FromBox};

pub enum CassTimestampGen {
ServerSide,
Monotonic(Arc<MonotonicTimestampGenerator>),
#[cfg(cpp_integration_testing)]
RecordingMonotonic(Arc<RecordingTimestampGenerator>),
}

/// A wrapper around `MonotonicTimestampGenerator` that records all generated timestamps.
/// This is used for integration testing purposes only.
#[cfg(cpp_integration_testing)]
#[allow(unnameable_types)]
pub struct RecordingTimestampGenerator {
inner: MonotonicTimestampGenerator,
timestamps: std::sync::Mutex<Vec<i64>>,
}

#[cfg(cpp_integration_testing)]
impl RecordingTimestampGenerator {
pub fn new() -> Self {
Self {
inner: MonotonicTimestampGenerator::new(),
timestamps: std::sync::Mutex::new(Vec::new()),
}
}

pub fn contains(&self, timestamp: i64) -> bool {
self.timestamps.lock().unwrap().contains(&timestamp)
}
}

#[cfg(cpp_integration_testing)]
impl TimestampGenerator for RecordingTimestampGenerator {
fn next_timestamp(&self) -> i64 {
let ts = self.inner.next_timestamp();
self.timestamps.lock().unwrap().push(ts);
ts
}
}

impl FFI for CassTimestampGen {
Expand Down
10 changes: 10 additions & 0 deletions src/testing_rust_impls.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ CASS_EXPORT void testing_statement_set_recording_history_listener(CassStatement*
// The returned pointer is allocated and must be freed with `testing_free_cstring`.
CASS_EXPORT char* testing_future_get_attempted_hosts(CassFuture* future);

// Creates a new monotonic timestamp generator that records all generated timestamps.
// Recorded timestamps can be queried using `testing_timestamp_gen_contains_timestamp`.
// The returned generator must be freed with `cass_timestamp_gen_free`.
CASS_EXPORT CassTimestampGen* testing_timestamp_gen_monotonic_new();

// Checks whether the given timestamp was generated by the recording monotonic generator.
// Returns cass_true if the timestamp was generated, cass_false otherwise.
CASS_EXPORT cass_bool_t testing_timestamp_gen_contains_timestamp(CassTimestampGen* timestamp_gen,
cass_int64_t timestamp);

/**
* Creates a new ignoring retry policy.
*
Expand Down
63 changes: 0 additions & 63 deletions src/timestamp_generator.cpp

This file was deleted.

87 changes: 0 additions & 87 deletions src/timestamp_generator.hpp

This file was deleted.

1 change: 0 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ set(CPP_DRIVER_SOURCE_FILES
${CASS_SRC_DIR}/get_time-win.cpp
${CASS_SRC_DIR}/address.cpp
${CASS_SRC_DIR}/memory.cpp
${CASS_SRC_DIR}/timestamp_generator.cpp
${CASS_SRC_DIR}/testing.cpp
${CASS_SRC_DIR}/logger.cpp
${CASS_SRC_DIR}/testing_unimplemented.cpp
Expand Down
Loading