Skip to content

Commit d90f0d9

Browse files
committed
editoast: async use deadpool instead of bb8
1 parent 42f60e2 commit d90f0d9

24 files changed

+145
-198
lines changed

editoast/Cargo.lock

+26-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editoast/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ diesel = { version = "2.1", features = [
2222
"chrono",
2323
"uuid",
2424
] }
25-
diesel-async = { version = "0.3.1", features = ["postgres", "bb8"] }
25+
diesel-async = { version = "0.3.1", features = ["postgres", "deadpool"] }
2626
diesel_json = "0.2.1"
2727
image = "0.24"
2828
json-patch = "1.0"

editoast/src/client/postgres_config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct PostgresConfig {
2121
pub psql_port: u16,
2222
#[derivative(Default(value = "32"))]
2323
#[arg(long, env, default_value_t = 32)]
24-
pub pool_size: u32,
24+
pub pool_size: usize,
2525
}
2626

2727
impl PostgresConfig {

editoast/src/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ impl EditoastError for JsonPayloadError {
118118
}
119119
}
120120

121-
/// Handle bb8 errors
122-
impl EditoastError for diesel_async::pooled_connection::bb8::RunError {
121+
/// Handle database pool errors
122+
impl EditoastError for diesel_async::pooled_connection::deadpool::PoolError {
123123
fn get_status(&self) -> StatusCode {
124124
StatusCode::INTERNAL_SERVER_ERROR
125125
}
126126

127127
fn get_type(&self) -> &'static str {
128-
"editoast:BB8Error"
128+
"editoast:DatabePoolError"
129129
}
130130
}
131131

editoast/src/fixtures.rs

