Skip to content

Commit f6ee324

Browse files
committed
Possibility to reset claims for testing purposes
1 parent 1109819 commit f6ee324

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "interchange"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["Nicolas Stalder <[email protected]>"]
55
edition = "2018"
66
description = "Request/response mechanism for embedded development, using atomics"

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ pub trait Interchange: Sized {
160160
/// Returns singleton static instances until all that were allocated are
161161
/// used up, thereafter, `None` is returned.
162162
fn claim() -> Option<(Requester<Self>, Responder<Self>)>;
163+
/// Method purely for testing - do not use in production
164+
///
165+
/// Rationale: In production, interchanges are supposed to be set up
166+
/// as global singletons during intialization. In testing however, multiple
167+
/// test cases are run serially; without this reset, such tests would need
168+
/// to allocate an extremely large amount of clients.
169+
///
170+
/// It does not work to put this behind a feature flag, as macro expansion
171+
/// happens at call site and can't see the feature.
172+
unsafe fn reset_claims();
163173

164174
#[doc(hidden)]
165175
unsafe fn rq_ref(&self) -> &Self::REQUEST;

src/macros.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,31 @@ macro_rules! interchange {
7575
)
7676
}
7777
}
78+
79+
fn last_claimed() -> &'static core::sync::atomic::AtomicUsize {
80+
use core::sync::atomic::{AtomicUsize, Ordering};
81+
static LAST_CLAIMED: AtomicUsize = AtomicUsize::new(0);
82+
&LAST_CLAIMED
83+
}
7884
}
7985

8086
impl $crate::Interchange for $Name {
8187
type REQUEST = $REQUEST;
8288
type RESPONSE = $RESPONSE;
8389

90+
unsafe fn reset_claims() {
91+
// debug!("last claimed was {}",
92+
// Self::last_claimed().load( core::sync::atomic::Ordering::SeqCst));
93+
Self::last_claimed().store(0, core::sync::atomic::Ordering::SeqCst);
94+
// debug!("last claimed is {}",
95+
// Self::last_claimed().load( core::sync::atomic::Ordering::SeqCst));
96+
}
97+
8498
fn claim() -> Option<($crate::Requester<Self>, $crate::Responder<Self>)> {
8599
use core::sync::atomic::{AtomicUsize, Ordering};
86-
static LAST_CLAIMED: AtomicUsize = AtomicUsize::new(0);
87-
let index = LAST_CLAIMED.fetch_add(1, Ordering::SeqCst);
100+
let last_claimed = Self::last_claimed();
101+
// static LAST_CLAIMED: AtomicUsize = AtomicUsize::new(0);
102+
let index = last_claimed.fetch_add(1, Ordering::SeqCst);
88103
if index > $N {
89104
None
90105
} else {

0 commit comments

Comments
 (0)