Skip to content

Commit feba1aa

Browse files
committed
transport/session_test: Added unit and integration test for MonotonicTimestampGenerator
1 parent 8ea2c61 commit feba1aa

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

scylla/src/transport/session_test.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,65 @@ async fn test_timestamp() {
13071307
assert_eq!(results, expected_results);
13081308
}
13091309

1310+
#[tokio::test]
1311+
async fn test_timestamp_generator() {
1312+
setup_tracing();
1313+
use crate::transport::timestamp_generator::MonotonicTimestampGenerator;
1314+
1315+
let session = create_new_session_builder()
1316+
.timestamp_generator(Arc::new(MonotonicTimestampGenerator::new()))
1317+
.build()
1318+
.await
1319+
.unwrap();
1320+
let ks = unique_keyspace_name();
1321+
1322+
session.query_unpaged(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks), &[]).await.unwrap();
1323+
session
1324+
.query_unpaged(
1325+
format!(
1326+
"CREATE TABLE IF NOT EXISTS {}.t_generator (a int, b int, primary key (a))",
1327+
ks
1328+
),
1329+
&[],
1330+
)
1331+
.await
1332+
.unwrap();
1333+
1334+
session.await_schema_agreement().await.unwrap();
1335+
1336+
for n in 1..100 {
1337+
let prepared = session
1338+
.prepare(format!(
1339+
"INSERT INTO {}.t_generator (a, b) VALUES (?, ?)",
1340+
ks
1341+
))
1342+
.await
1343+
.unwrap();
1344+
session.execute_unpaged(&prepared, (n, n)).await.unwrap();
1345+
}
1346+
1347+
let query_rows_result = session
1348+
.query_unpaged(
1349+
format!("SELECT a, b, WRITETIME(b) FROM {}.t_generator", ks),
1350+
&[],
1351+
)
1352+
.await
1353+
.unwrap()
1354+
.into_rows_result()
1355+
.unwrap();
1356+
1357+
let mut results = query_rows_result
1358+
.rows::<(i32, i32, i64)>()
1359+
.unwrap()
1360+
.map(Result::unwrap)
1361+
.collect::<Vec<_>>();
1362+
results.sort();
1363+
let timestamps = results.into_iter().map(|x| x.2).collect::<Vec<_>>();
1364+
let mut sorted_timestamps = timestamps.clone();
1365+
sorted_timestamps.sort();
1366+
assert_eq!(timestamps, sorted_timestamps);
1367+
}
1368+
13101369
#[ignore = "works on remote Scylla instances only (local ones are too fast)"]
13111370
#[tokio::test]
13121371
async fn test_request_timeout() {

scylla/src/transport/timestamp_generator.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,19 @@ impl TimestampGenerator for MonotonicTimestampGenerator {
9898
}
9999
}
100100
}
101+
102+
#[tokio::test]
103+
async fn monotonic_timestamp_generator_is_monotonic() {
104+
const NUMBER_OF_ITERATIONS: u32 = 1000;
105+
106+
let mut prev = None;
107+
let mut cur;
108+
let generator = MonotonicTimestampGenerator::new();
109+
for _ in 0..NUMBER_OF_ITERATIONS {
110+
cur = generator.next_timestamp().await;
111+
if let Some(prev_val) = prev {
112+
assert!(cur > prev_val);
113+
}
114+
prev = Some(cur);
115+
}
116+
}

0 commit comments

Comments
 (0)