Apache Mesos
disk_profile_server.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __TESTS_DISK_PROFILE_SERVER_HPP__
18 #define __TESTS_DISK_PROFILE_SERVER_HPP__
19 
20 #include <memory>
21 
22 #include <gmock/gmock.h>
23 
24 #include <process/dispatch.hpp>
25 #include <process/future.hpp>
26 #include <process/http.hpp>
27 #include <process/owned.hpp>
28 #include <process/process.hpp>
29 
30 #include <stout/none.hpp>
31 
32 namespace mesos {
33 namespace internal {
34 namespace tests {
35 
36 // A simple helper to provide a mock HTTP endpoint for `UriDiskProfileAdaptor`
37 // to fetch disk profile mappings from.
39  : public process::Process<TestDiskProfileServerProcess>
40 {
41 public:
43  : process::ProcessBase("test-disk-profile-server") {}
44 
46  {
47  return process::http::URL(
48  "http",
49  process::address().ip,
50  process::address().port,
51  self().id + "/profiles");
52  }
53 
55  profiles,
57 
58 private:
59  void initialize() override
60  {
61  route("/profiles", None(), &Self::profiles);
62  }
63 };
64 
65 
67 {
68 public:
70  {
71  // TODO(chhsiao): Make `server` a `unique_ptr` and move it into the
72  // following lambda once we get C++14.
73  std::shared_ptr<TestDiskProfileServer> server(new TestDiskProfileServer());
74 
75  // Wait for the process to finish initializing so that the routes are ready.
76  return process::dispatch(
77  server->process->self(),
78  [=]() -> std::shared_ptr<TestDiskProfileServer> { return server; });
79  }
80 
82  {
84  process::wait(process.get());
85  }
86 
87  std::unique_ptr<TestDiskProfileServerProcess> process;
88 
89 private:
91  {
92  process::spawn(process.get());
93  }
94 };
95 
96 } // namespace tests {
97 } // namespace internal {
98 } // namespace mesos {
99 
100 #endif // __TESTS_DISK_PROFILE_SERVER_HPP__
ProcessBase(const std::string &id="")
TestDiskProfileServerProcess()
Definition: disk_profile_server.hpp:42
void route(const std::string &name, const Option< std::string > &help, const HttpRequestHandler &handler, const RouteOptions &options=RouteOptions())
Sets up a handler for HTTP requests with the specified name.
UPID spawn(ProcessBase *process, bool manage=false)
Spawn a new process.
void terminate(const UPID &pid, bool inject=true)
Sends a TerminateEvent to the given process.
process::http::URL url()
Definition: disk_profile_server.hpp:45
Definition: http.hpp:533
void dispatch(const PID< T > &pid, void(T::*method)())
Definition: dispatch.hpp:174
Definition: http.hpp:130
Definition: agent.hpp:25
~TestDiskProfileServer()
Definition: disk_profile_server.hpp:81
bool wait(const UPID &pid, const Duration &duration=Seconds(-1))
Wait for the process to exit for no more than the specified seconds.
Definition: disk_profile_server.hpp:38
Definition: none.hpp:27
Definition: attributes.hpp:24
Definition: executor.hpp:48
network::inet::Address address()
Returns the socket address associated with this instance of the library.
Definition: disk_profile_server.hpp:66
std::unique_ptr< TestDiskProfileServerProcess > process
Definition: disk_profile_server.hpp:87
Definition: process.hpp:505
static process::Future< std::shared_ptr< TestDiskProfileServer > > create()
Definition: disk_profile_server.hpp:69
MOCK_METHOD1(profiles, process::Future< process::http::Response >(const process::http::Request &))