Skip to content

Commit

Permalink
Compute API - Update the tests to ensure the internal waiter map
Browse files Browse the repository at this point in the history
is empty at the end
  • Loading branch information
tatsuya6502 committed Jan 9, 2024
1 parent cc36d72 commit a5d0674
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
42 changes: 38 additions & 4 deletions src/future/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2045,18 +2045,26 @@ where
}
}

// For unit tests.
// For unit tests.
#[cfg(test)]
impl<K, V, S> Cache<K, V, S> {
pub(crate) fn is_table_empty(&self) -> bool {
self.entry_count() == 0
}

pub(crate) fn is_waiter_map_empty(&self) -> bool {
self.value_initializer.waiter_count() == 0
}
}

#[cfg(test)]
impl<K, V, S> Cache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
{
fn is_table_empty(&self) -> bool {
self.entry_count() == 0
}

fn invalidation_predicate_count(&self) -> usize {
self.base.invalidation_predicate_count()
}
Expand Down Expand Up @@ -3439,6 +3447,8 @@ mod tests {
};

futures_util::join!(task1, task2, task3, task4, task5);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -3519,6 +3529,8 @@ mod tests {
};

futures_util::join!(task1, task2, task3, task4, task5);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -3653,6 +3665,8 @@ mod tests {
};

futures_util::join!(task1, task2, task3, task4, task5, task6, task7);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -3787,6 +3801,8 @@ mod tests {
};

futures_util::join!(task1, task2, task3, task4, task5, task6, task7);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -3922,6 +3938,8 @@ mod tests {
};

futures_util::join!(task1, task2, task3, task4, task5, task6, task7, task8);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -4063,6 +4081,8 @@ mod tests {
};

futures_util::join!(task1, task2, task3, task4, task5, task6, task7, task8);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -4329,6 +4349,8 @@ mod tests {
};

futures_util::join!(task1, task2, task3, task4, task5, task6, task7, task8);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -4401,6 +4423,8 @@ mod tests {
assert_eq!(ent3.into_value(), 3);

assert_eq!(cache.get(&KEY).await, Some(3));

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -4566,6 +4590,8 @@ mod tests {
panic!("Expected `Unchanged`. Got {res6:?}")
};
assert_eq!(*entry.into_value().read().await, vec![5]);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -4687,6 +4713,8 @@ mod tests {
*entry.into_value().read().await,
vec![1, 2] // Removed value.
);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -4738,6 +4766,8 @@ mod tests {
cache.try_get_with(1, async { Ok(5) }).await as Result<_, Arc<Infallible>>,
Ok(5)
);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -4768,6 +4798,8 @@ mod tests {
handle.abort();

assert_eq!(cache.get_with(1, async { 5 }).await, 5);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down Expand Up @@ -4801,6 +4833,8 @@ mod tests {
cache.try_get_with(1, async { Ok(5) }).await as Result<_, Arc<Infallible>>,
Ok(5)
);

assert!(cache.is_waiter_map_empty());
}

#[tokio::test]
Expand Down
7 changes: 7 additions & 0 deletions src/future/value_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,13 @@ where
}
}

#[cfg(test)]
impl<K, V, S> ValueInitializer<K, V, S> {
pub(crate) fn waiter_count(&self) -> usize {
self.waiters.len()
}
}

#[inline]
fn remove_waiter<K, V, S>(waiter_map: &WaiterMap<K, V, S>, w_key: (Arc<K>, TypeId), w_hash: u64)
where
Expand Down
41 changes: 37 additions & 4 deletions src/sync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1864,17 +1864,24 @@ where
}

// For unit tests.
#[cfg(test)]
impl<K, V, S> Cache<K, V, S> {
pub(crate) fn is_table_empty(&self) -> bool {
self.entry_count() == 0
}

pub(crate) fn is_waiter_map_empty(&self) -> bool {
self.value_initializer.waiter_count() == 0
}
}

#[cfg(test)]
impl<K, V, S> Cache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
S: BuildHasher + Clone + Send + Sync + 'static,
{
pub(crate) fn is_table_empty(&self) -> bool {
self.entry_count() == 0
}

pub(crate) fn invalidation_predicate_count(&self) -> usize {
self.base.invalidation_predicate_count()
}
Expand Down Expand Up @@ -3070,6 +3077,8 @@ mod tests {
for t in [thread1, thread2, thread3, thread4, thread5] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -3153,6 +3162,8 @@ mod tests {
for t in [thread1, thread2, thread3, thread4, thread5] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -3290,6 +3301,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -3429,6 +3442,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -3568,6 +3583,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -3707,6 +3724,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -3836,6 +3855,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -3965,6 +3986,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -4032,6 +4055,8 @@ mod tests {
assert_eq!(ent3.into_value(), 3);

assert_eq!(cache.get(&KEY), Some(3));

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -4185,6 +4210,8 @@ mod tests {
panic!("Expected `Unchanged`. Got {res6:?}")
};
assert_eq!(*entry.into_value().read().unwrap(), vec![5]);

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -4302,6 +4329,8 @@ mod tests {
*entry.into_value().read().unwrap(),
vec![1, 2] // Removed value.
);

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand All @@ -4325,6 +4354,8 @@ mod tests {

barrier.wait();
assert_eq!(cache.get_with(1, || 5), 5);

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand All @@ -4351,6 +4382,8 @@ mod tests {
cache.try_get_with(1, || Ok(5)) as Result<_, Arc<Infallible>>,
Ok(5)
);

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down
15 changes: 15 additions & 0 deletions src/sync/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,13 @@ where
}

// For unit tests.
#[cfg(test)]
impl<K, V, S> SegmentedCache<K, V, S> {
fn is_waiter_map_empty(&self) -> bool {
self.inner.segments.iter().all(Cache::is_waiter_map_empty)
}
}

#[cfg(test)]
impl<K, V, S> SegmentedCache<K, V, S>
where
Expand Down Expand Up @@ -1419,6 +1426,8 @@ mod tests {
for t in [thread1, thread2, thread3, thread4, thread5] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -1548,6 +1557,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -1686,6 +1697,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

#[test]
Expand Down Expand Up @@ -1815,6 +1828,8 @@ mod tests {
] {
t.join().expect("Failed to join");
}

assert!(cache.is_waiter_map_empty());
}

// This test ensures that the `contains_key`, `get` and `invalidate` can use
Expand Down
7 changes: 7 additions & 0 deletions src/sync/value_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,10 @@ where
(w_key, w_hash)
}
}

#[cfg(test)]
impl<K, V, S> ValueInitializer<K, V, S> {
pub(crate) fn waiter_count(&self) -> usize {
self.waiters.len()
}
}

0 comments on commit a5d0674

Please sign in to comment.