-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Implement cooperative multitasking for UnixDatagram
#5967
Changes from all commits
3d4d096
2d4b02c
b55690e
f37a7b1
c995a75
b507e63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -411,3 +411,47 @@ async fn poll_ready() -> io::Result<()> { | |
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test(flavor = "current_thread")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the test macro will actually default to this |
||
async fn coop_uds() -> io::Result<()> { | ||
use std::sync::atomic::{AtomicU32, Ordering}; | ||
use std::time::{Duration, Instant}; | ||
|
||
const HELLO: &[u8] = b"hello world"; | ||
const DURATION: Duration = Duration::from_secs(1); | ||
|
||
let dir = tempfile::tempdir().unwrap(); | ||
let server_path = dir.path().join("server.sock"); | ||
|
||
let client = std::os::unix::net::UnixDatagram::unbound().unwrap(); | ||
let server = UnixDatagram::bind(&server_path).unwrap(); | ||
|
||
let counter = Arc::new(AtomicU32::new(0)); | ||
|
||
let counter_jh = tokio::spawn({ | ||
let counter = counter.clone(); | ||
|
||
async move { | ||
loop { | ||
tokio::time::sleep(Duration::from_millis(250)).await; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would recommend avoiding time in tests like this, and instead using yield_now to ensure that tasks wait a certain number of ticks rather than an amount of time. timing makes things brittle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how would I do that. Are there any existing tests in the code base that I could reference as a template? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I'd recommend doing here is rewriting this test such that you have the main task use a pair of UDS sockets to message itself and read the datagram message in a for loop which runs just often enough that it should deplete its budget once, with a second task spawned before the loop which sets a boolean flag and exits to indicate that it run. You'd then assert after the loop in the main task that the flag was flipped and the second task ran. |
||
counter.fetch_add(1, Ordering::Relaxed); | ||
} | ||
} | ||
}); | ||
|
||
let mut buf = [0; HELLO.len()]; | ||
let start = Instant::now(); | ||
while Instant::now().duration_since(start) < DURATION { | ||
let _ = client.send_to(HELLO, &server_path); | ||
let _ = server.recv(&mut buf[..]).await.unwrap(); | ||
} | ||
|
||
counter_jh.abort(); | ||
let _ = counter_jh.await; | ||
|
||
let expected = ((DURATION.as_secs() * 4) as f64 * 0.5) as u32; | ||
let counter = counter.load(Ordering::Relaxed); | ||
assert!(counter >= expected); | ||
|
||
Ok(()) | ||
} |
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.
I already mentioned this on our Discord chat, but I'll put it here too. This code isn't correct because when the coop budget is empty, you're registering the waker with the IO driver. But in this case, we need to be woken up by the coop system, and not by the IO waker. So you should register for readiness with the coop system instead.
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.
Yes. Thank you for pointing it out. I'm swamped right now with my job and university. I'll get to it as soon as I find some free time.
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.
No worries. That's perfectly fine.
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.
@maminrayej I can take this over from you if you're busy, I'd like to get this change over the finish line relatively quickly.
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.
@Noah-Kennedy Absolutely, feel free to take it over. Thanks for stepping in.
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.
no problem!