Skip to content

Commit

Permalink
Avoid deadlock with concurrent test
Browse files Browse the repository at this point in the history
The sql_query commands on lines 334 and 372 can be executes in parallel during a test and create a failure. With locking the users table this is avoided.
  • Loading branch information
peter-scholtens authored Nov 21, 2023
1 parent 4a18cb3 commit e71332c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions diesel_tests/tests/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ fn upsert_with_sql_literal_for_target() {
let connection = &mut connection();
// This index needs to happen before the insert or we'll get a deadlock
// with any transactions that are trying to get the row lock from insert
diesel::sql_query("CREATE UNIQUE INDEX ON users (name) WHERE name != 'Tess'")
// As tests can run concurrently, lock the table of interest.
diesel::sql_query("BEGIN; LOCK TABLE users; CREATE UNIQUE INDEX ON users (name) WHERE name != 'Tess'; COMMIT;")
.execute(connection)
.unwrap();
insert_sean_and_tess_into_users_table(connection);
Expand Down Expand Up @@ -369,7 +370,8 @@ fn upsert_with_sql_literal_for_target_with_condition() {
let connection = &mut connection();
// This index needs to happen before the insert or we'll get a deadlock
// with any transactions that are trying to get the row lock from insert
diesel::sql_query("CREATE UNIQUE INDEX ON users (name) WHERE name != 'Tess'")
// As tests can run concurrently, lock the table of interest.
diesel::sql_query("BEGIN; LOCK TABLE users; CREATE UNIQUE INDEX ON users (name) WHERE name != 'Tess'; COMMIT;")
.execute(connection)
.unwrap();
insert_sean_and_tess_into_users_table(connection);
Expand Down

0 comments on commit e71332c

Please sign in to comment.