~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to tests/mir_test_framework/in_process_server.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-01-08 02:04:38 UTC
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: package-import@ubuntu.com-20140108020438-e1npu0pm7qdv5wc4
Tags: upstream-0.1.3+14.04.20140108
Import upstream version 0.1.3+14.04.20140108

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2013 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/in_process_server.h"
 
20
 
 
21
#include "mir/default_server_configuration.h"
 
22
#include "mir/display_server.h"
 
23
#include "mir/frontend/connector.h"
 
24
#include "mir/run_mir.h"
 
25
 
 
26
#include <gtest/gtest.h>
 
27
 
 
28
#include <condition_variable>
 
29
#include <mutex>
 
30
 
 
31
namespace mtf = mir_test_framework;
 
32
 
 
33
void mtf::InProcessServer::SetUp()
 
34
{
 
35
    display_server = start_mir_server();
 
36
    ASSERT_TRUE(display_server);
 
37
}
 
38
 
 
39
std::string mtf::InProcessServer::new_connection()
 
40
{
 
41
    char connect_string[64] = {0};
 
42
    sprintf(connect_string, "fd://%d", server_config().the_connector()->client_socket_fd());
 
43
    return connect_string;
 
44
}
 
45
 
 
46
void mtf::InProcessServer::TearDown()
 
47
{
 
48
    ASSERT_TRUE(display_server)
 
49
        << "Did you override SetUp() and forget to call mtf::InProcessServer::SetUp()?";
 
50
    display_server->stop();
 
51
}
 
52
 
 
53
mtf::InProcessServer::~InProcessServer()
 
54
{
 
55
    if (server_thread.joinable()) server_thread.join();
 
56
}
 
57
 
 
58
mir::DisplayServer* mtf::InProcessServer::start_mir_server()
 
59
{
 
60
    std::mutex mutex;
 
61
    std::condition_variable cv;
 
62
    mir::DisplayServer* result{nullptr};
 
63
 
 
64
    server_thread = std::thread([&]
 
65
    {
 
66
        try
 
67
        {
 
68
            mir::run_mir(server_config(), [&](mir::DisplayServer& ds)
 
69
            {
 
70
                std::unique_lock<std::mutex> lock(mutex);
 
71
                result = &ds;
 
72
                cv.notify_one();
 
73
            });
 
74
        }
 
75
        catch (std::exception const& e)
 
76
        {
 
77
            FAIL() << e.what();
 
78
        }
 
79
    });
 
80
 
 
81
    using namespace std::chrono;
 
82
    auto const time_limit = system_clock::now() + seconds(2);
 
83
 
 
84
    std::unique_lock<std::mutex> lock(mutex);
 
85
 
 
86
    while (!result && time_limit > system_clock::now())
 
87
        cv.wait_until(lock, time_limit);
 
88
 
 
89
    return result;
 
90
}