Skip to content

Commit 372732a

Browse files
committed
store: Asyncify Layout.find
1 parent 478ba0d commit 372732a

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

store/postgres/src/deployment_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ impl DeploymentStore {
10741074
) -> Result<Option<Entity>, StoreError> {
10751075
let mut conn = self.get_conn().await?;
10761076
let layout = self.layout(&mut conn, site).await?;
1077-
layout.find(&mut conn, key, block)
1077+
layout.find(&mut conn, key, block).await
10781078
}
10791079

10801080
/// Retrieve all the entities matching `ids_for_type`, both the type and causality region, from

store/postgres/src/relational.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl Layout {
458458
.ok_or_else(|| StoreError::UnknownTable(entity.to_string()))
459459
}
460460

461-
pub fn find(
461+
pub async fn find(
462462
&self,
463463
conn: &mut PgConnection,
464464
key: &EntityKey,

store/test-store/tests/postgres/relational.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ async fn find() {
570570
&SCALAR_TYPE.parse_key("one").unwrap(),
571571
BLOCK_NUMBER_MAX,
572572
)
573+
.await
573574
.expect("Failed to read Scalar[one]")
574575
.unwrap();
575576
assert_entity_eq!(scrub(&SCALAR_ENTITY), entity);
@@ -581,6 +582,7 @@ async fn find() {
581582
&SCALAR_TYPE.parse_key("noone").unwrap(),
582583
BLOCK_NUMBER_MAX,
583584
)
585+
.await
584586
.expect("Failed to read Scalar[noone]");
585587
assert!(entity.is_none());
586588
})
@@ -604,6 +606,7 @@ async fn insert_null_fulltext_fields() {
604606
&NULLABLE_STRINGS_TYPE.parse_key("one").unwrap(),
605607
BLOCK_NUMBER_MAX,
606608
)
609+
.await
607610
.expect("Failed to read NullableStrings[one]")
608611
.unwrap();
609612
assert_entity_eq!(scrub(&EMPTY_NULLABLESTRINGS_ENTITY), entity);
@@ -637,6 +640,7 @@ async fn update() {
637640
&SCALAR_TYPE.parse_key("one").unwrap(),
638641
BLOCK_NUMBER_MAX,
639642
)
643+
.await
640644
.expect("Failed to read Scalar[one]")
641645
.unwrap();
642646
assert_entity_eq!(scrub(&entity), actual);
@@ -694,15 +698,17 @@ async fn update_many() {
694698
.expect("Failed to update");
695699

696700
// check updates took effect
697-
let updated: Vec<Entity> = ["one", "two", "three"]
698-
.iter()
699-
.map(|&id| {
700-
layout
701-
.find(conn, &SCALAR_TYPE.parse_key(id).unwrap(), BLOCK_NUMBER_MAX)
702-
.unwrap_or_else(|_| panic!("Failed to read Scalar[{}]", id))
703-
.unwrap()
704-
})
705-
.collect();
701+
let mut updated: Vec<Entity> = Vec::new();
702+
for id in &["one", "two", "three"] {
703+
let entity = layout
704+
.find(conn, &SCALAR_TYPE.parse_key(*id).unwrap(), BLOCK_NUMBER_MAX)
705+
.await
706+
.unwrap_or_else(|_| panic!("Failed to read Scalar[{}]", id))
707+
.unwrap();
708+
updated.push(entity);
709+
}
710+
let updated = updated;
711+
706712
let new_one = &updated[0];
707713
let new_two = &updated[1];
708714
let new_three = &updated[2];
@@ -768,6 +774,7 @@ async fn serialize_bigdecimal() {
768774
&SCALAR_TYPE.parse_key("one").unwrap(),
769775
BLOCK_NUMBER_MAX,
770776
)
777+
.await
771778
.expect("Failed to read Scalar[one]")
772779
.unwrap();
773780
assert_entity_eq!(entity, actual);
@@ -806,6 +813,7 @@ async fn enum_arrays() {
806813
.unwrap(),
807814
BLOCK_NUMBER_MAX,
808815
)
816+
.await
809817
.expect("Failed to read Spectrum[rainbow]")
810818
.unwrap();
811819
assert_entity_eq!(spectrum, actual);
@@ -1010,7 +1018,7 @@ async fn conflicting_entity() {
10101018

10111019
#[tokio::test]
10121020
async fn revert_block() {
1013-
fn check_fred(conn: &mut PgConnection, layout: &Layout) {
1021+
async fn check_fred(conn: &mut PgConnection, layout: &Layout) {
10141022
let id = "fred";
10151023

10161024
let set_fred = |conn: &mut PgConnection, name, block| {
@@ -1026,9 +1034,10 @@ async fn revert_block() {
10261034
}
10271035
};
10281036

1029-
let assert_fred = |conn: &mut PgConnection, name: &str| {
1037+
let assert_fred = async |conn: &mut PgConnection, name: &str| {
10301038
let fred = layout
10311039
.find(conn, &CAT_TYPE.parse_key(id).unwrap(), BLOCK_NUMBER_MAX)
1040+
.await
10321041
.unwrap()
10331042
.expect("there's a fred");
10341043
assert_eq!(name, fred.get("name").unwrap().as_str().unwrap())
@@ -1040,14 +1049,14 @@ async fn revert_block() {
10401049
set_fred(conn, "three", 3);
10411050

10421051
layout.revert_block(conn, 3).unwrap();
1043-
assert_fred(conn, "two");
1052+
assert_fred(conn, "two").await;
10441053
layout.revert_block(conn, 2).unwrap();
1045-
assert_fred(conn, "one");
1054+
assert_fred(conn, "one").await;
10461055

10471056
set_fred(conn, "three", 3);
1048-
assert_fred(conn, "three");
1057+
assert_fred(conn, "three").await;
10491058
layout.revert_block(conn, 3).unwrap();
1050-
assert_fred(conn, "one");
1059+
assert_fred(conn, "one").await;
10511060
}
10521061

10531062
async fn check_marty(conn: &mut PgConnection, layout: &Layout) {
@@ -1113,7 +1122,7 @@ async fn revert_block() {
11131122
}
11141123

11151124
run_test(async |conn, layout| {
1116-
check_fred(conn, layout);
1125+
check_fred(conn, layout).await;
11171126
check_marty(conn, layout).await;
11181127
})
11191128
.await;

store/test-store/tests/postgres/relational_bytes.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,39 +210,39 @@ where
210210
#[tokio::test]
211211
async fn bad_id() {
212212
run_test(async |conn, layout| {
213-
fn find(
213+
async fn find(
214214
conn: &mut PgConnection,
215215
layout: &Layout,
216216
id: &str,
217217
) -> Result<Option<Entity>, StoreError> {
218218
let key = THING_TYPE.parse_key(id)?;
219-
layout.find(conn, &key, BLOCK_NUMBER_MAX)
219+
layout.find(conn, &key, BLOCK_NUMBER_MAX).await
220220
}
221221

222222
// We test that we get errors for various strings that are not
223223
// valid 'Bytes' strings; we use `find` to force the conversion
224224
// from String -> Bytes internally
225-
let res = find(conn, layout, "bad");
225+
let res = find(conn, layout, "bad").await;
226226
assert!(res.is_err());
227227
assert_eq!(
228228
"store error: can not convert `bad` to Id::Bytes: Odd number of digits",
229229
res.err().unwrap().to_string()
230230
);
231231

232232
// We do not allow the `\x` prefix that Postgres uses
233-
let res = find(conn, layout, "\\xbadd");
233+
let res = find(conn, layout, "\\xbadd").await;
234234
assert!(res.is_err());
235235
assert_eq!(
236236
"store error: can not convert `\\xbadd` to Id::Bytes: Invalid character '\\\\' at position 0",
237237
res.err().unwrap().to_string()
238238
);
239239

240240
// Having the '0x' prefix is ok
241-
let res = find(conn, layout, "0xbadd");
241+
let res = find(conn, layout, "0xbadd").await;
242242
assert!(res.is_ok());
243243

244244
// Using non-hex characters is also bad
245-
let res = find(conn, layout, "nope");
245+
let res = find(conn, layout, "nope").await;
246246
assert!(res.is_err());
247247
assert_eq!(
248248
"store error: can not convert `nope` to Id::Bytes: Invalid character 'n' at position 0",
@@ -254,10 +254,11 @@ async fn bad_id() {
254254
#[tokio::test]
255255
async fn find() {
256256
run_test(async |mut conn, layout| {
257-
fn find_entity(conn: &mut PgConnection, layout: &Layout, id: &str) -> Option<Entity> {
257+
async fn find_entity(conn: &mut PgConnection, layout: &Layout, id: &str) -> Option<Entity> {
258258
let key = THING_TYPE.parse_key(id).unwrap();
259259
layout
260260
.find(conn, &key, BLOCK_NUMBER_MAX)
261+
.await
261262
.expect(&format!("Failed to read Thing[{}]", id))
262263
}
263264

@@ -266,12 +267,12 @@ async fn find() {
266267
insert_thing(&mut conn, layout, ID, NAME, 0);
267268

268269
// Happy path: find existing entity
269-
let entity = find_entity(conn, layout, ID).unwrap();
270+
let entity = find_entity(conn, layout, ID).await.unwrap();
270271
assert_entity_eq!(BEEF_ENTITY.clone(), entity);
271272
assert!(CausalityRegion::from_entity(&entity) == CausalityRegion::ONCHAIN);
272273

273274
// Find non-existing entity
274-
let entity = find_entity(conn, layout, "badd");
275+
let entity = find_entity(conn, layout, "badd").await;
275276
assert!(entity.is_none());
276277
})
277278
.await;
@@ -331,6 +332,7 @@ async fn update() {
331332

332333
let actual = layout
333334
.find(conn, &THING_TYPE.key(entity_id), BLOCK_NUMBER_MAX)
335+
.await
334336
.expect("Failed to read Thing[deadbeef]")
335337
.unwrap();
336338

0 commit comments

Comments
 (0)