Commit 7e85374b authored by gejun's avatar gejun

Add UT for returning EAGAIN from the IReader

parent 727dd4cd
...@@ -79,7 +79,7 @@ private: ...@@ -79,7 +79,7 @@ private:
// Parse records from the IReader, corrupted records will be skipped. // Parse records from the IReader, corrupted records will be skipped.
// Example: // Example:
// RecordReader rd(ireader); // RecordReader rd(...);
// Record rec; // Record rec;
// while (rd.ReadNext(&rec)) { // while (rd.ReadNext(&rec)) {
// // Handle the rec // // Handle the rec
......
...@@ -8,8 +8,11 @@ namespace { ...@@ -8,8 +8,11 @@ namespace {
class StringReader : public butil::IReader { class StringReader : public butil::IReader {
public: public:
StringReader(const std::string& str) StringReader(const std::string& str,
: _str(str), _offset(0) {} bool report_eagain_on_end = false)
: _str(str)
, _offset(0)
, _report_eagain_on_end(report_eagain_on_end) {}
ssize_t ReadV(const iovec* iov, int iovcnt) override { ssize_t ReadV(const iovec* iov, int iovcnt) override {
size_t total_nc = 0; size_t total_nc = 0;
...@@ -25,11 +28,16 @@ public: ...@@ -25,11 +28,16 @@ public:
break; break;
} }
} }
if (_report_eagain_on_end && total_nc == 0) {
errno = EAGAIN;
return -1;
}
return total_nc; return total_nc;
} }
private: private:
std::string _str; std::string _str;
size_t _offset; size_t _offset;
bool _report_eagain_on_end;
}; };
class StringWriter : public butil::IWriter { class StringWriter : public butil::IWriter {
...@@ -159,6 +167,50 @@ TEST(RecordIOTest, write_read_basic) { ...@@ -159,6 +167,50 @@ TEST(RecordIOTest, write_read_basic) {
ASSERT_EQ(sw.str().size(), rr.read_bytes()); ASSERT_EQ(sw.str().size(), rr.read_bytes());
} }
TEST(RecordIOTest, incomplete_reader) {
StringWriter sw;
butil::RecordWriter rw(&sw);
butil::Record src;
butil::IOBuf* foo_val = src.MutableMeta("foo");
foo_val->append("foo_data");
ASSERT_EQ(0, rw.Write(src));
butil::IOBuf* bar_val = src.MutableMeta("bar");
bar_val->append("bar_data");
ASSERT_EQ(0, rw.Write(src));
ASSERT_EQ(0, rw.Flush());
std::string data = sw.str();
std::cout << "len=" << data.size()
<< " content=" << butil::PrintedAsBinary(data, 256) << std::endl;
StringReader sr(data, true);
butil::RecordReader rr(&sr);
butil::Record r2;
ASSERT_TRUE(rr.ReadNext(&r2));
ASSERT_EQ(0, rr.last_error());
ASSERT_EQ((size_t)1, r2.MetaCount());
ASSERT_EQ("foo", r2.MetaAt(0).name);
ASSERT_EQ("foo_data", *r2.MetaAt(0).data);
ASSERT_TRUE(r2.Payload().empty());
butil::Record r3;
ASSERT_TRUE(rr.ReadNext(&r3));
ASSERT_EQ(0, rr.last_error());
ASSERT_EQ((size_t)2, r3.MetaCount());
ASSERT_EQ("foo", r3.MetaAt(0).name);
ASSERT_EQ("foo_data", *r3.MetaAt(0).data);
ASSERT_EQ("bar", r3.MetaAt(1).name);
ASSERT_EQ("bar_data", *r3.MetaAt(1).data);
ASSERT_TRUE(r3.Payload().empty());
ASSERT_FALSE(rr.ReadNext(NULL));
ASSERT_EQ(EAGAIN, rr.last_error());
ASSERT_EQ(sw.str().size(), rr.read_bytes());
}
static std::string rand_string(int min_len, int max_len) { static std::string rand_string(int min_len, int max_len) {
const int len = butil::fast_rand_in(min_len, max_len); const int len = butil::fast_rand_in(min_len, max_len);
std::string str; std::string str;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment