Fix unit tests for the Stream class

refs #6109
This commit is contained in:
Gunnar Beutner 2015-02-14 17:40:29 +01:00
parent f097e48889
commit 9e936cbea4
6 changed files with 25 additions and 15 deletions

View File

@ -34,11 +34,13 @@ using namespace icinga;
*/ */
StreamReadStatus NetString::ReadStringFromStream(const Stream::Ptr& stream, String *str, StreamReadContext& context) StreamReadStatus NetString::ReadStringFromStream(const Stream::Ptr& stream, String *str, StreamReadContext& context)
{ {
if (stream->IsEof()) if (context.Eof)
return StatusEof; return StatusEof;
if (context.MustRead && context.FillFromStream(stream) == 0) if (context.MustRead && !context.FillFromStream(stream)) {
context.Eof = true;
return StatusEof; return StatusEof;
}
size_t header_length = 0; size_t header_length = 0;

View File

@ -63,11 +63,13 @@ void Stream::WaitForData(void)
StreamReadStatus Stream::ReadLine(String *line, StreamReadContext& context) StreamReadStatus Stream::ReadLine(String *line, StreamReadContext& context)
{ {
if (IsEof()) if (context.Eof)
return StatusEof; return StatusEof;
if (context.MustRead) { if (context.MustRead) {
if (!context.FillFromStream(this)) { if (!context.FillFromStream(this)) {
context.Eof = true;
*line = String(context.Buffer, &(context.Buffer[context.Size])); *line = String(context.Buffer, &(context.Buffer[context.Size]));
boost::algorithm::trim_right(*line); boost::algorithm::trim_right(*line);
@ -106,6 +108,8 @@ bool StreamReadContext::FillFromStream(const Stream::Ptr& stream)
if (Wait && stream->SupportsWaiting()) if (Wait && stream->SupportsWaiting())
stream->WaitForData(); stream->WaitForData();
size_t count = 0;
do { do {
Buffer = (char *)realloc(Buffer, Size + 4096); Buffer = (char *)realloc(Buffer, Size + 4096);
@ -114,12 +118,13 @@ bool StreamReadContext::FillFromStream(const Stream::Ptr& stream)
size_t rc = stream->Read(Buffer + Size, 4096, true); size_t rc = stream->Read(Buffer + Size, 4096, true);
if (rc == 0 && stream->IsEof())
return false;
Size += rc; Size += rc;
count += rc;
} while (stream->IsDataAvailable()); } while (stream->IsDataAvailable());
if (count == 0 && stream->IsEof())
return false;
else
return true; return true;
} }

View File

@ -39,7 +39,7 @@ enum ConnectionRole
struct StreamReadContext struct StreamReadContext
{ {
StreamReadContext(bool wait = true) StreamReadContext(bool wait = true)
: Buffer(NULL), Size(0), MustRead(true), Wait(wait) : Buffer(NULL), Size(0), MustRead(true), Eof(false), Wait(wait)
{ } { }
~StreamReadContext(void) ~StreamReadContext(void)
@ -53,6 +53,7 @@ struct StreamReadContext
char *Buffer; char *Buffer;
size_t Size; size_t Size;
bool MustRead; bool MustRead;
bool Eof;
bool Wait; bool Wait;
}; };

View File

@ -43,12 +43,12 @@ BOOST_AUTO_TEST_CASE(io)
BOOST_CHECK(fifo->GetAvailableBytes() == 5); BOOST_CHECK(fifo->GetAvailableBytes() == 5);
char buffer1[2]; char buffer1[2];
fifo->Read(buffer1, 2); fifo->Read(buffer1, 2, true);
BOOST_CHECK(memcmp(buffer1, "he", 2) == 0); BOOST_CHECK(memcmp(buffer1, "he", 2) == 0);
BOOST_CHECK(fifo->GetAvailableBytes() == 3); BOOST_CHECK(fifo->GetAvailableBytes() == 3);
char buffer2[5]; char buffer2[5];
size_t rc = fifo->Read(buffer2, 5); size_t rc = fifo->Read(buffer2, 5, true);
BOOST_CHECK(rc == 3); BOOST_CHECK(rc == 3);
BOOST_CHECK(memcmp(buffer2, "llo", 3) == 0); BOOST_CHECK(memcmp(buffer2, "llo", 3) == 0);
BOOST_CHECK(fifo->GetAvailableBytes() == 0); BOOST_CHECK(fifo->GetAvailableBytes() == 0);

View File

@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(netstring)
String s; String s;
StreamReadContext src; StreamReadContext src;
BOOST_CHECK(NetString::ReadStringFromStream(fifo, &s, src)); BOOST_CHECK(NetString::ReadStringFromStream(fifo, &s, src) == StatusNewItem);
BOOST_CHECK(s == "hello"); BOOST_CHECK(s == "hello");
fifo->Close(); fifo->Close();

View File

@ -37,18 +37,20 @@ BOOST_AUTO_TEST_CASE(readline_stdio)
StreamReadContext rlc; StreamReadContext rlc;
String line; String line;
BOOST_CHECK(stdstream->ReadLine(&line, rlc)); BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusNewItem);
BOOST_CHECK(line == "Hello"); BOOST_CHECK(line == "Hello");
BOOST_CHECK(stdstream->ReadLine(&line, rlc)); BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusNewItem);
BOOST_CHECK(line == "World"); BOOST_CHECK(line == "World");
BOOST_CHECK(stdstream->ReadLine(&line, rlc)); BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusNewItem);
BOOST_CHECK(line == ""); BOOST_CHECK(line == "");
BOOST_CHECK(stdstream->ReadLine(&line, rlc)); BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusNewItem);
BOOST_CHECK(line == ""); BOOST_CHECK(line == "");
BOOST_CHECK(stdstream->ReadLine(&line, rlc) == StatusEof);
stdstream->Close(); stdstream->Close();
} }