Skip to content

Commit ac6945e

Browse files
committed
primitves & sentry:
- primitives - improve tests & test-util modules - sentry - db - postgres Pool uses default `max_size`
1 parent 669e285 commit ac6945e

File tree

5 files changed

+54
-47
lines changed

5 files changed

+54
-47
lines changed

primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub mod sentry;
4646
pub mod spender;
4747
pub mod supermarket;
4848
pub mod targeting;
49-
#[cfg(feature = "test-util")]
49+
#[cfg(any(test, feature = "test-util"))]
5050
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
5151
pub mod test_util;
5252
pub mod unified_num;

primitives/src/sentry.rs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,15 +1129,55 @@ mod postgres {
11291129
}
11301130
}
11311131
}
1132+
1133+
#[cfg(test)]
1134+
mod test {
1135+
use chrono::{TimeZone, Utc};
1136+
1137+
use crate::{postgres::POSTGRES_POOL, sentry::DateHour};
1138+
1139+
#[tokio::test]
1140+
pub async fn datehour_from_to_sql() {
1141+
let client = POSTGRES_POOL.get().await.unwrap();
1142+
let sql_type = "TIMESTAMPTZ";
1143+
1144+
let example_datehour = DateHour::<Utc>::from_ymdh(2021, 1, 1, 1);
1145+
let expected_datehour = DateHour::try_from(Utc.ymd(2021, 1, 1).and_hms(1, 0, 0))
1146+
.expect("Should get DateHour");
1147+
assert_eq!(
1148+
example_datehour, expected_datehour,
1149+
"Example and expected datehour must be the same"
1150+
);
1151+
1152+
// from SQL
1153+
let actual_datehour: DateHour<Utc> = client
1154+
.query_one(
1155+
&*format!("SELECT '{}'::{}", example_datehour.to_datetime(), sql_type),
1156+
&[],
1157+
)
1158+
.await
1159+
.unwrap()
1160+
.get(0);
1161+
1162+
assert_eq!(&expected_datehour, &actual_datehour);
1163+
1164+
// to SQL
1165+
let actual_datehour: DateHour<Utc> = client
1166+
.query_one(&*format!("SELECT $1::{}", sql_type), &[&example_datehour])
1167+
.await
1168+
.unwrap()
1169+
.get(0);
1170+
1171+
assert_eq!(&expected_datehour, &actual_datehour);
1172+
}
1173+
}
11321174
}
11331175

11341176
#[cfg(test)]
11351177
mod test {
11361178
use super::*;
1137-
use crate::{
1138-
postgres::POSTGRES_POOL,
1139-
test_util::{DUMMY_IPFS, PUBLISHER},
1140-
};
1179+
use crate::test_util::{DUMMY_IPFS, PUBLISHER};
1180+
11411181
use serde_json::{json, Value};
11421182

11431183
#[test]
@@ -1243,39 +1283,4 @@ mod test {
12431283
"UTC+0 value should be equal to UTC+2"
12441284
);
12451285
}
1246-
1247-
#[tokio::test]
1248-
pub async fn datehour_from_to_sql() {
1249-
let client = POSTGRES_POOL.get().await.unwrap();
1250-
let sql_type = "TIMESTAMPTZ";
1251-
1252-
let example_datehour = DateHour::<Utc>::from_ymdh(2021, 1, 1, 1);
1253-
let expected_datehour =
1254-
DateHour::try_from(Utc.ymd(2021, 1, 1).and_hms(1, 0, 0)).expect("Should get DateHour");
1255-
assert_eq!(
1256-
example_datehour, expected_datehour,
1257-
"Example and expected datehour must be the same"
1258-
);
1259-
1260-
// from SQL
1261-
let actual_datehour: DateHour<Utc> = client
1262-
.query_one(
1263-
&*format!("SELECT '{}'::{}", example_datehour.to_datetime(), sql_type),
1264-
&[],
1265-
)
1266-
.await
1267-
.unwrap()
1268-
.get(0);
1269-
1270-
assert_eq!(&expected_datehour, &actual_datehour);
1271-
1272-
// to SQL
1273-
let actual_datehour: DateHour<Utc> = client
1274-
.query_one(&*format!("SELECT $1::{}", sql_type), &[&example_datehour])
1275-
.await
1276-
.unwrap()
1277-
.get(0);
1278-
1279-
assert_eq!(&expected_datehour, &actual_datehour);
1280-
}
12811286
}

sentry/src/db.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ pub async fn redis_connection(
5353
client.get_multiplexed_async_connection().await
5454
}
5555

56+
/// Uses the default `max_size` of the `PoolConfig` which is `num_cpus::get_physical() * 4`
5657
pub async fn postgres_connection(
57-
max_size: usize,
5858
config: tokio_postgres::Config,
5959
) -> Result<DbPool, deadpool_postgres::BuildError> {
6060
let mgr_config = ManagerConfig {
@@ -63,7 +63,9 @@ pub async fn postgres_connection(
6363

6464
let manager = Manager::from_config(config, NoTls, mgr_config);
6565

66-
DbPool::builder(manager).max_size(max_size).build()
66+
// use default max_size which is set by PoolConfig::default()
67+
// num_cpus::get_physical() * 4
68+
DbPool::builder(manager).build()
6769
}
6870

6971
/// Sets the migrations using the `POSTGRES_*` environment variables
@@ -138,7 +140,7 @@ pub fn setup_migrations(environment: Environment) {
138140
.expect("Reloading config for migration failed");
139141
}
140142

141-
#[cfg(feature = "test-util")]
143+
#[cfg(any(test, feature = "test-util"))]
142144
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
143145
pub mod tests_postgres {
144146
use std::{
@@ -230,7 +232,7 @@ pub mod tests_postgres {
230232
#[derive(Debug, Error)]
231233
pub enum Error {
232234
#[error(transparent)]
233-
Build(#[from] deadpool::managed::BuildError<tokio_postgres::Error>),
235+
Build(#[from] deadpool_postgres::BuildError),
234236
#[error(transparent)]
235237
Pool(#[from] PoolError),
236238
}
@@ -480,7 +482,7 @@ pub mod tests_postgres {
480482
}
481483
}
482484

483-
#[cfg(feature = "test-util")]
485+
#[cfg(any(test, feature = "test-util"))]
484486
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
485487
pub mod redis_pool {
486488

sentry/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
134134
});
135135

136136
// use the environmental variables to setup the Postgres connection
137-
let postgres = match postgres_connection(420, POSTGRES_CONFIG.clone()).await {
137+
let postgres = match postgres_connection(POSTGRES_CONFIG.clone()).await {
138138
Ok(pool) => pool,
139139
Err(build_err) => panic!("Failed to build postgres database pool: {build_err}"),
140140
};

test_harness/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2800,7 +2800,7 @@ pub mod run {
28002800
config
28012801
};
28022802

2803-
let postgres = postgres_connection(42, postgres_config).await?;
2803+
let postgres = postgres_connection(postgres_config).await?;
28042804
let mut redis = redis_connection(validator.sentry_config.redis_url.clone()).await?;
28052805

28062806
Manager::flush_db(&mut redis)

0 commit comments

Comments
 (0)