forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_test.h
71 lines (60 loc) · 2.79 KB
/
thread_test.h
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_TESTING_THREAD_TEST_H_
#define FLUTTER_TESTING_THREAD_TEST_H_
#include <memory>
#include <string>
#include "flutter/fml/macros.h"
#include "flutter/fml/message_loop.h"
#include "flutter/fml/task_runner.h"
#include "flutter/fml/thread.h"
#include "gtest/gtest.h"
namespace flutter {
namespace testing {
//------------------------------------------------------------------------------
/// @brief A fixture that creates threads with running message loops that
/// are terminated when the test is done (the threads are joined
/// then as well). While this fixture may be used on it's own, it is
/// often sub-classed by other fixtures whose functioning requires
/// threads to be created as necessary.
///
class ThreadTest : public ::testing::Test {
public:
ThreadTest();
//----------------------------------------------------------------------------
/// @brief Get the task runner for the thread that the current unit-test
/// is running on. This creates a message loop as necessary.
///
/// @attention Unlike all other threads and task runners, this task runner is
/// shared by all tests running in the process. Tests must ensure
/// that all tasks posted to this task runner are executed before
/// the test ends to prevent the task from one test being executed
/// while another test is running. When in doubt, just create a
/// bespoke thread and task running. These cannot be seen by other
/// tests in the process.
///
/// @see `GetThreadTaskRunner`, `CreateNewThread`.
///
/// @return The task runner for the thread the test is running on.
///
fml::RefPtr<fml::TaskRunner> GetCurrentTaskRunner();
//----------------------------------------------------------------------------
/// @brief Creates a new thread, initializes a message loop on it, and,
/// returns its task runner to the unit-test. The message loop is
/// terminated (and its thread joined) when the test ends. This
/// allows tests to create multiple named threads as necessary.
///
/// @param[in] name The name of the OS thread created.
///
/// @return The task runner for the newly created thread.
///
fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name = "");
private:
fml::RefPtr<fml::TaskRunner> current_task_runner_;
std::vector<std::unique_ptr<fml::Thread>> extra_threads_;
FML_DISALLOW_COPY_AND_ASSIGN(ThreadTest);
};
} // namespace testing
} // namespace flutter
#endif // FLUTTER_TESTING_THREAD_TEST_H_