-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/async support #95
Open
xgroleau
wants to merge
13
commits into
jamesmunns:main
Choose a base branch
from
xgroleau:feat/async-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a394567
wip
xgroleau f028688
Basic async support
xgroleau e93a979
added exection order test
xgroleau 318a07c
Removed unused import
xgroleau 63adbae
removed optional
xgroleau 91362d6
Remove unsound grant exact async api
xgroleau 00ed22d
Revert "Remove unsound grant exact async api"
xgroleau 6c6ed17
Added frame api
xgroleau 4ef4059
remove dep on embassy and added frame test
xgroleau 3f8e445
Added doc and tests
xgroleau 170e2d3
Wrapped waker in a struct
xgroleau 34a7d4b
ran fmt
xgroleau 2a72118
Added explicit return
xgroleau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
|
||
use bbqueue::BBBuffer; | ||
use futures::executor::block_on; | ||
|
||
#[test] | ||
fn frame_wrong_size() { | ||
block_on(async { | ||
let bb: BBBuffer<256> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split_framed().unwrap(); | ||
|
||
// Create largeish grants | ||
let mut wgr = prod.grant(127).unwrap(); | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = i as u8; | ||
} | ||
// Note: In debug mode, this hits a debug_assert | ||
wgr.commit(256); | ||
|
||
let rgr = cons.read().unwrap(); | ||
assert_eq!(rgr.len(), 127); | ||
for (i, by) in rgr.iter().enumerate() { | ||
assert_eq!((i as u8), *by); | ||
} | ||
rgr.release(); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn full_size() { | ||
block_on(async { | ||
let bb: BBBuffer<256> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split_framed().unwrap(); | ||
let mut ctr = 0; | ||
|
||
for _ in 0..10_000 { | ||
// Create largeish grants | ||
if let Ok(mut wgr) = prod.grant(127) { | ||
ctr += 1; | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = i as u8; | ||
} | ||
wgr.commit(127); | ||
|
||
let rgr = cons.read().unwrap(); | ||
assert_eq!(rgr.len(), 127); | ||
for (i, by) in rgr.iter().enumerate() { | ||
assert_eq!((i as u8), *by); | ||
} | ||
rgr.release(); | ||
} else { | ||
// Create smallish grants | ||
let mut wgr = prod.grant(1).unwrap(); | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = i as u8; | ||
} | ||
wgr.commit(1); | ||
|
||
let rgr = cons.read().unwrap(); | ||
assert_eq!(rgr.len(), 1); | ||
for (i, by) in rgr.iter().enumerate() { | ||
assert_eq!((i as u8), *by); | ||
} | ||
rgr.release(); | ||
}; | ||
} | ||
|
||
assert!(ctr > 1); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn frame_overcommit() { | ||
block_on(async { | ||
let bb: BBBuffer<256> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split_framed().unwrap(); | ||
|
||
// Create largeish grants | ||
let mut wgr = prod.grant(128).unwrap(); | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = i as u8; | ||
} | ||
wgr.commit(255); | ||
|
||
let mut wgr = prod.grant(64).unwrap(); | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = (i as u8) + 128; | ||
} | ||
wgr.commit(127); | ||
|
||
let rgr = cons.read().unwrap(); | ||
assert_eq!(rgr.len(), 128); | ||
rgr.release(); | ||
|
||
let rgr = cons.read().unwrap(); | ||
assert_eq!(rgr.len(), 64); | ||
rgr.release(); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn frame_undercommit() { | ||
block_on(async { | ||
let bb: BBBuffer<512> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split_framed().unwrap(); | ||
|
||
for _ in 0..100_000 { | ||
// Create largeish grants | ||
let mut wgr = prod.grant(128).unwrap(); | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = i as u8; | ||
} | ||
wgr.commit(13); | ||
|
||
let mut wgr = prod.grant(64).unwrap(); | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = (i as u8) + 128; | ||
} | ||
wgr.commit(7); | ||
|
||
let mut wgr = prod.grant(32).unwrap(); | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = (i as u8) + 192; | ||
} | ||
wgr.commit(0); | ||
|
||
let rgr = cons.read().unwrap(); | ||
assert_eq!(rgr.len(), 13); | ||
rgr.release(); | ||
|
||
let rgr = cons.read().unwrap(); | ||
assert_eq!(rgr.len(), 7); | ||
rgr.release(); | ||
|
||
let rgr = cons.read().unwrap(); | ||
assert_eq!(rgr.len(), 0); | ||
rgr.release(); | ||
} | ||
}); | ||
} | ||
|
||
#[test] | ||
fn frame_auto_commit_release() { | ||
block_on(async { | ||
let bb: BBBuffer<256> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split_framed().unwrap(); | ||
|
||
for _ in 0..100 { | ||
{ | ||
let mut wgr = prod.grant(64).unwrap(); | ||
wgr.to_commit(64); | ||
for (i, by) in wgr.iter_mut().enumerate() { | ||
*by = i as u8; | ||
} | ||
// drop | ||
} | ||
|
||
{ | ||
let mut rgr = cons.read().unwrap(); | ||
rgr.auto_release(true); | ||
let rgr = rgr; | ||
|
||
for (i, by) in rgr.iter().enumerate() { | ||
assert_eq!(*by, i as u8); | ||
} | ||
assert_eq!(rgr.len(), 64); | ||
// drop | ||
} | ||
} | ||
|
||
assert!(cons.read().is_none()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use bbqueue::BBBuffer; | ||
use bbqueue::Error; | ||
use futures::{executor::block_on, future::join}; | ||
|
||
#[test] | ||
fn test_read() { | ||
let bb: BBBuffer<6> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split().unwrap(); | ||
|
||
{ | ||
let mut grant = prod.grant_exact(4).unwrap(); | ||
let buf = grant.buf(); | ||
buf[0] = 0xDE; | ||
buf[1] = 0xAD; | ||
buf[2] = 0xC0; | ||
buf[3] = 0xDE; | ||
grant.commit(4); | ||
} | ||
|
||
let r_grant = block_on(cons.read_async()).unwrap(); | ||
|
||
assert_eq!(4, r_grant.len()); | ||
assert_eq!(r_grant[0], 0xDE); | ||
assert_eq!(r_grant[1], 0xAD); | ||
assert_eq!(r_grant[2], 0xC0); | ||
assert_eq!(r_grant[3], 0xDE); | ||
} | ||
|
||
#[test] | ||
fn test_write() { | ||
let bb: BBBuffer<6> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split().unwrap(); | ||
|
||
let mut w_grant = block_on(prod.grant_exact_async(4)).unwrap(); | ||
assert_eq!(4, w_grant.len()); | ||
w_grant[0] = 0xDE; | ||
w_grant[1] = 0xAD; | ||
w_grant[2] = 0xC0; | ||
w_grant[3] = 0xDE; | ||
w_grant.commit(4); | ||
|
||
let grant = cons.read().unwrap(); | ||
let rx_buf = grant.buf(); | ||
assert_eq!(4, rx_buf.len()); | ||
assert_eq!(rx_buf[0], 0xDE); | ||
assert_eq!(rx_buf[1], 0xAD); | ||
assert_eq!(rx_buf[2], 0xC0); | ||
assert_eq!(rx_buf[3], 0xDE); | ||
} | ||
|
||
#[test] | ||
fn test_read_after_write() { | ||
let bb: BBBuffer<6> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split().unwrap(); | ||
|
||
let read_fut = async { | ||
let r_grant = cons.read_async().await.unwrap(); | ||
r_grant.release(4); | ||
|
||
let time = std::time::Instant::now(); // TODO: Remove time dependence in test | ||
#[cfg(feature = "verbose")] | ||
println!("Read completed at {:?}", time); | ||
time | ||
}; | ||
|
||
let write_fut = async { | ||
let mut w_grant = prod.grant_exact_async(4).await.unwrap(); | ||
w_grant[0] = 0xDE; | ||
w_grant[1] = 0xAD; | ||
w_grant[2] = 0xC0; | ||
w_grant[3] = 0xDE; | ||
w_grant.commit(4); | ||
|
||
let time = std::time::Instant::now(); // TODO: Remove time dependence in test | ||
#[cfg(feature = "verbose")] | ||
println!("Write completed at {:?}", time); | ||
time | ||
}; | ||
|
||
let (r_time, w_time) = block_on(join(read_fut, write_fut)); | ||
assert!(r_time > w_time) | ||
} | ||
|
||
#[test] | ||
fn grant_exact_too_big() { | ||
let bb: BBBuffer<6> = BBBuffer::new(); | ||
let (mut prod, mut _cons) = bb.try_split().unwrap(); | ||
let w_grant_res = block_on(async { prod.grant_exact_async(8).await }); | ||
|
||
assert_eq!(w_grant_res.unwrap_err(), Error::InsufficientSize); | ||
} | ||
|
||
#[test] | ||
fn grant_exact_loop() { | ||
let bb: BBBuffer<6> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split().unwrap(); | ||
let w_grant = prod.grant_exact(4).unwrap(); | ||
w_grant.commit(4); | ||
|
||
let read_fut = async { | ||
let r_grant = cons.read_async().await.unwrap(); | ||
r_grant.release(4); | ||
|
||
let time = std::time::Instant::now(); // TODO: Remove time dependence in test | ||
#[cfg(feature = "verbose")] | ||
println!("Read completed at {:?}", time); | ||
time | ||
}; | ||
|
||
let write_fut = async { | ||
let w_grant = prod.grant_exact_async(3).await.unwrap(); | ||
w_grant.commit(4); | ||
|
||
let time = std::time::Instant::now(); // TODO: Remove time dependence in test | ||
#[cfg(feature = "verbose")] | ||
println!("Write completed at {:?}", time); | ||
time | ||
}; | ||
|
||
let (w_time, r_time) = block_on(join(write_fut, read_fut)); | ||
assert!(r_time < w_time); | ||
} | ||
|
||
#[test] | ||
fn grant_exact_loop_too_big() { | ||
let bb: BBBuffer<6> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split().unwrap(); | ||
let w_grant = prod.grant_exact(4).unwrap(); | ||
w_grant.commit(4); | ||
|
||
let read_fut = async { | ||
let r_grant = cons.read_async().await.unwrap(); | ||
r_grant.release(4); | ||
}; | ||
|
||
let write_fut = async { | ||
let w_grant = prod.grant_exact_async(4).await; | ||
assert_eq!(w_grant.unwrap_err(), Error::InsufficientSize); | ||
}; | ||
|
||
block_on(join(write_fut, read_fut)); | ||
} | ||
|
||
#[test] | ||
fn write_cancelled() { | ||
let bb: BBBuffer<6> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split().unwrap(); | ||
let w_grant_fut = prod.grant_exact_async(6); | ||
drop(w_grant_fut); | ||
let r_grant = cons.read(); | ||
assert_eq!(r_grant.unwrap_err(), Error::InsufficientSize); | ||
} | ||
|
||
#[test] | ||
fn read_cancelled() { | ||
let bb: BBBuffer<6> = BBBuffer::new(); | ||
let (mut prod, mut cons) = bb.try_split().unwrap(); | ||
let w_grant = prod.grant_exact(6).unwrap(); | ||
w_grant.commit(6); | ||
|
||
let r_grant_fut = cons.read_async(); | ||
drop(r_grant_fut); | ||
|
||
let w_grant = prod.grant_max_remaining(4); | ||
assert_eq!(w_grant.unwrap_err(), Error::InsufficientSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like some of these tests are invoking the sync and not async versions (
grant
instead ofgrant_async
). Is that intended?