~ubuntu-branches/ubuntu/vivid/mir/vivid

« back to all changes in this revision

Viewing changes to tests/mir_test_framework/headless_test.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-11-20 16:41:15 UTC
  • mto: This revision was merged to the branch mainline in revision 86.
  • Revision ID: package-import@ubuntu.com-20141120164115-a3j4vq6cq2u78m47
Tags: upstream-0.9.0+15.04.20141120.1
Import upstream version 0.9.0+15.04.20141120.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2014 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored By: Alan Griffiths <alan@octopull.co.uk>
 
17
 */
 
18
 
 
19
#include "mir_test_framework/headless_test.h"
 
20
 
 
21
#include "mir/fd.h"
 
22
#include "mir/main_loop.h"
 
23
 
 
24
#include <boost/throw_exception.hpp>
 
25
 
 
26
namespace mtf = mir_test_framework;
 
27
 
 
28
namespace
 
29
{
 
30
std::chrono::seconds const timeout{10};
 
31
}
 
32
 
 
33
mtf::HeadlessTest::HeadlessTest()
 
34
{
 
35
    add_to_environment("MIR_SERVER_PLATFORM_GRAPHICS_LIB", "libmirplatformstub.so");
 
36
}
 
37
 
 
38
void mtf::HeadlessTest::add_to_environment(char const* key, char const* value)
 
39
{
 
40
    env.emplace_back(key, value);
 
41
}
 
42
 
 
43
void mtf::HeadlessTest::start_server()
 
44
{
 
45
    server.add_init_callback([&]
 
46
        {
 
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().
 
51
            main_loop->enqueue(
 
52
                this,
 
53
                [&]
 
54
                {
 
55
                    std::lock_guard<std::mutex> lock(mutex);
 
56
                    server_running = true;
 
57
                    started.notify_one();
 
58
                });
 
59
        });
 
60
 
 
61
    server.apply_settings();
 
62
 
 
63
    server_thread = std::thread([&]
 
64
        {
 
65
            try
 
66
            {
 
67
                server.run();
 
68
            }
 
69
            catch (std::exception const& e)
 
70
            {
 
71
                FAIL() << e.what();
 
72
            }
 
73
            std::lock_guard<std::mutex> lock(mutex);
 
74
            server_running = false;
 
75
            started.notify_one();
 
76
        });
 
77
 
 
78
    std::unique_lock<std::mutex> lock(mutex);
 
79
    started.wait_for(lock, timeout, [&] { return server_running; });
 
80
 
 
81
    if (!server_running)
 
82
    {
 
83
        BOOST_THROW_EXCEPTION(std::runtime_error{"Failed to start server thread"});
 
84
    }
 
85
}
 
86
 
 
87
void mtf::HeadlessTest::stop_server()
 
88
{
 
89
    connections.clear();
 
90
    server.stop();
 
91
    wait_for_server_exit();
 
92
}
 
93
 
 
94
void mtf::HeadlessTest::wait_for_server_exit()
 
95
{
 
96
    std::unique_lock<std::mutex> lock(mutex);
 
97
    started.wait_for(lock, timeout, [&] { return !server_running; });
 
98
 
 
99
    if (server_running)
 
100
    {
 
101
        BOOST_THROW_EXCEPTION(std::logic_error{"stop_server() failed to stop server"});
 
102
    }
 
103
}
 
104
 
 
105
mtf::HeadlessTest::~HeadlessTest() noexcept
 
106
{
 
107
    if (server_thread.joinable()) server_thread.join();
 
108
}
 
109
 
 
110
auto mtf::HeadlessTest::new_connection() -> std::string
 
111
{
 
112
    return connection(server.open_client_socket());
 
113
}
 
114
 
 
115
auto mtf::HeadlessTest::connection(mir::Fd fd) -> std::string
 
116
{
 
117
    char connect_string[64] = {0};
 
118
    connections.push_back(fd);
 
119
    sprintf(connect_string, "fd://%d", fd.operator int());
 
120
    return connect_string;
 
121
}