~albaguirre/mir/possibly-fix-yakkety-build-failure

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright © 2013-2015 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 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/>.
 *
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 */

#include "mir/compositor/display_buffer_compositor_factory.h"
#include "mir/compositor/display_buffer_compositor.h"
#include "mir/run_mir.h"
#include "mir/main_loop.h"

#include "mir_toolkit/mir_client_library.h"

#include "mir_test_framework/fake_input_server_configuration.h"
#include "mir_test_framework/fake_input_device.h"
#include "mir_test_framework/stub_server_platform_factory.h"
#include "mir_test_framework/any_surface.h"
#include "mir_test_framework/server_runner.h"
#include "mir_test_framework/testing_server_configuration.h"
#include "mir_test_framework/using_stub_client_platform.h"

#include "mir/test/doubles/null_display_buffer_compositor_factory.h"
#include "mir/test/doubles/stub_frame_dropping_policy_factory.h"

#include "mir/test/doubles/stub_renderer.h"

#include "mir/dispatch/action_queue.h"
#include "mir/input/input_device.h"
#include "mir/input/input_device_info.h"
#include "mir/input/input_device_registry.h"

#include "mir_toolkit/mir_client_library.h"

#include <gtest/gtest.h>

namespace mtf = mir_test_framework;
namespace mtd = mir::test::doubles;
namespace mc = mir::compositor;
namespace mg = mir::graphics;
namespace mi = mir::input;

namespace
{

struct ServerShutdown : testing::Test, mtf::ServerRunner
{
    std::unique_ptr<mir::DefaultServerConfiguration> server_configuration;
    mtf::UsingStubClientPlatform using_stub_client_platform;

    mir::DefaultServerConfiguration& server_config() override
    {
        return *server_configuration;
    }
};

void null_buffer_stream_callback(MirBufferStream*, void*)
{
}

void null_lifecycle_callback(MirConnection*, MirLifecycleState, void*)
{
}

struct ClientWithSurface
{
    ClientWithSurface(std::string const& server_socket)
        : connection{mir_connect_sync(
            server_socket.c_str(), __PRETTY_FUNCTION__)}
    {

        // Default lifecycle handler terminates the process on disconnect, so
        // override it
        mir_connection_set_lifecycle_event_callback(
            connection, null_lifecycle_callback, nullptr);
        surface = mtf::make_any_surface(connection);
    }

    ~ClientWithSurface()
    {
        mir_surface_release_sync(surface);
        mir_connection_release(connection);
    }

    void swap_sync()
    {
        mir_buffer_stream_swap_buffers_sync(
            mir_surface_get_buffer_stream(surface));
    }

    void swap_async()
    {
        mir_buffer_stream_swap_buffers(
            mir_surface_get_buffer_stream(surface),
            null_buffer_stream_callback, nullptr);
    }

    MirConnection* const connection;
    MirSurface* surface;
};

}

TEST_F(ServerShutdown, server_can_shut_down_when_clients_are_blocked)
{
    struct ServerConfig : mtf::TestingServerConfiguration
    {
        // Don't consume frames, so the clients can block waiting for a buffer
        std::shared_ptr<mc::DisplayBufferCompositorFactory> the_display_buffer_compositor_factory() override
        {
            return display_buffer_compositor_factory(
                [] { return std::make_shared<mtd::NullDisplayBufferCompositorFactory>(); });
        }
        // Don't drop frames, so the clients can block waiting for a buffer
        std::shared_ptr<mc::FrameDroppingPolicyFactory> the_frame_dropping_policy_factory() override
        {
            return frame_dropping_policy_factory(
                [] { return std::make_shared<mtd::StubFrameDroppingPolicyFactory>(); });
        }
    };
    server_configuration.reset(new ServerConfig{});

    start_server();

    ClientWithSurface client1{new_connection()};
    ClientWithSurface client2{new_connection()};
    ClientWithSurface client3{new_connection()};

    client1.swap_sync();
    // Ask for the second buffer (should block)
    client1.swap_async();

    client2.swap_sync();
    // Ask for the second buffer (should block)
    client2.swap_async();

    client3.swap_sync();
    // Ask for the second buffer (should block)
    client3.swap_async();

    // Shutting down the server should not block
    stop_server();
}

TEST_F(ServerShutdown, server_releases_resources_on_shutdown_with_connected_clients)
{
    server_configuration.reset(new mtf::TestingServerConfiguration{});

    start_server();

    ClientWithSurface client1{new_connection()};
    ClientWithSurface client2{new_connection()};
    ClientWithSurface client3{new_connection()};

    stop_server();

    std::weak_ptr<mir::graphics::Display> display = server_configuration->the_display();
    std::weak_ptr<mir::compositor::Compositor> compositor = server_configuration->the_compositor();
    std::weak_ptr<mir::frontend::Connector> connector = server_configuration->the_connector();
    std::weak_ptr<mi::InputManager> input_manager = server_configuration->the_input_manager();

    server_configuration.reset();

    EXPECT_EQ(0, display.use_count());
    EXPECT_EQ(0, compositor.use_count());
    EXPECT_EQ(0, connector.use_count());
    EXPECT_EQ(0, input_manager.use_count());
}

