use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use crate::transfer::{
IoRequest, PendingCause, PollWork, StateMachineTerminalReceiver, Transfer, TransferContext,
TransferId, WorkOutcome,
};
pub(crate) trait MockStateMachine: Send + Sync + std::fmt::Debug {
fn poll_work(&self, id: TransferId) -> PollWork;
fn execute<'a>(
&'a self,
work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>>;
}
#[derive(Clone)]
pub(crate) struct MockTransfer {
id: TransferId,
ctx: TransferContext,
state_machine: Arc<dyn MockStateMachine>,
}
impl std::fmt::Debug for MockTransfer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MockTransfer")
.field("id", &self.id)
.field("state_machine", &self.state_machine)
.finish()
}
}
impl MockTransfer {
pub(crate) fn new<S: MockStateMachine + 'static>(
id: TransferId,
state_machine: Arc<S>,
) -> Self {
let s3_client = aws_smithy_mocks::mock_client!(aws_sdk_s3, []);
let config = crate::Config::builder()
.sdk_client(s3_client)
.build()
.expect("valid config");
let handle = crate::client::Handle::new_for_test(config, 1);
let (ctx, _completion_rx) = TransferContext::with_id(id, handle);
Self {
id,
ctx,
state_machine,
}
}
pub(crate) fn new_with_handle<S: MockStateMachine + 'static>(
id: TransferId,
state_machine: Arc<S>,
handle: Arc<crate::client::Handle>,
) -> Self {
let (ctx, _completion_rx) = TransferContext::with_id(id, handle);
Self {
id,
ctx,
state_machine,
}
}
pub(crate) fn poll_work(&self) -> PollWork {
let work = self.state_machine.poll_work(self.id);
if matches!(work, PollWork::Pending) {
self.ctx.set_pending(PendingCause::other("mock"));
}
work
}
pub(crate) async fn execute(&self, work: &mut IoRequest) -> WorkOutcome {
let outcome = self.state_machine.execute(work).await;
if matches!(outcome, WorkOutcome::Failed { .. }) {
self.ctx.set_failed(crate::error::from_kind(
crate::error::ErrorKind::RuntimeError,
)("mock transfer failed"));
self.ctx.signal_terminal();
}
outcome
}
}
impl Transfer for MockTransfer {
fn ctx(&self) -> &TransferContext {
&self.ctx
}
fn poll_work(&self) -> PollWork {
MockTransfer::poll_work(self)
}
fn execute<'a>(
&'a self,
work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
Box::pin(MockTransfer::execute(self, work))
}
}
#[derive(Debug)]
pub(crate) struct FixedWorkCount {
total: u64,
generated: AtomicU64,
completed: AtomicU64,
}
impl FixedWorkCount {
pub(crate) fn new(count: u64) -> Self {
Self {
total: count,
generated: AtomicU64::new(0),
completed: AtomicU64::new(0),
}
}
pub(crate) fn is_complete(&self) -> bool {
self.completed.load(Ordering::SeqCst) >= self.total
}
pub(crate) fn completed_count(&self) -> u64 {
self.completed.load(Ordering::SeqCst)
}
}
impl MockStateMachine for FixedWorkCount {
fn poll_work(&self, _id: TransferId) -> PollWork {
let gen = self.generated.fetch_add(1, Ordering::SeqCst);
if gen >= self.total {
return PollWork::Done;
}
PollWork::ready(IoRequest { data: None })
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
Box::pin(async move {
self.completed.fetch_add(1, Ordering::SeqCst);
WorkOutcome::Success { data: None }
})
}
}
#[derive(Debug)]
pub(crate) struct WithDelay<S> {
inner: S,
delay: Duration,
}
impl<S: MockStateMachine> WithDelay<S> {
pub(crate) fn new(inner: S, delay: Duration) -> Self {
Self { inner, delay }
}
pub(crate) fn inner(&self) -> &S {
&self.inner
}
}
impl<S: MockStateMachine> MockStateMachine for WithDelay<S> {
fn poll_work(&self, id: TransferId) -> PollWork {
self.inner.poll_work(id)
}
fn execute<'a>(
&'a self,
work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
Box::pin(async move {
tokio::time::sleep(self.delay).await;
self.inner.execute(work).await
})
}
}
pub(crate) struct WithExecute<S> {
inner: S,
execute_fn: fn(&mut IoRequest) -> WorkOutcome,
}
impl<S: std::fmt::Debug> std::fmt::Debug for WithExecute<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WithExecute")
.field("inner", &self.inner)
.finish()
}
}
impl<S> WithExecute<S> {
pub(crate) fn new(inner: S, execute_fn: fn(&mut IoRequest) -> WorkOutcome) -> Self {
Self { inner, execute_fn }
}
}
impl<S> MockStateMachine for WithExecute<S>
where
S: MockStateMachine,
{
fn poll_work(&self, id: TransferId) -> PollWork {
self.inner.poll_work(id)
}
fn execute<'a>(
&'a self,
work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
let outcome = (self.execute_fn)(work);
Box::pin(async move { outcome })
}
}
#[derive(Debug, Clone)]
pub(crate) struct DispatchCounter {
count: Arc<AtomicU64>,
trace: Option<(u8, Arc<DispatchTrace>)>,
}
impl DispatchCounter {
pub(crate) fn new() -> Self {
Self {
count: Arc::new(AtomicU64::new(0)),
trace: None,
}
}
pub(crate) fn count(&self) -> u64 {
self.count.load(Ordering::SeqCst)
}
fn increment(&self) {
if let Some((label, trace)) = &self.trace {
trace.record(*label);
}
self.count.fetch_add(1, Ordering::SeqCst);
}
}
#[derive(Debug)]
pub(crate) struct DispatchTrace {
next: AtomicUsize,
entries: Box<[AtomicU8]>,
}
impl DispatchTrace {
pub(crate) fn new(capacity: usize) -> Arc<Self> {
let entries = (0..capacity)
.map(|_| AtomicU8::new(0))
.collect::<Vec<_>>()
.into_boxed_slice();
Arc::new(Self {
next: AtomicUsize::new(0),
entries,
})
}
pub(crate) fn counter(self: &Arc<Self>, label: u8) -> DispatchCounter {
assert_ne!(label, 0, "zero is reserved for unwritten trace slots");
DispatchCounter {
count: Arc::new(AtomicU64::new(0)),
trace: Some((label, Arc::clone(self))),
}
}
pub(crate) fn snapshot(&self) -> Vec<u8> {
let len = self.next.load(Ordering::SeqCst);
assert!(
len <= self.entries.len(),
"dispatch trace recorded {len} entries into capacity {}",
self.entries.len()
);
self.entries[..len]
.iter()
.enumerate()
.map(|(index, entry)| {
let label = entry.load(Ordering::SeqCst);
assert_ne!(label, 0, "dispatch trace slot {index} was not published");
label
})
.collect()
}
fn record(&self, label: u8) {
let index = self.next.fetch_add(1, Ordering::SeqCst);
if let Some(entry) = self.entries.get(index) {
entry.store(label, Ordering::SeqCst);
}
}
}
#[derive(Debug)]
pub(crate) struct CountedWork {
total: u64,
generated: AtomicU64,
completed: AtomicU64,
counter: DispatchCounter,
}
impl CountedWork {
pub(crate) fn new(count: u64, counter: DispatchCounter) -> Self {
Self {
total: count,
generated: AtomicU64::new(0),
completed: AtomicU64::new(0),
counter,
}
}
pub(crate) fn is_complete(&self) -> bool {
self.completed.load(Ordering::SeqCst) >= self.total
}
#[allow(dead_code)]
pub(crate) fn completed_count(&self) -> u64 {
self.completed.load(Ordering::SeqCst)
}
}
impl MockStateMachine for CountedWork {
fn poll_work(&self, _id: TransferId) -> PollWork {
let gen = self.generated.fetch_add(1, Ordering::SeqCst);
if gen >= self.total {
return PollWork::Done;
}
PollWork::ready(IoRequest { data: None })
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
Box::pin(async move {
self.counter.increment();
self.completed.fetch_add(1, Ordering::SeqCst);
WorkOutcome::Success { data: None }
})
}
}
#[derive(Debug)]
pub(crate) struct BlockingWork {
generated: AtomicU64,
total: u64,
completed: AtomicU64,
notify: Arc<tokio::sync::Notify>,
counter: DispatchCounter,
}
impl BlockingWork {
#[allow(dead_code)]
pub(crate) fn new(
count: u64,
notify: Arc<tokio::sync::Notify>,
counter: DispatchCounter,
) -> Self {
Self {
generated: AtomicU64::new(0),
total: count,
completed: AtomicU64::new(0),
notify,
counter,
}
}
#[allow(dead_code)]
pub(crate) fn completed_count(&self) -> u64 {
self.completed.load(Ordering::SeqCst)
}
}
impl MockStateMachine for BlockingWork {
fn poll_work(&self, _id: TransferId) -> PollWork {
let gen = self.generated.fetch_add(1, Ordering::SeqCst);
if gen >= self.total {
return PollWork::Done;
}
PollWork::ready(IoRequest { data: None })
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
Box::pin(async move {
self.notify.notified().await;
self.counter.increment();
self.completed.fetch_add(1, Ordering::SeqCst);
WorkOutcome::Success { data: None }
})
}
}
use std::sync::Mutex;
pub(crate) struct ChildMockTransfer {
ctx: TransferContext,
total: u64,
generated: AtomicU64,
completed: AtomicU64,
counter: DispatchCounter,
notify: Option<Arc<tokio::sync::Notify>>,
state_lock: Mutex<()>,
terminated_counter: Option<Arc<AtomicU64>>,
poll_log: Option<Arc<Mutex<Vec<u64>>>>,
}
impl std::fmt::Debug for ChildMockTransfer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ChildMockTransfer")
.field("id", &self.ctx.id)
.finish_non_exhaustive()
}
}
impl ChildMockTransfer {
pub(crate) fn new(
id: TransferId,
handle: Arc<crate::client::Handle>,
work_count: u64,
counter: DispatchCounter,
) -> Self {
let (ctx, _rx) = TransferContext::with_id(id, handle);
Self {
ctx,
total: work_count,
generated: AtomicU64::new(0),
completed: AtomicU64::new(0),
counter,
notify: None,
state_lock: Mutex::new(()),
terminated_counter: None,
poll_log: None,
}
}
pub(crate) fn new_blocking(
id: TransferId,
handle: Arc<crate::client::Handle>,
counter: DispatchCounter,
notify: Arc<tokio::sync::Notify>,
) -> Self {
let (ctx, _rx) = TransferContext::with_id(id, handle);
Self {
ctx,
total: 1,
generated: AtomicU64::new(0),
completed: AtomicU64::new(0),
counter,
notify: Some(notify),
state_lock: Mutex::new(()),
terminated_counter: None,
poll_log: None,
}
}
pub(crate) fn with_terminated_counter(mut self, counter: Arc<AtomicU64>) -> Self {
self.terminated_counter = Some(counter);
self
}
pub(crate) fn with_poll_log(mut self, log: Arc<Mutex<Vec<u64>>>) -> Self {
self.poll_log = Some(log);
self
}
}
impl Transfer for ChildMockTransfer {
fn ctx(&self) -> &TransferContext {
&self.ctx
}
fn poll_work(&self) -> PollWork {
if let Some(ref log) = self.poll_log {
log.lock().unwrap().push(self.ctx.id.id);
}
let gen = self.generated.load(Ordering::SeqCst);
if gen >= self.total {
let _guard = self.state_lock.lock().unwrap();
self.ctx.set_pending(PendingCause::other("test"));
if self.completed.load(Ordering::SeqCst) >= self.total {
drop(_guard);
self.ctx.set_completed();
if let Some(ref tc) = self.terminated_counter {
tc.fetch_add(1, Ordering::SeqCst);
}
self.ctx.signal_terminal();
return PollWork::Done;
}
return PollWork::Pending;
}
self.generated.fetch_add(1, Ordering::SeqCst);
PollWork::ready(IoRequest { data: None })
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
Box::pin(async move {
if let Some(ref notify) = self.notify {
loop {
if self.ctx.is_cancelled() {
return WorkOutcome::Cancelled;
}
match tokio::time::timeout(Duration::from_millis(10), notify.notified()).await {
Ok(()) => break,
Err(_) => continue,
}
}
}
if self.ctx.is_cancelled() {
return WorkOutcome::Cancelled;
}
self.counter.increment();
{
let _guard = self.state_lock.lock().unwrap();
self.completed.fetch_add(1, Ordering::SeqCst);
}
self.ctx.try_wake();
WorkOutcome::Success { data: None }
})
}
}
struct CompositeMockState {
total_children: u64,
spawned: u64,
}
pub(crate) struct CompositeMock {
ctx: TransferContext,
handle: Arc<crate::client::Handle>,
state: Mutex<CompositeMockState>,
work_per_child: u64,
memory_cap: u64,
counter: DispatchCounter,
child_notify: Option<Arc<tokio::sync::Notify>>,
next_child_id: AtomicU64,
terminated_counter: Arc<AtomicU64>,
}
impl std::fmt::Debug for CompositeMock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CompositeMock")
.field("id", &self.ctx.id)
.finish_non_exhaustive()
}
}
impl CompositeMock {
pub(crate) fn new(
id: TransferId,
handle: Arc<crate::client::Handle>,
total_children: u64,
work_per_child: u64,
memory_cap: u64,
counter: DispatchCounter,
) -> Self {
let (ctx, _rx) = TransferContext::with_id(id, handle.clone());
Self {
ctx,
handle,
state: Mutex::new(CompositeMockState {
total_children,
spawned: 0,
}),
work_per_child,
memory_cap,
counter,
child_notify: None,
next_child_id: AtomicU64::new(id.id * 1_000_000 + 1),
terminated_counter: Arc::new(AtomicU64::new(0)),
}
}
pub(crate) fn new_blocking(
id: TransferId,
handle: Arc<crate::client::Handle>,
total_children: u64,
memory_cap: u64,
counter: DispatchCounter,
notify: Arc<tokio::sync::Notify>,
) -> Self {
let (ctx, _rx) = TransferContext::with_id(id, handle.clone());
Self {
ctx,
handle,
state: Mutex::new(CompositeMockState {
total_children,
spawned: 0,
}),
work_per_child: 1,
memory_cap,
counter,
child_notify: Some(notify),
next_child_id: AtomicU64::new(id.id * 1_000_000 + 1),
terminated_counter: Arc::new(AtomicU64::new(0)),
}
}
#[allow(dead_code)]
pub(crate) fn total_spawned(&self) -> u64 {
self.state.lock().unwrap().spawned
}
#[allow(dead_code)]
pub(crate) fn total_terminated(&self) -> u64 {
self.terminated_counter.load(Ordering::SeqCst)
}
#[allow(dead_code)]
pub(crate) fn is_complete(&self) -> bool {
let s = self.state.lock().unwrap();
let terminated = self.terminated_counter.load(Ordering::SeqCst);
terminated >= s.total_children && s.spawned >= s.total_children
}
#[allow(dead_code)]
pub(crate) fn dispatch_counter(&self) -> &DispatchCounter {
&self.counter
}
fn spawn_children(&self, state: &mut CompositeMockState) {
let terminated = self.terminated_counter.load(Ordering::SeqCst);
let in_flight = state.spawned.saturating_sub(terminated);
let available = self.memory_cap.saturating_sub(in_flight);
let remaining = state.total_children - state.spawned;
let to_spawn = available.min(remaining).min(32);
for _ in 0..to_spawn {
let child_id_num = self.next_child_id.fetch_add(1, Ordering::Relaxed);
let child_id = TransferId {
id: child_id_num,
parent: Some(self.ctx.id.id),
};
let child: Box<dyn Transfer> = if let Some(ref notify) = self.child_notify {
Box::new(
ChildMockTransfer::new_blocking(
child_id,
self.handle.clone(),
self.counter.clone(),
notify.clone(),
)
.with_terminated_counter(self.terminated_counter.clone()),
)
} else {
Box::new(
ChildMockTransfer::new(
child_id,
self.handle.clone(),
self.work_per_child,
self.counter.clone(),
)
.with_terminated_counter(self.terminated_counter.clone()),
)
};
self.handle.scheduler.enqueue_transfer(child);
state.spawned += 1;
}
}
}
impl Transfer for CompositeMock {
fn ctx(&self) -> &TransferContext {
&self.ctx
}
fn poll_work(&self) -> PollWork {
let mut state = self.state.lock().unwrap();
let terminated = self.terminated_counter.load(Ordering::SeqCst);
if terminated >= state.total_children && state.spawned >= state.total_children {
drop(state);
self.ctx.set_completed();
self.ctx.signal_terminal();
return PollWork::Done;
}
if state.spawned < state.total_children {
self.spawn_children(&mut state);
}
drop(state);
self.ctx.set_pending(PendingCause::other("test"));
PollWork::Pending
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
unreachable!("CompositeMock never returns PollWork::Ready")
}
}
pub(crate) struct PanickingCompositeMock {
ctx: TransferContext,
}
impl std::fmt::Debug for PanickingCompositeMock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PanickingCompositeMock").finish()
}
}
impl PanickingCompositeMock {
pub(crate) fn new(id: TransferId, handle: Arc<crate::client::Handle>) -> Self {
let (ctx, _rx) = TransferContext::with_id(id, handle);
Self { ctx }
}
}
impl Transfer for PanickingCompositeMock {
fn ctx(&self) -> &TransferContext {
&self.ctx
}
fn poll_work(&self) -> PollWork {
panic!("PanickingCompositeMock: intentional panic in poll_work")
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
unreachable!("PanickingCompositeMock never returns PollWork::Ready")
}
}
pub(crate) struct BuggyDoneMock {
ctx: TransferContext,
handle: Arc<crate::client::Handle>,
num_children: u64,
children_spawned: AtomicBool,
child_terminals: Arc<std::sync::Mutex<Option<Vec<StateMachineTerminalReceiver>>>>,
}
pub(crate) type BuggyDoneChildTerminals =
Arc<std::sync::Mutex<Option<Vec<StateMachineTerminalReceiver>>>>;
impl std::fmt::Debug for BuggyDoneMock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BuggyDoneMock").finish()
}
}
impl BuggyDoneMock {
pub(crate) fn new(
id: TransferId,
handle: Arc<crate::client::Handle>,
num_children: u64,
) -> (Self, BuggyDoneChildTerminals) {
let (ctx, _rx) = TransferContext::with_id(id, handle.clone());
let terminals: BuggyDoneChildTerminals = Arc::new(std::sync::Mutex::new(Some(
Vec::with_capacity(num_children as usize),
)));
let mock = Self {
ctx,
handle,
num_children,
children_spawned: AtomicBool::new(false),
child_terminals: terminals.clone(),
};
(mock, terminals)
}
}
impl Transfer for BuggyDoneMock {
fn ctx(&self) -> &TransferContext {
&self.ctx
}
fn poll_work(&self) -> PollWork {
if !self
.children_spawned
.swap(true, std::sync::atomic::Ordering::SeqCst)
{
let mut terminals = Vec::with_capacity(self.num_children as usize);
for i in 0..self.num_children {
let child_id = TransferId {
id: 100_000 + i,
parent: Some(self.ctx.id.id),
};
let (child, term_rx) = NoopChild::new(child_id, self.handle.clone());
terminals.push(term_rx);
self.handle.scheduler.enqueue_transfer(Box::new(child));
}
*self.child_terminals.lock().unwrap() = Some(terminals);
}
self.ctx.set_completed();
self.ctx.signal_terminal();
PollWork::Done
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
unreachable!("BuggyDoneMock never returns PollWork::Ready")
}
}
pub(crate) struct NoopChild {
ctx: TransferContext,
}
impl std::fmt::Debug for NoopChild {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NoopChild").finish()
}
}
impl NoopChild {
pub(crate) fn new(
id: TransferId,
handle: Arc<crate::client::Handle>,
) -> (Self, StateMachineTerminalReceiver) {
let (ctx, rx) = TransferContext::with_id(id, handle);
(Self { ctx }, rx)
}
}
impl Transfer for NoopChild {
fn ctx(&self) -> &TransferContext {
&self.ctx
}
fn poll_work(&self) -> PollWork {
self.ctx.set_pending(PendingCause::other("test"));
PollWork::Pending
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
unreachable!("NoopChild never returns PollWork::Ready")
}
}
pub(crate) struct TerminalWithoutSignalMock {
ctx: TransferContext,
handle: Arc<crate::client::Handle>,
spawned: AtomicBool,
}
impl std::fmt::Debug for TerminalWithoutSignalMock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TerminalWithoutSignalMock").finish()
}
}
impl TerminalWithoutSignalMock {
pub(crate) fn new(
id: TransferId,
handle: Arc<crate::client::Handle>,
) -> (Self, StateMachineTerminalReceiver) {
let (ctx, completion_rx) = TransferContext::with_id(id, handle.clone());
(
Self {
ctx,
handle,
spawned: AtomicBool::new(false),
},
completion_rx,
)
}
}
impl Transfer for TerminalWithoutSignalMock {
fn ctx(&self) -> &TransferContext {
&self.ctx
}
fn poll_work(&self) -> PollWork {
if !self.spawned.swap(true, Ordering::SeqCst) {
let child_id = TransferId {
id: 200_000,
parent: Some(self.ctx.id.id),
};
let (child, _term_rx) = NoopChild::new(child_id, self.handle.clone());
self.handle.scheduler.enqueue_transfer(Box::new(child));
return PollWork::ready(IoRequest { data: None });
}
self.ctx.set_pending(PendingCause::other("test"));
PollWork::Pending
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
Box::pin(async move {
self.ctx.set_failed(crate::error::from_kind(
crate::error::ErrorKind::RuntimeError,
)("terminal without signal"));
WorkOutcome::Success { data: None }
})
}
}
pub(crate) struct SingleTicketCompositeMock {
ctx: TransferContext,
handle: Arc<crate::client::Handle>,
state: Mutex<CompositeMockState>,
work_per_child: u64,
memory_cap: u64,
counter: DispatchCounter,
child_notify: Option<Arc<tokio::sync::Notify>>,
next_child_id: AtomicU64,
terminated_counter: Arc<AtomicU64>,
spawned_counter: Arc<AtomicU64>,
poll_log: Option<Arc<Mutex<Vec<u64>>>>,
}
impl std::fmt::Debug for SingleTicketCompositeMock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SingleTicketCompositeMock")
.field("id", &self.ctx.id)
.finish_non_exhaustive()
}
}
impl SingleTicketCompositeMock {
pub(crate) fn new(
id: TransferId,
handle: Arc<crate::client::Handle>,
total_children: u64,
work_per_child: u64,
memory_cap: u64,
counter: DispatchCounter,
) -> Self {
let (ctx, _rx) = TransferContext::with_id(id, handle.clone());
Self {
ctx,
handle,
state: Mutex::new(CompositeMockState {
total_children,
spawned: 0,
}),
work_per_child,
memory_cap,
counter,
child_notify: None,
next_child_id: AtomicU64::new(id.id * 1_000_000 + 1),
terminated_counter: Arc::new(AtomicU64::new(0)),
spawned_counter: Arc::new(AtomicU64::new(0)),
poll_log: None,
}
}
pub(crate) fn new_blocking(
id: TransferId,
handle: Arc<crate::client::Handle>,
total_children: u64,
memory_cap: u64,
counter: DispatchCounter,
notify: Arc<tokio::sync::Notify>,
) -> Self {
let (ctx, _rx) = TransferContext::with_id(id, handle.clone());
Self {
ctx,
handle,
state: Mutex::new(CompositeMockState {
total_children,
spawned: 0,
}),
work_per_child: 1,
memory_cap,
counter,
child_notify: Some(notify),
next_child_id: AtomicU64::new(id.id * 1_000_000 + 1),
terminated_counter: Arc::new(AtomicU64::new(0)),
spawned_counter: Arc::new(AtomicU64::new(0)),
poll_log: None,
}
}
pub(crate) fn with_poll_log(mut self, log: Arc<Mutex<Vec<u64>>>) -> Self {
self.poll_log = Some(log);
self
}
#[allow(dead_code)]
pub(crate) fn total_spawned(&self) -> u64 {
self.state.lock().unwrap().spawned
}
#[allow(dead_code)]
pub(crate) fn total_terminated(&self) -> u64 {
self.terminated_counter.load(Ordering::SeqCst)
}
#[allow(dead_code)]
pub(crate) fn is_complete(&self) -> bool {
let s = self.state.lock().unwrap();
let terminated = self.terminated_counter.load(Ordering::SeqCst);
terminated >= s.total_children && s.spawned >= s.total_children
}
#[allow(dead_code)]
pub(crate) fn dispatch_counter(&self) -> &DispatchCounter {
&self.counter
}
#[allow(dead_code)]
pub(crate) fn observer_handles(&self) -> (Arc<AtomicU64>, Arc<AtomicU64>) {
(
self.spawned_counter.clone(),
self.terminated_counter.clone(),
)
}
fn spawn_one_child(&self, state: &mut CompositeMockState) -> bool {
let terminated = self.terminated_counter.load(Ordering::SeqCst);
let in_flight = state.spawned.saturating_sub(terminated);
if in_flight >= self.memory_cap {
return false;
}
if state.spawned >= state.total_children {
return false;
}
let child_id_num = self.next_child_id.fetch_add(1, Ordering::Relaxed);
let child_id = TransferId {
id: child_id_num,
parent: Some(self.ctx.id.id),
};
let mut child = if let Some(ref notify) = self.child_notify {
ChildMockTransfer::new_blocking(
child_id,
self.handle.clone(),
self.counter.clone(),
notify.clone(),
)
.with_terminated_counter(self.terminated_counter.clone())
} else {
ChildMockTransfer::new(
child_id,
self.handle.clone(),
self.work_per_child,
self.counter.clone(),
)
.with_terminated_counter(self.terminated_counter.clone())
};
if let Some(ref log) = self.poll_log {
child = child.with_poll_log(log.clone());
}
let child: Box<dyn Transfer> = Box::new(child);
self.handle.scheduler.enqueue_transfer(child);
state.spawned += 1;
self.spawned_counter.fetch_add(1, Ordering::SeqCst);
true
}
}
impl Transfer for SingleTicketCompositeMock {
fn ctx(&self) -> &TransferContext {
&self.ctx
}
fn poll_work(&self) -> PollWork {
if let Some(ref log) = self.poll_log {
log.lock().unwrap().push(self.ctx.id.id);
}
let mut state = self.state.lock().unwrap();
let terminated = self.terminated_counter.load(Ordering::SeqCst);
if terminated >= state.total_children && state.spawned >= state.total_children {
drop(state);
self.ctx.set_completed();
self.ctx.signal_terminal();
return PollWork::Done;
}
if self.spawn_one_child(&mut state) {
return PollWork::Spawned;
}
drop(state);
self.ctx.set_pending(PendingCause::other("test"));
PollWork::Pending
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
unreachable!("SingleTicketCompositeMock never returns PollWork::Ready")
}
}
#[derive(Debug)]
pub(crate) struct FusedReadySpawnedMock {
polled: AtomicBool,
completed: AtomicU64,
}
impl FusedReadySpawnedMock {
pub(crate) fn new() -> Self {
Self {
polled: AtomicBool::new(false),
completed: AtomicU64::new(0),
}
}
#[allow(dead_code)]
pub(crate) fn completed_count(&self) -> u64 {
self.completed.load(Ordering::SeqCst)
}
}
impl MockStateMachine for FusedReadySpawnedMock {
fn poll_work(&self, _id: TransferId) -> PollWork {
if !self.polled.swap(true, Ordering::SeqCst) {
return PollWork::Ready {
io: IoRequest { data: None },
spawned: true,
};
}
PollWork::Pending
}
fn execute<'a>(
&'a self,
_work: &'a mut IoRequest,
) -> Pin<Box<dyn Future<Output = WorkOutcome> + Send + 'a>> {
Box::pin(async move {
self.completed.fetch_add(1, Ordering::SeqCst);
WorkOutcome::Success { data: None }
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_id() -> TransferId {
TransferId {
id: 1,
parent: None,
}
}
#[cfg_attr(miri, ignore)]
#[tokio::test]
async fn test_fixed_work_count() {
let sm = FixedWorkCount::new(3);
let id = test_id();
for _ in 0..3 {
assert!(matches!(sm.poll_work(id), PollWork::Ready { .. }));
}
assert!(matches!(sm.poll_work(id), PollWork::Done));
}
#[cfg_attr(miri, ignore)]
#[tokio::test]
async fn test_with_delay() {
let sm = WithDelay::new(FixedWorkCount::new(1), Duration::from_millis(50));
let id = test_id();
let mut work = match sm.poll_work(id) {
PollWork::Ready { io, .. } => io,
_ => panic!("expected Ready"),
};
let start = std::time::Instant::now();
sm.execute(&mut work).await;
assert!(start.elapsed() >= Duration::from_millis(50));
}
}