From 98a22bd2a48b95179881bdbb0c51234cad545a57 Mon Sep 17 00:00:00 2001 From: Onur Tirtir Date: Fri, 3 Nov 2023 01:35:56 +0300 Subject: [PATCH] Fix flaky isolation_get_all_active_transactions.spec test --- .../isolation_get_all_active_transactions.out | 30 ++++++++++++++----- ...isolation_get_all_active_transactions.spec | 26 +++++++++++++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/test/regress/expected/isolation_get_all_active_transactions.out b/src/test/regress/expected/isolation_get_all_active_transactions.out index a9739a826..410f90f4c 100644 --- a/src/test/regress/expected/isolation_get_all_active_transactions.out +++ b/src/test/regress/expected/isolation_get_all_active_transactions.out @@ -94,7 +94,7 @@ step s2-commit: COMMIT; -starting permutation: s4-record-pid s3-show-activity s5-kill s3-show-activity +starting permutation: s4-record-pid s3-show-activity s5-kill s3-wait-backend-termination step s4-record-pid: SELECT pg_backend_pid() INTO selected_pid; @@ -109,18 +109,34 @@ count step s5-kill: SELECT pg_terminate_backend(pg_backend_pid) FROM selected_pid; + SELECT pg_sleep(1); -- sleep for a while to make sure that backend is terminated pg_terminate_backend --------------------------------------------------------------------- t (1 row) -step s3-show-activity: - SET ROLE postgres; - select count(*) from get_all_active_transactions() where process_id IN (SELECT * FROM selected_pid); - -count +pg_sleep --------------------------------------------------------------------- - 0 + (1 row) +step s3-wait-backend-termination: + SET ROLE postgres; + DO $$ + DECLARE + i int; + BEGIN + i := 0; + -- try for 5 sec then timeout + WHILE (select count(*) > 0 from get_all_active_transactions() where process_id IN (SELECT * FROM selected_pid)) + LOOP + PERFORM pg_sleep(0.1); + i := i + 1; + IF i > 50 THEN + RAISE EXCEPTION 'Timeout while waiting for backend to terminate'; + END IF; + END LOOP; + END; + $$; + diff --git a/src/test/regress/spec/isolation_get_all_active_transactions.spec b/src/test/regress/spec/isolation_get_all_active_transactions.spec index 497b3a58a..d9971d727 100644 --- a/src/test/regress/spec/isolation_get_all_active_transactions.spec +++ b/src/test/regress/spec/isolation_get_all_active_transactions.spec @@ -107,6 +107,29 @@ step "s3-show-activity" select count(*) from get_all_active_transactions() where process_id IN (SELECT * FROM selected_pid); } +step "s3-wait-backend-termination" +{ + SET ROLE postgres; + + DO $$ + DECLARE + i int; + BEGIN + i := 0; + + -- try for 5 sec then timeout + WHILE (select count(*) > 0 from get_all_active_transactions() where process_id IN (SELECT * FROM selected_pid)) + LOOP + PERFORM pg_sleep(0.1); + i := i + 1; + IF i > 50 THEN + RAISE EXCEPTION 'Timeout while waiting for backend to terminate'; + END IF; + END LOOP; + END; + $$; +} + session "s4" step "s4-record-pid" @@ -119,8 +142,9 @@ session "s5" step "s5-kill" { SELECT pg_terminate_backend(pg_backend_pid) FROM selected_pid; + SELECT pg_sleep(1); -- sleep for a while to make sure that backend is terminated } permutation "s1-grant" "s1-begin-insert" "s2-begin-insert" "s3-as-admin" "s3-as-user-1" "s3-as-readonly" "s3-as-monitor" "s1-commit" "s2-commit" -permutation "s4-record-pid" "s3-show-activity" "s5-kill" "s3-show-activity" +permutation "s4-record-pid" "s3-show-activity" "s5-kill" "s3-wait-backend-termination"