-
Notifications
You must be signed in to change notification settings - Fork 102
State::IdleInTransaction isn't updated until the end of a request #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
integration/rust/tests/integration/transaction_state.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| use rust::setup::admin_sqlx; | ||
| use serial_test::serial; | ||
| use sqlx::{Executor, Pool, Postgres, Row}; | ||
| use tokio::time::{Duration, Instant, sleep}; | ||
|
|
||
| const APP_NAME: &str = "test_transaction_state_flow"; | ||
|
|
||
| #[tokio::test] | ||
| #[serial] | ||
| async fn test_transaction_state_transitions() { | ||
| let admin = admin_sqlx().await; | ||
| assert!(fetch_client_state(&admin, APP_NAME).await.is_none()); | ||
|
|
||
| let (client, connection) = tokio_postgres::connect( | ||
| "host=127.0.0.1 user=pgdog dbname=pgdog password=pgdog port=6432", | ||
| tokio_postgres::NoTls, | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let connection_handle = tokio::spawn(async move { | ||
| if let Err(e) = connection.await { | ||
| panic!("connection error: {}", e); | ||
| } | ||
| }); | ||
|
|
||
| client | ||
| .batch_execute(&format!("SET application_name TO '{}';", APP_NAME)) | ||
| .await | ||
| .unwrap(); | ||
| client | ||
| .batch_execute("SET statement_timeout TO '10s';") | ||
| .await | ||
| .unwrap(); | ||
| client.batch_execute("SELECT 1;").await.unwrap(); | ||
|
|
||
| wait_for_client_state(&admin, APP_NAME, "idle").await; | ||
|
|
||
| client.batch_execute("BEGIN;").await.unwrap(); | ||
| wait_for_client_state(&admin, APP_NAME, "idle in transaction").await; | ||
|
|
||
| { | ||
| let query = client.simple_query("SELECT pg_sleep(0.25);"); | ||
| tokio::pin!(query); | ||
|
|
||
| let deadline = Instant::now() + Duration::from_secs(5); | ||
| let mut saw_active = false; | ||
| loop { | ||
| tokio::select! { | ||
| result = &mut query => { | ||
| result.unwrap(); | ||
| break; | ||
| } | ||
| _ = sleep(Duration::from_millis(10)) => { | ||
| if let Some(state) = fetch_client_state(&admin, APP_NAME).await { | ||
| if state == "active" { | ||
| saw_active = true; | ||
| } | ||
| } | ||
|
|
||
| if Instant::now() >= deadline { | ||
| panic!("timed out waiting for client to become active"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| assert!( | ||
| saw_active, | ||
| "client never reported active state during query" | ||
| ); | ||
| } | ||
|
|
||
| wait_for_client_state(&admin, APP_NAME, "idle in transaction").await; | ||
|
|
||
| client.batch_execute("COMMIT;").await.unwrap(); | ||
| wait_for_client_state(&admin, APP_NAME, "idle").await; | ||
|
|
||
| drop(client); | ||
|
|
||
| wait_for_no_client(&admin, APP_NAME).await; | ||
|
|
||
| admin.close().await; | ||
| connection_handle.await.unwrap(); | ||
| } | ||
|
|
||
| async fn fetch_client_state(admin: &Pool<Postgres>, application_name: &str) -> Option<String> { | ||
| let rows = admin.fetch_all("SHOW CLIENTS").await.unwrap(); | ||
| for row in rows { | ||
| let db: String = row.get::<String, _>("database"); | ||
| let app: String = row.get::<String, _>("application_name"); | ||
| if db == "pgdog" && app == application_name { | ||
| return Some(row.get::<String, _>("state")); | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| async fn wait_for_client_state(admin: &Pool<Postgres>, application_name: &str, expected: &str) { | ||
| let deadline = Instant::now() + Duration::from_secs(5); | ||
| loop { | ||
| if let Some(state) = fetch_client_state(admin, application_name).await { | ||
| if state == expected { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if Instant::now() >= deadline { | ||
| panic!( | ||
| "timed out waiting for client state '{}' (expected '{}')", | ||
| fetch_client_state(admin, application_name) | ||
| .await | ||
| .unwrap_or_else(|| "<none>".to_string()), | ||
| expected | ||
| ); | ||
| } | ||
|
|
||
| sleep(Duration::from_millis(25)).await; | ||
| } | ||
| } | ||
|
|
||
| async fn wait_for_no_client(admin: &Pool<Postgres>, application_name: &str) { | ||
| let deadline = Instant::now() + Duration::from_secs(5); | ||
| loop { | ||
| if fetch_client_state(admin, application_name).await.is_none() { | ||
| return; | ||
| } | ||
|
|
||
| if Instant::now() >= deadline { | ||
| panic!("client '{}' still present", application_name); | ||
| } | ||
|
|
||
| sleep(Duration::from_millis(25)).await; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean next if not?