You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by al...@apache.org on 2016/11/25 11:02:08 UTC

[3/4] mesos git commit: Added a test helper to obtain unused port.

Added a test helper to obtain unused port.

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


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

Branch: refs/heads/master
Commit: 9de5fb277b00e77414075e268e8709c0b6d5d0a9
Parents: 5f84a47
Author: Benjamin Bannier <be...@mesosphere.io>
Authored: Fri Nov 25 11:58:46 2016 +0100
Committer: Alexander Rukletsov <al...@apache.org>
Committed: Fri Nov 25 11:58:46 2016 +0100

----------------------------------------------------------------------
 src/tests/utils.cpp | 26 ++++++++++++++++++++++++++
 src/tests/utils.hpp | 10 ++++++++++
 2 files changed, 36 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/9de5fb27/src/tests/utils.cpp
----------------------------------------------------------------------
diff --git a/src/tests/utils.cpp b/src/tests/utils.cpp
index c7769cf..d36aa9c 100644
--- a/src/tests/utils.cpp
+++ b/src/tests/utils.cpp
@@ -20,11 +20,13 @@
 
 #include <mesos/http.hpp>
 
+#include <process/address.hpp>
 #include <process/future.hpp>
 #include <process/gtest.hpp>
 #include <process/http.hpp>
 #include <process/pid.hpp>
 #include <process/process.hpp>
+#include <process/socket.hpp>
 
 #include <stout/gtest.hpp>
 
@@ -37,6 +39,7 @@ using process::UPID;
 using process::address;
 
 namespace http = process::http;
+namespace network = process::network;
 
 namespace mesos {
 namespace internal {
@@ -66,6 +69,29 @@ JSON::Object Metrics()
   return parse.get();
 }
 
+
+Try<uint16_t> getFreePort()
+{
+  // Bind to port=0 to obtain a random unused port.
+  Try<network::Socket> socket = network::Socket::create();
+
+  if (socket.isError()) {
+    return Error(socket.error());
+  }
+
+  Try<network::Address> result = socket->bind(network::Address());
+
+  if (result.isSome()) {
+    return result->port;
+  } else {
+    return Error(result.error());
+  }
+
+  // No explicit cleanup of `socket` as we rely on the implementation
+  // of `Socket` to close the socket on destruction.
+}
+
+
 string getModulePath(const string& name)
 {
   string path = path::join(tests::flags.build_dir, "src", ".libs");

http://git-wip-us.apache.org/repos/asf/mesos/blob/9de5fb27/src/tests/utils.hpp
----------------------------------------------------------------------
diff --git a/src/tests/utils.hpp b/src/tests/utils.hpp
index dcdb03d..cdbbc1c 100644
--- a/src/tests/utils.hpp
+++ b/src/tests/utils.hpp
@@ -17,9 +17,12 @@
 #ifndef __TESTS_UTILS_HPP__
 #define __TESTS_UTILS_HPP__
 
+#include <stdint.h>
+
 #include <string>
 
 #include <stout/json.hpp>
+#include <stout/try.hpp>
 
 namespace mesos {
 namespace internal {
@@ -71,6 +74,13 @@ std::string getSbinDir();
 // Get the path to the directory of the webui files/assets.
 std::string getWebUIDir();
 
+// Get a random unused port.
+//
+// NOTE: While we make sure that the port returned by this function is
+// unused when this function returns, it might become used before the
+// caller can bind to it.
+Try<uint16_t> getFreePort();
+
 } // namespace tests {
 } // namespace internal {
 } // namespace mesos {