+39-46
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ pub mod tests {
2020
use postgis_diesel::types::LineString;
2121
use rstest::*;
2222
use serde_json::json;
23+
use std::fmt;
2324

24-
#[derive(Debug)]
2525
pub struct TestFixture<T: Delete + Identifiable + Send> {
2626
pub model: T,
2727
pub db_pool: Data<DbPool>,
2828
pub infra: Option<Infra>,
2929
}
3030

31+
impl<T: fmt::Debug + Delete + Identifiable + Send> fmt::Debug for TestFixture<T> {
32+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33+
write!(f, "Fixture {:?} {:?}", self.model, self.infra)
34+
}
35+
}
36+
3137
impl<T: Delete + Identifiable + Send> TestFixture<T> {
3238
pub fn id(&self) -> i64 {
3339
self.model.get_id()
@@ -62,52 +68,47 @@ pub mod tests {
6268
}
6369

6470
#[fixture]
65-
pub async fn db_pool() -> Data<DbPool> {
71+
pub fn db_pool() -> Data<DbPool> {
6672
let url = PostgresConfig::default().url();
6773
let config = AsyncDieselConnectionManager::<diesel_async::AsyncPgConnection>::new(url);
68-
let pool = DbPool::builder().build(config).await.unwrap();
74+
let pool = DbPool::builder(config).build().unwrap();
6975
Data::new(pool)
7076
}
7177

7278
#[fixture]
73-
pub async fn fast_rolling_stock(
74-
#[future] db_pool: Data<DbPool>,
75-
) -> TestFixture<RollingStockModel> {
79+
pub async fn fast_rolling_stock(db_pool: Data<DbPool>) -> TestFixture<RollingStockModel> {
7680
TestFixture::create(
7781
serde_json::from_str::<RollingStockModel>(include_str!(
7882
"./tests/example_rolling_stock_1.json"
7983
))
8084
.expect("Unable to parse"),
81-
db_pool.await,
85+
db_pool,
8286
)
8387
.await
8488
}
8589

8690
#[fixture]
87-
pub async fn other_rolling_stock(
88-
#[future] db_pool: Data<DbPool>,
89-
) -> TestFixture<RollingStockModel> {
91+
pub async fn other_rolling_stock(db_pool: Data<DbPool>) -> TestFixture<RollingStockModel> {
9092
TestFixture::create(
9193
serde_json::from_str::<RollingStockModel>(include_str!(
9294
"./tests/example_rolling_stock_2_energy_sources.json"
9395
))
9496
.expect("Unable to parse"),
95-
db_pool.await,
97+
db_pool,
9698
)
9799
.await
98100
}
99101

100102
#[fixture]
101103
pub async fn train_schedule(
102-
#[future] db_pool: Data<DbPool>,
104+
db_pool: Data<DbPool>,
103105
#[future] pathfinding: TestFixture<Pathfinding>,
104106
#[future] timetable: TestFixture<Timetable>,
105107
#[future] fast_rolling_stock: TestFixture<RollingStockModel>,
106108
) -> TestFixture<TrainSchedule> {
107109
let pathfinding = pathfinding.await;
108110
let timetable = timetable.await;
109111
let rolling_stock = fast_rolling_stock.await;
110-
let db_pool = db_pool.await;
111112
let train_schedule = make_train_schedule(
112113
db_pool.clone(),
113114
pathfinding.id(),
@@ -159,14 +160,13 @@ pub mod tests {
159160
let pathfinding = pathfinding(db_pool()).await;
160161
let rolling_stock = fast_rolling_stock(db_pool()).await;
161162
let ts_model = make_train_schedule(
162-
db_pool().await,
163+
db_pool(),
163164
pathfinding.id(),
164165
timetable.id(),
165166
rolling_stock.id(),
166167
)
167168
.await;
168-
let train_schedule: TestFixture<TrainSchedule> =
169-
TestFixture::new(ts_model, db_pool().await);
169+
let train_schedule: TestFixture<TrainSchedule> = TestFixture::new(ts_model, db_pool());
170170
TrainScheduleFixtureSet {
171171
train_schedule,
172172
project,
@@ -201,7 +201,7 @@ pub mod tests {
201201
creation_date: Some(Utc::now().naive_utc()),
202202
..Scenario::default()
203203
};
204-
let scenario = TestFixture::create(scenario_model, db_pool().await).await;
204+
let scenario = TestFixture::create(scenario_model, db_pool()).await;
205205
ScenarioFixtureSet {
206206
project,
207207
study,
@@ -218,7 +218,7 @@ pub mod tests {
218218

219219
#[fixture]
220220
pub async fn study_fixture_set(
221-
#[future] db_pool: Data<DbPool>,
221+
db_pool: Data<DbPool>,
222222
#[future] project: TestFixture<Project>,
223223
) -> StudyFixtureSet {
224224
let project = project.await;
@@ -237,12 +237,12 @@ pub mod tests {
237237
};
238238
StudyFixtureSet {
239239
project,
240-
study: TestFixture::create(study_model, db_pool.await).await,
240+
study: TestFixture::create(study_model, db_pool).await,
241241
}
242242
}
243243

244244
#[fixture]
245-
pub async fn project(#[future] db_pool: Data<DbPool>) -> TestFixture<Project> {
245+
pub async fn project(db_pool: Data<DbPool>) -> TestFixture<Project> {
246246
let project_model = Project {
247247
name: Some("test_project".into()),
248248
objectives: Some("".into()),
@@ -253,20 +253,20 @@ pub mod tests {
253253
creation_date: Some(Utc::now().naive_utc()),
254254
..Default::default()
255255
};
256-
TestFixture::create(project_model, db_pool.await).await
256+
TestFixture::create(project_model, db_pool).await
257257
}
258258

259259
#[fixture]
260-
pub async fn timetable(#[future] db_pool: Data<DbPool>) -> TestFixture<Timetable> {
260+
pub async fn timetable(db_pool: Data<DbPool>) -> TestFixture<Timetable> {
261261
let timetable_model = Timetable {
262262
id: None,
263263
name: Some(String::from("with_electrical_profiles")),
264264
};
265-
TestFixture::create(timetable_model, db_pool.await).await
265+
TestFixture::create(timetable_model, db_pool).await
266266
}
267267

268268
#[fixture]
269-
pub async fn document_example(#[future] db_pool: Data<DbPool>) -> TestFixture<Document> {
269+
pub async fn document_example(db_pool: Data<DbPool>) -> TestFixture<Document> {
270270
let img = image::open("src/tests/example_rolling_stock_image_1.gif").unwrap();
271271
let mut img_bytes: Vec<u8> = Vec::new();
272272
assert!(img
@@ -275,16 +275,12 @@ pub mod tests {
275275
image::ImageOutputFormat::Png
276276
)
277277
.is_ok());
278-
TestFixture::create(
279-
Document::new(String::from("image/png"), img_bytes),
280-
db_pool.await,
281-
)
282-
.await
278+
TestFixture::create(Document::new(String::from("image/png"), img_bytes), db_pool).await
283279
}
284280

285281
#[fixture]
286282
pub async fn rolling_stock_livery(
287-
#[future] db_pool: Data<DbPool>,
283+
db_pool: Data<DbPool>,
288284
#[future] fast_rolling_stock: TestFixture<RollingStockModel>,
289285
#[future] document_example: TestFixture<Document>,
290286
) -> TestFixture<RollingStockLiveryModel> {
@@ -296,29 +292,29 @@ pub mod tests {
296292
rolling_stock_id: Some(rolling_stock.id()),
297293
compound_image_id: Some(Some(image.id())),
298294
};
299-
TestFixture::create(rolling_stock_livery, db_pool.await).await
295+
TestFixture::create(rolling_stock_livery, db_pool).await
300296
}
301297

302298
#[fixture]
303299
pub async fn electrical_profile_set(
304-
#[future] db_pool: Data<DbPool>,
300+
db_pool: Data<DbPool>,
305301
) -> TestFixture<ElectricalProfileSet> {
306302
TestFixture::create(
307303
serde_json::from_str::<ElectricalProfileSet>(include_str!(
308304
"./tests/electrical_profile_set.json"
309305
))
310306
.expect("Unable to parse"),
311-
db_pool.await,
307+
db_pool,
312308
)
313309
.await
314310
}
315311

316312
#[fixture]
317-
pub async fn empty_infra(#[future] db_pool: Data<DbPool>) -> TestFixture<Infra> {
313+
pub async fn empty_infra(db_pool: Data<DbPool>) -> TestFixture<Infra> {
318314
let infra_form = InfraForm {
319315
name: String::from("test_infra"),
320316
};
321-
TestFixture::create(Infra::from(infra_form), db_pool.await).await
317+
TestFixture::create(Infra::from(infra_form), db_pool).await
322318
}
323319

324320
async fn make_small_infra(db_pool: Data<DbPool>) -> Infra {
@@ -341,14 +337,12 @@ pub mod tests {
341337
/// is made, the `editoast/tests/small_infra/small_infra.json` file should be updated
342338
/// to the latest infra description.
343339
#[fixture]
344-
pub async fn small_infra(#[future] db_pool: Data<DbPool>) -> TestFixture<Infra> {
345-
let db_pool = db_pool.await;
340+
pub async fn small_infra(db_pool: Data<DbPool>) -> TestFixture<Infra> {
346341
TestFixture::new(make_small_infra(db_pool.clone()).await, db_pool)
347342
}
348343

349344
#[fixture]
350-
pub async fn pathfinding(#[future] db_pool: Data<DbPool>) -> TestFixture<Pathfinding> {
351-
let db_pool = db_pool.await;
345+
pub async fn pathfinding(db_pool: Data<DbPool>) -> TestFixture<Pathfinding> {
352346
let small_infra = make_small_infra(db_pool.clone()).await;
353347
let pf_cs = PathfindingChangeset {
354348
infra_id: small_infra.id,
@@ -400,11 +394,10 @@ pub mod tests {
400394
timetable,
401395
infra,
402396
} = scenario_fixture_set().await;
403-
let pathfinding = pathfinding.await;
404-
let rolling_stock = fast_rolling_stock.await;
405-
let db_pool = db_pool.await;
397+
let rolling_stock = fast_rolling_stock(db_pool()).await;
398+
let pathfinding = pathfinding(db_pool()).await;
406399
let train_schedule = make_train_schedule(
407-
db_pool.clone(),
400+
db_pool().clone(),
408401
pathfinding.id(),
409402
timetable.id(),
410403
rolling_stock.id(),
@@ -443,13 +436,13 @@ pub mod tests {
443436
..Default::default()
444437
};
445438
let simulation_output: SimulationOutput = simulation_output
446-
.create(db_pool.clone())
439+
.create(db_pool().clone())
447440
.await
448441
.unwrap()
449442
.into();
450443

451-
let train_schedule = TestFixture::new(train_schedule, db_pool.clone());
452-
let simulation_output = TestFixture::new(simulation_output, db_pool);
444+
let train_schedule = TestFixture::new(train_schedule, db_pool().clone());
445+
let simulation_output = TestFixture::new(simulation_output, db_pool());
453446
TrainScheduleWithSimulationOutputFixtureSet {
454447
project,
455448
study,

0 commit comments

Comments
 (0)