use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;
use std::task::Context;
use std::task::Poll;
use rings_core::delegation::DelegateeKey;
use tokio::task::AbortHandle;
use tokio::task::JoinError;
use tokio::task::JoinSet;
use crate::error::Result;
use crate::extension::ext::Extensions;
use crate::extension::ext::Scope;
use crate::processor::ProcessorBuilder;
use crate::processor::ProcessorConfig;
use crate::sync_lock::lock;
pub(crate) fn test_scope(delegatee_key: DelegateeKey, namespace: &str) -> Result<Scope> {
let config = ProcessorConfig::new(1, String::new(), delegatee_key, 1);
let processor = ProcessorBuilder::from_config(&config)?
.advertise_presence(false)
.build()?;
let extensions = Extensions::new(Arc::new(processor));
Ok(Scope::new(extensions.core(), namespace.to_string()))
}
pub(crate) fn without_runtime<T: Send>(work: impl FnOnce() -> T + Send) -> T {
std::thread::scope(|scope| {
scope
.spawn(|| {
assert!(rings_runtime::Spawner::current().is_err());
work()
})
.join()
.unwrap_or_else(|panic| std::panic::resume_unwind(panic))
})
}
pub(crate) struct BlockingSendProbe<K> {
blocked: AtomicBool,
released: AtomicBool,
blocked_key: Mutex<Option<K>>,
entered: tokio::sync::Notify,
release: tokio::sync::Notify,
}
impl<K> Default for BlockingSendProbe<K> {
fn default() -> Self {
Self {
blocked: AtomicBool::new(false),
released: AtomicBool::new(false),
blocked_key: Mutex::new(None),
entered: tokio::sync::Notify::new(),
release: tokio::sync::Notify::new(),
}
}
}
impl<K> BlockingSendProbe<K>
where K: Clone + Eq
{
pub(crate) async fn block_key(&self, key: K) -> Result<()> {
if !self.register(key)? {
return Ok(());
}
self.wait_for_release().await;
Ok(())
}
fn register(&self, key: K) -> Result<bool> {
let mut blocked_key = lock(&self.blocked_key)?;
if blocked_key.is_none() {
*blocked_key = Some(key.clone());
self.blocked.store(true, Ordering::Release);
self.entered.notify_one();
}
Ok(blocked_key.as_ref() == Some(&key))
}
async fn wait_for_release(&self) {
while !self.released.load(Ordering::Acquire) {
let released = self.release.notified();
if self.released.load(Ordering::Acquire) {
return;
}
released.await;
}
}
pub(crate) async fn wait_until_blocked(&self) {
while !self.blocked.load(Ordering::Acquire) {
let entered = self.entered.notified();
if self.blocked.load(Ordering::Acquire) {
return;
}
entered.await;
}
}
pub(crate) fn release(&self) {
self.released.store(true, Ordering::Release);
self.release.notify_waiters();
}
}
pub(crate) struct TestTask<T> {
set: JoinSet<T>,
handle: AbortHandle,
}
impl<T: Send + 'static> TestTask<T> {
pub(crate) fn spawn(task: impl Future<Output = T> + Send + 'static) -> Self {
let mut set = JoinSet::new();
let handle = set.spawn(task);
Self { set, handle }
}
}
impl<T> TestTask<T> {
pub(crate) fn is_finished(&self) -> bool {
self.handle.is_finished()
}
pub(crate) fn abort(&self) {
self.handle.abort();
}
}
impl<T: 'static> Future for TestTask<T> {
type Output = std::result::Result<T, JoinError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.set.poll_join_next(cx) {
Poll::Ready(Some(result)) => Poll::Ready(result),
Poll::Ready(None) | Poll::Pending => Poll::Pending,
}
}
}