Skip to content

Commit fb937ef

Browse files
committed
fix non-determinism bug arising from a call to thread_rng while picking
which row to check existence for in the result of the select query
1 parent c3ea027 commit fb937ef

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

simulator/generation/property.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
};
88

99
use super::{
10-
frequency, pick,
10+
frequency, pick, pick_index,
1111
plan::{Assertion, Interaction, InteractionStats, ResultSet},
1212
ArbitraryFrom,
1313
};
@@ -34,6 +34,8 @@ pub(crate) enum Property {
3434
InsertSelect {
3535
/// The insert query
3636
insert: Insert,
37+
/// Selected row index
38+
row_index: usize,
3739
/// Additional interactions in the middle of the property
3840
queries: Vec<Query>,
3941
/// The select query
@@ -73,6 +75,7 @@ impl Property {
7375
match self {
7476
Property::InsertSelect {
7577
insert,
78+
row_index,
7679
queries,
7780
select,
7881
} => {
@@ -83,7 +86,7 @@ impl Property {
8386
);
8487

8588
// Pick a random row within the insert values
86-
let row = pick(&insert.values, &mut rand::thread_rng()).clone();
89+
let row = insert.values[*row_index].clone();
8790

8891
// Assume that the table exists
8992
let assumption = Interaction::Assumption(Assertion {
@@ -202,7 +205,8 @@ fn property_insert_select<R: rand::Rng>(
202205
.collect::<Vec<_>>();
203206

204207
// Pick a random row to select
205-
let row = pick(&rows, rng).clone();
208+
let row_index = pick_index(rows.len(), rng).clone();
209+
let row = rows[row_index].clone();
206210

207211
// Insert the rows
208212
let insert_query = Insert {
@@ -248,6 +252,7 @@ fn property_insert_select<R: rand::Rng>(
248252

249253
Property::InsertSelect {
250254
insert: insert_query,
255+
row_index,
251256
queries,
252257
select: select_query,
253258
}

simulator/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn main() -> Result<(), String> {
218218
last_execution,
219219
);
220220

221-
match (shrunk, &result) {
221+
match (&shrunk, &result) {
222222
(
223223
SandboxedResult::Panicked { error: e1, .. },
224224
SandboxedResult::Panicked { error: e2, .. },
@@ -227,7 +227,7 @@ fn main() -> Result<(), String> {
227227
SandboxedResult::FoundBug { error: e1, .. },
228228
SandboxedResult::FoundBug { error: e2, .. },
229229
) => {
230-
if &e1 != e2 {
230+
if e1 != e2 {
231231
log::error!(
232232
"shrinking failed, the error was not properly reproduced"
233233
);
@@ -291,6 +291,7 @@ fn revert_db_and_plan_files(output_dir: &Path) {
291291
std::fs::rename(&new_plan_path, &old_plan_path).unwrap();
292292
}
293293

294+
#[derive(Debug)]
294295
enum SandboxedResult {
295296
Panicked {
296297
error: String,

simulator/runner/execution.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::generation::{
99

1010
use super::env::{SimConnection, SimulatorEnv};
1111

12-
#[derive(Clone, Copy)]
12+
#[derive(Debug, Clone, Copy)]
1313
pub(crate) struct Execution {
1414
pub(crate) connection_index: usize,
1515
pub(crate) interaction_index: usize,
@@ -30,6 +30,7 @@ impl Execution {
3030
}
3131
}
3232

33+
#[derive(Debug)]
3334
pub(crate) struct ExecutionHistory {
3435
pub(crate) history: Vec<Execution>,
3536
}

0 commit comments

Comments
 (0)