You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by an...@apache.org on 2016/11/22 03:27:28 UTC

[08/13] mesos git commit: Introduced a `readAll()` helper on `http::Pipe::Reader`.

Introduced a `readAll()` helper on `http::Pipe::Reader`.

The helper reads from the pipe till EOF. This is used later to
read BODY requests from the streaming request decoder.

Review: https://reviews.apache.org/r/53485/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/e8e3fe59
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/e8e3fe59
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/e8e3fe59

Branch: refs/heads/master
Commit: e8e3fe596f242767fc10ccb95cbdcd36c49a89a5
Parents: 5152728
Author: Anand Mazumdar <an...@apache.org>
Authored: Mon Nov 21 18:07:29 2016 -0800
Committer: Anand Mazumdar <an...@apache.org>
Committed: Mon Nov 21 19:27:08 2016 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/http.hpp | 11 +++++++
 3rdparty/libprocess/src/http.cpp             | 25 ++++++++++++++
 3rdparty/libprocess/src/tests/http_tests.cpp | 40 +++++++++++++++++++++++
 3 files changed, 76 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e8e3fe59/3rdparty/libprocess/include/process/http.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/http.hpp b/3rdparty/libprocess/include/process/http.hpp
index ed853d8..a684e09 100644
--- a/3rdparty/libprocess/include/process/http.hpp
+++ b/3rdparty/libprocess/include/process/http.hpp
@@ -307,6 +307,12 @@ public:
     // is closed.
     Future<std::string> read();
 
+    // Performs a series of asynchronous reads, until EOF is reached.
+    // Returns the concatenated result of the reads.
+    // Returns Failure if the writer failed, or the read-end
+    // is closed.
+    Future<std::string> readAll();
+
     // Closing the read-end of the pipe before the write-end closes
     // or fails will notify the writer that the reader is no longer
     // interested. Returns false if the read-end was already closed.
@@ -327,6 +333,11 @@ public:
 
     explicit Reader(const std::shared_ptr<Data>& _data) : data(_data) {}
 
+    // Continuation for `readAll()`.
+    static Future<std::string> _readAll(
+        Pipe::Reader reader,
+        const std::shared_ptr<std::string>& buffer);
+
     std::shared_ptr<Data> data;
   };
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/e8e3fe59/3rdparty/libprocess/src/http.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/http.cpp b/3rdparty/libprocess/src/http.cpp
index b5ddac9..ca7c948 100644
--- a/3rdparty/libprocess/src/http.cpp
+++ b/3rdparty/libprocess/src/http.cpp
@@ -418,6 +418,31 @@ Future<string> Pipe::Reader::read()
 }
 
 
+Future<string> Pipe::Reader::readAll()
+{
+  std::shared_ptr<string> buffer(new string());
+
+  return _readAll(*this, buffer);
+}
+
+
+Future<string> Pipe::Reader::_readAll(
+    Pipe::Reader reader,
+    const std::shared_ptr<string>& buffer)
+{
+  return reader.read()
+    .then([reader, buffer](const string& read) -> Future<string> {
+      if (read.empty()) { // EOF.
+        return std::move(*buffer);
+      }
+
+      buffer->append(read);
+
+      return _readAll(reader, buffer);
+    });
+}
+
+
 bool Pipe::Reader::close()
 {
   bool closed = false;

http://git-wip-us.apache.org/repos/asf/mesos/blob/e8e3fe59/3rdparty/libprocess/src/tests/http_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/http_tests.cpp b/3rdparty/libprocess/src/tests/http_tests.cpp
index 9147679..10d3fb0 100644
--- a/3rdparty/libprocess/src/tests/http_tests.cpp
+++ b/3rdparty/libprocess/src/tests/http_tests.cpp
@@ -395,6 +395,46 @@ TEST(HTTPTest, PipeFailure)
 }
 
 
+TEST(HTTPTest, PipeReadAll)
+{
+  {
+    http::Pipe pipe;
+    http::Pipe::Reader reader = pipe.reader();
+    http::Pipe::Writer writer = pipe.writer();
+
+    Future<string> readAll = reader.readAll();
+    EXPECT_TRUE(readAll.isPending());
+
+    // Close the writer after writing some data. This should result in
+    // a successful `readAll()` operation.
+    EXPECT_TRUE(writer.write("hello"));
+    EXPECT_TRUE(writer.write("world"));
+
+    EXPECT_TRUE(writer.close());
+
+    AWAIT_EXPECT_EQ("helloworld", readAll);
+  }
+
+  {
+    http::Pipe pipe;
+    http::Pipe::Reader reader = pipe.reader();
+    http::Pipe::Writer writer = pipe.writer();
+
+    Future<string> readAll = reader.readAll();
+    EXPECT_TRUE(readAll.isPending());
+
+    // Fail the writer after writing some data. This should result in
+    // a failed `readAll()` operation.
+    EXPECT_TRUE(writer.write("hello"));
+    EXPECT_TRUE(writer.write("world"));
+
+    EXPECT_TRUE(writer.fail("disconnected!"));
+
+    AWAIT_EXPECT_FAILED(readAll);
+  }
+}
+
+
 TEST(HTTPTest, PipeReaderCloses)
 {
   http::Pipe pipe;