Skip to content
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

Fix futures_io::AsyncSeek implementaion for Compat #5783

Merged
merged 7 commits into from Jun 25, 2023

Conversation

dhruv9vats
Copy link
Contributor

Motivation

Aims to address #5764.

Solution

Waiting for a seek operation to complete by calling poll_complete before calling start_seek.

tokio-util/src/compat.rs Outdated Show resolved Hide resolved
tokio-util/tests/compat.rs Show resolved Hide resolved
@@ -56,6 +58,7 @@ async-stream = "0.3.0"
futures = "0.3.0"
futures-test = "0.3.5"
parking_lot = "0.12.0"
tempfile = "3.1.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a dependency to add a test feels overkill.
But the thing is, the test only fails for a tokio::fs::file::File.
Using something like tokio::io::util::buf_writer::BufWriter or std::io::cursor::Cursor seems to be working fine on latest upstream/master:

#[tokio::test]
async fn compat_seek() -> futures_util::io::Result<()> {
    let mut cursor = Cursor::new(Vec::new()).compat_write();

    cursor.write_all(&[0, 1, 2, 3, 4, 5]).await?;
    cursor.write_all(&[6, 7]).await?;

    assert_eq!(cursor.stream_position().await?, 8);

    // Modify elements at position 2.
    assert_eq!(cursor.seek(SeekFrom::Start(2)).await?, 2);
    cursor.write_all(&[8, 9]).await?;

    cursor.flush().await?;

    // Verify we still have 8 elements
    assert_eq!(cursor.seek(SeekFrom::End(0)).await?, 8);
    // Seek back to the start to read and verify contents.
    cursor.seek(SeekFrom::Start(0)).await?;

    let mut buf = Vec::new();
    let num_bytes = cursor.read_to_end(&mut buf).await?;
    assert_eq!(&buf[..num_bytes], &[0, 1, 8, 9, 4, 5, 6, 7]);

    Ok(())
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Darksonn, is there some alternative way to test this? And would it more sense to skip adding one if there is none?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency already exists in the main Tokio crate, so adding it here doesn't actually add a new dependency.

@dhruv9vats dhruv9vats requested a review from Darksonn June 13, 2023 06:43
@dhruv9vats dhruv9vats marked this pull request as ready for review June 17, 2023 12:20
tokio-util/tests/compat.rs Outdated Show resolved Hide resolved
tokio-util/Cargo.toml Outdated Show resolved Hide resolved
@dhruv9vats dhruv9vats requested a review from Darksonn June 25, 2023 10:16
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@Darksonn Darksonn merged commit 910a1e2 into tokio-rs:master Jun 25, 2023
56 checks passed
pcrumley added a commit to swift-nav/esthri that referenced this pull request Dec 5, 2023
This PR upgrades a bunch of the esthri dependencies, and the main reason
is that we are hoping it will fix a bug we encountered in Tokio:
tokio-rs/tokio#5783
kdesjard pushed a commit to kdesjard/esthri that referenced this pull request Dec 6, 2023
This PR upgrades a bunch of the esthri dependencies, and the main reason
is that we are hoping it will fix a bug we encountered in Tokio:
tokio-rs/tokio#5783
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants