Skip to content

Commit 72578fc

Browse files
committed
transport/session_test: Added tests for MonotonicTimestampGenerator
1 parent b0931b0 commit 72578fc

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
@@ -1328,6 +1328,65 @@ async fn test_timestamp() {
13281328
assert_eq!(results, expected_results);
13291329
}
13301330

1331+
#[tokio::test]
1332+
async fn test_timestamp_generator() {
1333+
setup_tracing();
1334+
use crate::transport::timestamp_generator::MonotonicTimestampGenerator;
1335+
1336+
let session = create_new_session_builder()
1337+
.timestamp_generator(Arc::new(MonotonicTimestampGenerator::new()))
1338+
.build()
1339+
.await
1340+
.unwrap();
1341+
let ks = unique_keyspace_name();
1342+
1343+
session.query_unpaged(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks), &[]).await.unwrap();
1344+
session
1345+
.query_unpaged(
1346+
format!(
1347+
"CREATE TABLE IF NOT EXISTS {}.t_generator (a int, b int, primary key (a))",
1348+
ks
1349+
),
1350+
&[],
1351+
)
1352+
.await
1353+
.unwrap();
1354+
1355+
session.await_schema_agreement().await.unwrap();
1356+
1357+
for n in 1..100 {
1358+
let prepared = session
1359+
.prepare(format!(
1360+
"INSERT INTO {}.t_generator (a, b) VALUES (?, ?)",
1361+
ks
1362+
))
1363+
.await
1364+
.unwrap();
1365+
session.execute_unpaged(&prepared, (n, n)).await.unwrap();
1366+
}
1367+
1368+
let query_rows_result = session
1369+
.query_unpaged(
1370+
format!("SELECT a, b, WRITETIME(b) FROM {}.t_generator", ks),
1371+
&[],
1372+
)
1373+
.await
1374+
.unwrap()
1375+
.into_rows_result()
1376+
.unwrap();
1377+
1378+
let mut results = query_rows_result
1379+
.rows::<(i32, i32, i64)>()
1380+
.unwrap()
1381+
.map(Result::unwrap)
1382+
.collect::<Vec<_>>();
1383+
results.sort();
1384+
let timestamps = results.into_iter().map(|x| x.2).collect::<Vec<_>>();
1385+
let mut sorted_timestamps = timestamps.clone();
1386+
sorted_timestamps.sort();
1387+
assert_eq!(timestamps, sorted_timestamps);
1388+
}
1389+
13311390
#[ignore = "works on remote Scylla instances only (local ones are too fast)"]
13321391
#[tokio::test]
13331392
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
@@ -125,3 +125,19 @@ impl TimestampGenerator for MonotonicTimestampGenerator {
125125
}
126126
}
127127
}
128+
129+
#[tokio::test]
130+
async fn monotonic_timestamp_generator_is_monotonic() {
131+
const NUMBER_OF_ITERATIONS: u32 = 1000;
132+
133+
let mut prev = None;
134+
let mut cur;
135+
let generator = MonotonicTimestampGenerator::new();
136+
for _ in 0..NUMBER_OF_ITERATIONS {
137+
cur = generator.next_timestamp();
138+
if let Some(prev_val) = prev {
139+
assert!(cur > prev_val);
140+
}
141+
prev = Some(cur);
142+
}
143+
}

0 commit comments

Comments
 (0)