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 BufferedStream.write #2721

Merged
merged 1 commit into from Apr 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 24 additions & 7 deletions stream/vibe/stream/bufferedstream.d
Expand Up @@ -295,13 +295,6 @@ struct BufferedStream(S) {

size_t write(in ubyte[] bytes, IOMode mode)
@blocking {
if (bytes.length <= state.peekBuffer.length) {
state.peekBuffer[0 .. bytes.length] = bytes;
state.peekBuffer = state.peekBuffer[bytes.length .. $];
state.ptr += bytes.length;
return bytes.length;
}

size_t nwritten = 0;

ubyte[] newpeek;
Expand Down Expand Up @@ -552,6 +545,30 @@ mixin validateRandomAccessStream!(BufferedStream!RandomAccessStream);
assert(bb[0] == 130);
}

@safe unittest { // regression: write after read causes write to be missed during flush
import std.exception : assertThrown;
import vibe.stream.memory : createMemoryStream;
import vibe.stream.operations : readAll;

auto buf = new ubyte[](256);
foreach (i, ref b; buf) b = cast(ubyte)i;
auto str = createMemoryStream(buf, true, 128);
auto bstr = bufferedStream(str, 16, 4);

ubyte[1] ob;
bstr.read(ob[]);
assert(ob[0] == 0);
bstr.seek(0);
bstr.write([cast(ubyte)1]);
bstr.seek(0);
bstr.read(ob[]);
assert(ob[0] == 1);
bstr.finalize();
str.seek(0);
str.read(ob[]);
assert(ob[0] == 1);
}

unittest {
static assert(isTruncatableStream!(BufferedStream!TruncatableStream));
static assert(isClosableRandomAccessStream!(BufferedStream!ClosableRandomAccessStream));
Expand Down