Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/testing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ end
AsyncIO(terminal::VirtualTerminal) = AsyncIO(terminal.output)

function Base.peek(io::AsyncIO, ::Type{UInt8})
while bytesavailable(io) < 1 yield() end
return @lock io.lock io.buffer[1]
while true
@lock io.lock begin
isempty(io.buffer) || return io.buffer[1]
end
yield()
end
end
function Base.read(io::AsyncIO, ::Type{UInt8})
ref = Ref(0x00)
Expand All @@ -97,17 +101,24 @@ function Base.readavailable(io::AsyncIO)
end

function Base.unsafe_read(io::AsyncIO, to::Ptr{UInt8}, nb::UInt)
written = 0
written = UInt(0)
while written < nb
bytesavailable(io) > 0 || (yield(); continue)
@lock io.lock begin
n = @lock io.lock begin
(; buffer) = io
n = min(length(buffer), nb)
GC.@preserve buffer begin
unsafe_copyto!(to, pointer(buffer), n)
n = min(UInt(length(buffer)), nb - written)
if n > 0
GC.@preserve buffer begin
unsafe_copyto!(to + written, pointer(buffer), n)
end
splice!(buffer, 1:n)
end
splice!(buffer, 1:n)
written += n
n
end
written += n
if written < nb
# Yield first to give the background task a chance to read more data
yield()
eof(io) && throw(EOFError())
Copy link
Collaborator

@serenity4 serenity4 Dec 4, 2025

Choose a reason for hiding this comment

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

From what I can see, on a semantic level, the changes:

  • perform multiple unsafe_copyto! instead of waiting for the buffer to have all the data then copy in one go.
  • check for eof(io) at the end of every iteration.

But nothing related to

Fix unsafe_read to correctly calculate remaining bytes (nb - written) and use proper destination offset (to + written)

and we should already correctly calculate the right number of bytes.

The rest seems to be mostly noise. If the issue was that we need an extra istaskdone(io.task) && throw(EOFError()) in the loop, I'm all good with it, but through the LLM noise that single line appears to be the only change worth noting and the rest just increases the complexity for no reason AFAIU.

end
end
end
Expand Down
Loading