namespace
{

class ExceptionThrowingDisplayBufferCompositorFactory : public mc::DisplayBufferCompositorFactory
{
public:
    std::unique_ptr<mc::DisplayBufferCompositor>
        create_compositor_for(mg::DisplayBuffer&) override
    {
        struct ExceptionThrowingDisplayBufferCompositor : mc::DisplayBufferCompositor
        {
            void composite(mc::SceneElementSequence&&) override
            {
                throw std::runtime_error("ExceptionThrowingDisplayBufferCompositor");
            }
        };

        return std::unique_ptr<mc::DisplayBufferCompositor>(
            new ExceptionThrowingDisplayBufferCompositor{});
    }
};

}

TEST(ServerShutdownWithThreadException,
     server_releases_resources_on_abnormal_compositor_thread_termination)
{
    struct ServerConfig : mtf::TestingServerConfiguration
    {
        std::shared_ptr<mc::DisplayBufferCompositorFactory>
            the_display_buffer_compositor_factory() override
        {
            return display_buffer_compositor_factory(
                [this]()
                {
                    return std::make_shared<ExceptionThrowingDisplayBufferCompositorFactory>();
                });
        }
    };

    auto server_config = std::make_shared<ServerConfig>();

    std::thread server{
        [&]
        {
            EXPECT_THROW(
                mir::run_mir(*server_config, [](mir::DisplayServer&){}),
                std::runtime_error);
        }};

    server.join();

    std::weak_ptr<mir::graphics::Display> display = server_config->the_display();
    std::weak_ptr<mir::compositor::Compositor> compositor = server_config->the_compositor();
    std::weak_ptr<mir::frontend::Connector> connector = server_config->the_connector();
    std::weak_ptr<mi::InputManager> input_manager = server_config->the_input_manager();
    std::weak_ptr<mi::InputDeviceHub> hub = server_config->the_input_device_hub();

    server_config.reset();

    EXPECT_EQ(0, display.use_count());
    EXPECT_EQ(0, compositor.use_count());
    EXPECT_EQ(0, connector.use_count());
    EXPECT_EQ(0, input_manager.use_count());
    EXPECT_EQ(0, hub.use_count());
}

TEST(ServerShutdownWithThreadException,
     server_releases_resources_on_abnormal_input_thread_termination)
{
    auto server_config = std::make_shared<mtf::FakeInputServerConfiguration>();

    std::thread server{
        [&server_config]
        {
            EXPECT_THROW(
                mir::run_mir(*server_config, [](mir::DisplayServer&){}),
                std::runtime_error);
        }};

    {
        auto dev = mtf::add_fake_input_device(mir::input::InputDeviceInfo{"throwing device", "throwing-device-0",
                                                                          mir::input::DeviceCapability::unknown});
        dev->emit_runtime_error();
        server.join();
        dev.reset();
    }

    std::weak_ptr<mir::graphics::Display> display = server_config->the_display();
    std::weak_ptr<mir::compositor::Compositor> compositor = server_config->the_compositor();
    std::weak_ptr<mir::frontend::Connector> connector = server_config->the_connector();
    std::weak_ptr<mi::InputManager> input_manager = server_config->the_input_manager();
    std::weak_ptr<mi::InputDeviceHub> hub = server_config->the_input_device_hub();

    server_config.reset();

    EXPECT_EQ(0, display.use_count());
    EXPECT_EQ(0, compositor.use_count());
    EXPECT_EQ(0, connector.use_count());
    EXPECT_EQ(0, input_manager.use_count());
    EXPECT_EQ(0, hub.use_count());
    }

// This also acts as a regression test for LP: #1378740
TEST(ServerShutdownWithThreadException,
     server_releases_resources_on_abnormal_main_thread_termination)
{
    // Use the FakeInputServerConfiguration to get the production input components
    auto server_config = std::make_shared<mtf::FakeInputServerConfiguration>();

    std::thread server{
        [&]
        {
            EXPECT_THROW(
                mir::run_mir(*server_config,
                    [server_config](mir::DisplayServer&)
                    {
                        server_config->the_main_loop()->enqueue(
                            server_config.get(),
                            [] { throw std::runtime_error(""); });
                    }),
                std::runtime_error);
        }};

    server.join();

    std::weak_ptr<mir::graphics::Display> display = server_config->the_display();
    std::weak_ptr<mir::compositor::Compositor> compositor = server_config->the_compositor();
    std::weak_ptr<mir::frontend::Connector> connector = server_config->the_connector();
    std::weak_ptr<mi::InputManager> input_manager = server_config->the_input_manager();
    std::weak_ptr<mi::InputDeviceHub> hub = server_config->the_input_device_hub();

    server_config.reset();

    EXPECT_EQ(0, display.use_count());
    EXPECT_EQ(0, compositor.use_count());
    EXPECT_EQ(0, connector.use_count());
    EXPECT_EQ(0, input_manager.use_count());
    EXPECT_EQ(0, hub.use_count());
}