Skip to content

Commit 2891a76

Browse files
committed
store: Test WritableStore::get, get_many, and get_derived more thoroughly
1 parent 51de464 commit 2891a76

File tree

2 files changed

+106
-23
lines changed

2 files changed

+106
-23
lines changed

store/postgres/src/writable.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,16 @@ pub(crate) mod test_support {
732732
queue.push(()).await
733733
}
734734
}
735+
736+
pub async fn flush_steps(deployment: graph::components::store::DeploymentId) {
737+
let queue = {
738+
let mut map = STEPS.lock().unwrap();
739+
map.remove(&deployment)
740+
};
741+
if let Some(queue) = queue {
742+
queue.push(()).await;
743+
}
744+
}
735745
}
736746

737747
impl std::fmt::Debug for Queue {
@@ -994,6 +1004,10 @@ impl Queue {
9941004
/// Wait for the background writer to finish processing queued entries
9951005
async fn flush(&self) -> Result<(), StoreError> {
9961006
self.check_err()?;
1007+
1008+
#[cfg(debug_assertions)]
1009+
test_support::flush_steps(self.store.site.id.into()).await;
1010+
9971011
// Turn off batching so the queue doesn't wait for a batch to become
9981012
// full, but restore the old behavior once the queue is empty.
9991013
let batching = self.batch_writes.load(Ordering::SeqCst);

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

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use graph::blockchain::block_stream::FirehoseCursor;
22
use graph::data::subgraph::schema::DeploymentCreate;
3+
use graph::data::value::Word;
4+
use graph::data_source::CausalityRegion;
35
use graph::schema::InputSchema;
46
use lazy_static::lazy_static;
7+
use std::collections::BTreeSet;
58
use std::marker::PhantomData;
69
use test_store::*;
710

8-
use graph::components::store::{DeploymentLocator, EntityKey, WritableStore};
11+
use graph::components::store::{DeploymentLocator, DerivedEntityQuery, EntityKey, WritableStore};
912
use graph::data::subgraph::*;
1013
use graph::semver::Version;
1114
use graph::{entity, prelude::*};
@@ -125,47 +128,113 @@ async fn pause_writer(deployment: &DeploymentLocator) {
125128
writable::allow_steps(deployment, 0).await;
126129
}
127130

128-
async fn resume_writer(deployment: &DeploymentLocator, steps: usize) {
129-
writable::allow_steps(deployment, steps).await;
130-
flush(deployment).await.unwrap();
131-
}
132-
133-
#[test]
134-
fn tracker() {
135-
run_test(|store, writable, deployment| async move {
131+
/// Test that looking up entities when several changes to the same entity
132+
/// are queued works. When `batch` is true, the changes all reside in one
133+
/// batch. If it is false, each change is in its own batch.
134+
///
135+
/// `read_count` lets us look up entities in different ways to exercise
136+
/// different methods in `WritableStore`
137+
fn get_with_pending<F>(batch: bool, read_count: F)
138+
where
139+
F: Send + Fn(&dyn WritableStore) -> i32 + Sync + 'static,
140+
{
141+
run_test(move |store, writable, deployment| async move {
136142
let subgraph_store = store.subgraph_store();
137143

138-
let read_count = || {
139-
let counter = writable.get(&count_key("1")).unwrap().unwrap();
140-
counter.get("count").unwrap().as_int().unwrap()
141-
};
144+
let read_count = || read_count(writable.as_ref());
145+
146+
if !batch {
147+
writable.deployment_synced().unwrap();
148+
}
149+
142150
for count in 1..4 {
143151
insert_count(&subgraph_store, &deployment, count).await;
144152
}
153+
154+
// Test reading back with pending writes to the same entity
145155
pause_writer(&deployment).await;
156+
for count in 4..7 {
157+
insert_count(&subgraph_store, &deployment, count).await;
158+
}
159+
assert_eq!(6, read_count());
146160

147-
// Test reading back with a pending write
148-
insert_count(&subgraph_store, &deployment, 4).await;
149-
assert_eq!(4, read_count());
150-
resume_writer(&deployment, 1).await;
151-
assert_eq!(4, read_count());
161+
writable.flush().await.unwrap();
162+
assert_eq!(6, read_count());
152163

153-
// Test reading back with a pending revert
164+
// Test reading back with pending writes and a pending revert
165+
for count in 7..10 {
166+
insert_count(&subgraph_store, &deployment, count).await;
167+
}
154168
writable
155169
.revert_block_operations(block_pointer(2), FirehoseCursor::None)
156170
.await
157171
.unwrap();
158172

159173
assert_eq!(2, read_count());
160174

161-
resume_writer(&deployment, 1).await;
162-
assert_eq!(2, read_count());
163-
164-
// There shouldn't be anything left to do, but make sure of that
165175
writable.flush().await.unwrap();
176+
assert_eq!(2, read_count());
166177
})
167178
}
168179

180+
/// Get the count using `WritableStore::get_many`
181+
fn count_get_many(writable: &dyn WritableStore) -> i32 {
182+
let key = count_key("1");
183+
let keys = BTreeSet::from_iter(vec![key.clone()]);
184+
let counter = writable.get_many(keys).unwrap().get(&key).unwrap().clone();
185+
counter.get("count").unwrap().as_int().unwrap()
186+
}
187+
188+
/// Get the count using `WritableStore::get`
189+
fn count_get(writable: &dyn WritableStore) -> i32 {
190+
let counter = writable.get(&count_key("1")).unwrap().unwrap();
191+
counter.get("count").unwrap().as_int().unwrap()
192+
}
193+
194+
fn count_get_derived(writable: &dyn WritableStore) -> i32 {
195+
let key = count_key("1");
196+
let query = DerivedEntityQuery {
197+
entity_type: key.entity_type.clone(),
198+
entity_field: Word::from("id"),
199+
value: key.entity_id.clone(),
200+
id_is_bytes: false,
201+
causality_region: CausalityRegion::ONCHAIN,
202+
};
203+
let map = writable.get_derived(&query).unwrap();
204+
let counter = map.get(&key).unwrap();
205+
counter.get("count").unwrap().as_int().unwrap()
206+
}
207+
208+
#[test]
209+
fn get_batch() {
210+
get_with_pending(true, count_get);
211+
}
212+
213+
#[test]
214+
fn get_nobatch() {
215+
get_with_pending(false, count_get);
216+
}
217+
218+
#[test]
219+
fn get_many_batch() {
220+
get_with_pending(true, count_get_many);
221+
}
222+
223+
#[test]
224+
fn get_many_nobatch() {
225+
get_with_pending(false, count_get_many);
226+
}
227+
228+
#[test]
229+
fn get_derived_batch() {
230+
get_with_pending(true, count_get_derived);
231+
}
232+
233+
#[test]
234+
fn get_derived_nobatch() {
235+
get_with_pending(false, count_get_derived);
236+
}
237+
169238
#[test]
170239
fn restart() {
171240
run_test(|store, writable, deployment| async move {

0 commit comments

Comments
 (0)