Flutter Engine
 
Loading...
Searching...
No Matches
thread_test.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_TESTING_THREAD_TEST_H_
6#define FLUTTER_TESTING_THREAD_TEST_H_
7
8#include <memory>
9#include <string>
10
11#include "flutter/fml/macros.h"
14#include "flutter/fml/thread.h"
15#include "gtest/gtest.h"
16
17namespace flutter::testing {
18
19//------------------------------------------------------------------------------
20/// @brief A fixture that creates threads with running message loops that
21/// are terminated when the test is done (the threads are joined
22/// then as well). While this fixture may be used on it's own, it is
23/// often sub-classed by other fixtures whose functioning requires
24/// threads to be created as necessary.
25///
26class ThreadTest : public ::testing::Test {
27 public:
28 ThreadTest();
29
30 //----------------------------------------------------------------------------
31 /// @brief Get the task runner for the thread that the current unit-test
32 /// is running on. This creates a message loop as necessary.
33 ///
34 /// @attention Unlike all other threads and task runners, this task runner is
35 /// shared by all tests running in the process. Tests must ensure
36 /// that all tasks posted to this task runner are executed before
37 /// the test ends to prevent the task from one test being executed
38 /// while another test is running. When in doubt, just create a
39 /// bespoke thread and task running. These cannot be seen by other
40 /// tests in the process.
41 ///
42 /// @see `GetThreadTaskRunner`, `CreateNewThread`.
43 ///
44 /// @return The task runner for the thread the test is running on.
45 ///
47
48 //----------------------------------------------------------------------------
49 /// @brief Creates a new thread, initializes a message loop on it, and,
50 /// returns its task runner to the unit-test. The message loop is
51 /// terminated (and its thread joined) when the test ends. This
52 /// allows tests to create multiple named threads as necessary.
53 ///
54 /// @param[in] name The name of the OS thread created.
55 ///
56 /// @return The task runner for the newly created thread.
57 ///
58 fml::RefPtr<fml::TaskRunner> CreateNewThread(const std::string& name = "");
59
60 private:
61 fml::RefPtr<fml::TaskRunner> current_task_runner_;
62 std::vector<std::unique_ptr<fml::Thread>> extra_threads_;
63
65};
66
67} // namespace flutter::testing
68
69#endif // FLUTTER_TESTING_THREAD_TEST_H_
A fixture that creates threads with running message loops that are terminated when the test is done (...
Definition thread_test.h:26
fml::RefPtr< fml::TaskRunner > GetCurrentTaskRunner()
Get the task runner for the thread that the current unit-test is running on. This creates a message l...
fml::RefPtr< fml::TaskRunner > CreateNewThread(const std::string &name="")
Creates a new thread, initializes a message loop on it, and, returns its task runner to the unit-test...
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27