Skip to content

Useless TCP test #1123

@Kixunil

Description

@Kixunil

Looking at #1119 I noticed that tests use TCP with seemingly the only intention being testing whether the code can handle broken-up packets. This can be achieved without TCP and without artificial delay of 1s considerably slowing down the tests. I've seen an entire library to simulate breaks at random points, I just can't remember how it was called. But it can be done manually too:

// Breaks up reading to force multiple `read` calls when it reaches the end of each part
struct BreakingReader {
    // Changing to iterators left as an exercise for the reader
    data: Vec<Vec<u8>>,
    idx: usize,
    pos: usize,
}

impl BreakingReader {
    fn new(data: Vec<Vec<u8>>) -> Self {
        BreakingReader {
            data,
            idx: 0,
            pos: 0,
        }
    }
}

impl io::Read for BreakingReader {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if self.idx >= self.data.len() {
            return Ok(0)
        }
        // We never let pos to go out of bounds
        let part = &self.data[self.idx][self.pos..];
        let to_copy = part.len().min(buf.len());
        buf[..to_copy].copy_from_slice(&part[..to_copy]);
        if to_copy == part.len() {
            self.pos = 0;
            self.idx += 1;
        } else {
            self.pos += to_copy;
        }
        Ok(to_copy)
    }
}

Then just replace serve_tcp() with BreakingReader::new() and remove the handle/join.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Testscode qualityMakes code easier to understand and less likely to lead to problemsgood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions