|
| 1 | +use std::sync::{Arc, Mutex}; |
| 2 | +use wp_api::posts::PostStatus; |
| 3 | +use wp_api::request::endpoint::posts_endpoint::PostEndpointType; |
| 4 | +use wp_mobile::filters::PostListFilter; |
| 5 | +use wp_mobile_cache::{DatabaseDelegate, UpdateHook}; |
| 6 | +use wp_mobile_integration_tests::*; |
| 7 | + |
| 8 | +/// Reproduces an issue where refreshing a post collection sends way too many updates. |
| 9 | +#[tokio::test] |
| 10 | +#[parallel] |
| 11 | +async fn test_minimal_updates() { |
| 12 | + let ctx = create_test_context(); |
| 13 | + |
| 14 | + let collection = ctx |
| 15 | + .service |
| 16 | + .posts() |
| 17 | + .create_post_metadata_collection_with_edit_context( |
| 18 | + PostEndpointType::Posts, |
| 19 | + PostListFilter { |
| 20 | + status: vec![PostStatus::Draft], |
| 21 | + ..Default::default() |
| 22 | + }, |
| 23 | + 10, |
| 24 | + ); |
| 25 | + |
| 26 | + let collector = Arc::new(UpdateCollector::new()); |
| 27 | + ctx.cache.start_listening_for_updates(collector.clone()); |
| 28 | + |
| 29 | + let result = collection.refresh().await; |
| 30 | + assert!(result.is_ok(), "refresh should succeed: {:?}", result.err()); |
| 31 | + |
| 32 | + let all_updates = collector.collected_updates(); |
| 33 | + let relevant_updates: Vec<_> = all_updates |
| 34 | + .iter() |
| 35 | + .filter(|hook| collection.is_relevant_update(hook)) |
| 36 | + .collect(); |
| 37 | + |
| 38 | + // TODO: What's the reasonable amount of updates for the `refresh` call? |
| 39 | + assert!( |
| 40 | + relevant_updates.len() < 5, |
| 41 | + "expected fewer than 5 relevant updates, got {}", |
| 42 | + relevant_updates.len() |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +/// Reproduces an issue where refreshing one post collection trigger updates on an unrelated collection. |
| 47 | +#[tokio::test] |
| 48 | +#[parallel] |
| 49 | +async fn test_update_should_be_isolated() { |
| 50 | + let ctx = create_test_context(); |
| 51 | + |
| 52 | + let draft_collection = ctx |
| 53 | + .service |
| 54 | + .posts() |
| 55 | + .create_post_metadata_collection_with_edit_context( |
| 56 | + PostEndpointType::Posts, |
| 57 | + PostListFilter { |
| 58 | + status: vec![PostStatus::Draft], |
| 59 | + ..Default::default() |
| 60 | + }, |
| 61 | + 10, |
| 62 | + ); |
| 63 | + |
| 64 | + let collector = Arc::new(UpdateCollector::new()); |
| 65 | + ctx.cache.start_listening_for_updates(collector.clone()); |
| 66 | + |
| 67 | + let publish_collection = ctx |
| 68 | + .service |
| 69 | + .posts() |
| 70 | + .create_post_metadata_collection_with_edit_context( |
| 71 | + PostEndpointType::Posts, |
| 72 | + PostListFilter { |
| 73 | + status: vec![PostStatus::Publish], |
| 74 | + ..Default::default() |
| 75 | + }, |
| 76 | + 10, |
| 77 | + ); |
| 78 | + |
| 79 | + let result = publish_collection.refresh().await; |
| 80 | + assert!(result.is_ok(), "refresh should succeed: {:?}", result.err()); |
| 81 | + |
| 82 | + let all_updates = collector.collected_updates(); |
| 83 | + let draft_relevant_updates: Vec<_> = all_updates |
| 84 | + .iter() |
| 85 | + .filter(|hook| draft_collection.is_relevant_update(hook)) |
| 86 | + .collect(); |
| 87 | + |
| 88 | + assert_eq!( |
| 89 | + draft_relevant_updates.len(), |
| 90 | + 0, |
| 91 | + "expected no updates relevant to draft collection when refreshing publish collection, got {}", |
| 92 | + draft_relevant_updates.len() |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +struct UpdateCollector { |
| 97 | + updates: Mutex<Vec<UpdateHook>>, |
| 98 | +} |
| 99 | + |
| 100 | +impl UpdateCollector { |
| 101 | + fn new() -> Self { |
| 102 | + Self { |
| 103 | + updates: Mutex::new(Vec::new()), |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + fn collected_updates(&self) -> Vec<UpdateHook> { |
| 108 | + self.updates.lock().unwrap().clone() |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl DatabaseDelegate for UpdateCollector { |
| 113 | + fn did_update(&self, update_hook: UpdateHook) { |
| 114 | + self.updates.lock().unwrap().push(update_hook); |
| 115 | + } |
| 116 | +} |
0 commit comments