2
* Copyright © 2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored By: Alan Griffiths <alan@octopull.co.uk>
19
#include "mir_test_framework/headless_test.h"
22
#include "mir/main_loop.h"
24
#include <boost/throw_exception.hpp>
26
namespace mtf = mir_test_framework;
30
std::chrono::seconds const timeout{10};
33
mtf::HeadlessTest::HeadlessTest()
35
add_to_environment("MIR_SERVER_PLATFORM_GRAPHICS_LIB", "libmirplatformstub.so");
38
void mtf::HeadlessTest::add_to_environment(char const* key, char const* value)
40
env.emplace_back(key, value);
43
void mtf::HeadlessTest::start_server()
45
server.add_init_callback([&]
47
auto const main_loop = server.the_main_loop();
48
// By enqueuing the notification code in the main loop, we are
49
// ensuring that the server has really and fully started before
50
// leaving start_mir_server().
55
std::lock_guard<std::mutex> lock(mutex);
56
server_running = true;
61
server.apply_settings();
63
server_thread = std::thread([&]
69
catch (std::exception const& e)
73
std::lock_guard<std::mutex> lock(mutex);
74
server_running = false;
78
std::unique_lock<std::mutex> lock(mutex);
79
started.wait_for(lock, timeout, [&] { return server_running; });
83
BOOST_THROW_EXCEPTION(std::runtime_error{"Failed to start server thread"});
87
void mtf::HeadlessTest::stop_server()
91
wait_for_server_exit();
94
void mtf::HeadlessTest::wait_for_server_exit()
96
std::unique_lock<std::mutex> lock(mutex);
97
started.wait_for(lock, timeout, [&] { return !server_running; });
101
BOOST_THROW_EXCEPTION(std::logic_error{"stop_server() failed to stop server"});
105
mtf::HeadlessTest::~HeadlessTest() noexcept
107
if (server_thread.joinable()) server_thread.join();
110
auto mtf::HeadlessTest::new_connection() -> std::string
112
return connection(server.open_client_socket());
115
auto mtf::HeadlessTest::connection(mir::Fd fd) -> std::string
117
char connect_string[64] = {0};
118
connections.push_back(fd);
119
sprintf(connect_string, "fd://%d", fd.operator int());
120
return connect_string;