Skip to content
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

tests: Add retry to first database connection (release/0.6 backport) #2552

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions aggregator_core/src/datastore/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
datastore::{Crypter, Datastore, Transaction},
test_util::noop_meter,
};
use backoff::{future::retry, ExponentialBackoffBuilder};
use chrono::NaiveDateTime;
use deadpool_postgres::{Manager, Pool};
use janus_core::{
Expand All @@ -20,6 +21,7 @@ use std::{
str::FromStr,
sync::{Arc, Barrier, Weak},
thread::{self, JoinHandle},
time::Duration,
};
use testcontainers::RunnableImage;
use tokio::sync::{oneshot, Mutex};
Expand Down Expand Up @@ -178,9 +180,28 @@ pub async fn ephemeral_datastore_schema_version(schema_version: i64) -> Ephemera
trace!("Creating ephemeral postgres datastore {db_name}");

// Create Postgres DB.
let (client, conn) = connect(&db.connection_string("postgres"), NoTls)
.await
.unwrap();
//
// Since this is the first connection we're establishing since the container has been created,
// retry this a few times. The database may not be ready yet.
let backoff = ExponentialBackoffBuilder::new()
.with_initial_interval(Duration::from_millis(500))
.with_max_interval(Duration::from_millis(500))
.with_max_elapsed_time(Some(Duration::from_secs(5)))
.build();
let (client, conn) = retry(backoff, || {
let connection_string = db.connection_string("postgres");
async move {
connect(&connection_string, NoTls)
.await
.map_err(|err| backoff::Error::Transient {
err,
retry_after: None,
})
}
})
.await
.unwrap();

tokio::spawn(async move { conn.await.unwrap() }); // automatically stops after Client is dropped
client
.batch_execute(&format!("CREATE DATABASE {db_name}"))
Expand Down
Loading