File: async_server_runner.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (171 lines) | stat: -rw-r--r-- 5,501 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mir_test_framework/async_server_runner.h"
#include "mir_test_framework/command_line_server_configuration.h"

#include "mir/fd.h"
#include <mir/logging/dumb_console_logger.h>
#include <mir/logging/file_logger.h>
#include <mir/logging/multi_logger.h>
#include "mir/main_loop.h"
#include "mir/options/option.h"
#include <mir/report_exception.h>
#include <mir/thread_name.h>
#include "mir/test/doubles/null_logger.h"

#include <boost/throw_exception.hpp>

#include <gtest/gtest.h>

namespace ml = mir::logging;
namespace mo = mir::options;
namespace msh = mir::shell;
namespace mtd = mir::test::doubles;
namespace mtf = mir_test_framework;
namespace mt = mir::test;

namespace
{
std::chrono::seconds const timeout{20};

std::string const get_log_filename(const ::testing::TestInfo* test_info)
{
    std::ostringstream filename;
    filename << "/tmp/" << test_info->test_case_name() << "_" << test_info->name() << "_server.log";
    return filename.str();
}
}

mtf::AsyncServerRunner::AsyncServerRunner()
: output_filename{get_log_filename(::testing::UnitTest::GetInstance()->current_test_info())}
{
    unsetenv("WAYLAND_DISPLAY");    // We don't want to conflict with any existing Wayland server
    configure_from_commandline(server);

    server.add_configuration_option(mtd::logging_opt, mtd::logging_descr, false);
    server.override_the_logger([&]() -> std::shared_ptr<ml::Logger>
        {
            std::ofstream out{output_filename};
            if (out.good())
            {
                std::cerr << "Saving server logs to: " << output_filename << std::endl;
                if (server.get_options()->get<bool>(mtd::logging_opt))
                {
                    return std::make_shared<ml::MultiLogger>(
                        std::initializer_list<std::shared_ptr<ml::Logger>>{
                            std::make_shared<ml::DumbConsoleLogger>(),
                            std::make_shared<ml::FileLogger>(std::move(out))
                        }
                    );
                }
                else
                {
                    return std::make_shared<ml::FileLogger>(std::move(out));
                }
            }
            else
            {
                std::cerr << "Failed to open log file: " << output_filename << std::endl;
                if (!server.get_options()->get<bool>(mtd::logging_opt))
                {
                    return std::make_shared<mtd::NullLogger>();
                }
            }

            // If we return null then the default will be used
            return {};
        });
}

void mtf::AsyncServerRunner::add_to_environment(char const* key, char const* value)
{
    env.emplace_back(key, value);
}

void mtf::AsyncServerRunner::start_server()
{
    mt::AutoJoinThread t([this]
        {
            try
            {
                mir::set_thread_name("mtf/AsyncServer");

                server.add_init_callback([this]
                    {
                        auto const main_loop = server.the_main_loop();
                        // By enqueuing the notification code in the main loop, we are
                        // ensuring that the server has really and fully started before
                        // leaving start_mir_server().
                        main_loop->enqueue(
                            this,
                            [this]
                            {
                                {
                                    std::lock_guard lock(mutex);
                                    server_running = true;
                                }
                                started.notify_one();
                            });
                    });

                server.apply_settings();
                server.run();
            }
            catch (std::exception const& e)
            {
                std::ostringstream error;
                mir::report_exception(error);
                FAIL() << error.str();
            }
            {
                std::lock_guard lock(mutex);
                server_running = false;
            }
            started.notify_one();
        });

    std::unique_lock lock(mutex);
    started.wait_for(lock, timeout, [this] { return server_running; });

    if (!server_running)
    {
        BOOST_THROW_EXCEPTION(std::runtime_error{"Failed to start server thread"});
    }
    server_thread = std::move(t);
}

void mtf::AsyncServerRunner::stop_server()
{
    server.stop();
    wait_for_server_exit();
}

void mtf::AsyncServerRunner::wait_for_server_exit()
{
    std::unique_lock lock(mutex);
    started.wait_for(lock, timeout, [&] { return !server_running; });

    if (server_running)
    {
        BOOST_THROW_EXCEPTION(std::logic_error{"stop_server() failed to stop server"});
    }
    server_thread.stop();
}

mtf::AsyncServerRunner::~AsyncServerRunner() noexcept
{
}