File: thread_wrapper_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (226 lines) | stat: -rw-r--r-- 7,733 bytes parent folder | download | duplicates (3)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webrtc/thread_wrapper.h"

#include <stdint.h>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/api/task_queue/task_queue_factory.h"
#include "third_party/webrtc/api/task_queue/task_queue_test.h"
#include "third_party/webrtc_overrides/metronome_source.h"
#include "third_party/webrtc_overrides/test/metronome_like_task_queue_test.h"
#include "third_party/webrtc_overrides/timer_based_tick_provider.h"

namespace webrtc {
namespace {

using ::blink::MetronomeLikeTaskQueueTest;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::MockFunction;

constexpr TimeDelta kTestDelay1 = TimeDelta::Millis(10);
constexpr TimeDelta kTestDelay2 = TimeDelta::Millis(20);
constexpr TimeDelta kTestDelay3 = TimeDelta::Millis(30);
constexpr TimeDelta kTestDelay4 = TimeDelta::Millis(40);
constexpr base::TimeDelta kMaxTestDelay = base::Milliseconds(40);

class ThreadWrapperTest : public testing::Test {
 public:
  // This method is used by the BlockingCallDuringBlockingCall test.
  // It sends message to the main thread synchronously using BlockingCall().
  void PingMainThread() {
    MockFunction<void()> handler;
    EXPECT_CALL(handler, Call);
    thread_->BlockingCall(handler.AsStdFunction());
  }

 protected:
  ThreadWrapperTest() : thread_(nullptr) {}

  void SetUp() override {
    ThreadWrapper::EnsureForCurrentMessageLoop();
    thread_ = ThreadWrapper::current();
  }

  // ThreadWrapper destroys itself when |message_loop_| is destroyed.
  base::test::SingleThreadTaskEnvironment task_environment_;
  raw_ptr<ThreadWrapper> thread_;
};

TEST_F(ThreadWrapperTest, PostTask) {
  MockFunction<void()> handler1;
  MockFunction<void()> handler2;
  MockFunction<void()> handler3;
  MockFunction<void()> handler4;

  thread_->PostTask(handler1.AsStdFunction());
  thread_->PostTask(handler2.AsStdFunction());
  thread_->PostTask(handler3.AsStdFunction());
  thread_->PostTask(handler4.AsStdFunction());

  InSequence in_seq;
  EXPECT_CALL(handler1, Call);
  EXPECT_CALL(handler2, Call);
  EXPECT_CALL(handler3, Call);
  EXPECT_CALL(handler4, Call);

  base::RunLoop().RunUntilIdle();
}

TEST_F(ThreadWrapperTest, PostDelayedTask) {
  MockFunction<void()> handler1;
  MockFunction<void()> handler2;
  MockFunction<void()> handler3;
  MockFunction<void()> handler4;

  thread_->PostDelayedHighPrecisionTask(handler1.AsStdFunction(), kTestDelay1);
  thread_->PostDelayedHighPrecisionTask(handler2.AsStdFunction(), kTestDelay2);
  thread_->PostDelayedHighPrecisionTask(handler3.AsStdFunction(), kTestDelay3);
  thread_->PostDelayedHighPrecisionTask(handler4.AsStdFunction(), kTestDelay4);

  InSequence in_seq;
  EXPECT_CALL(handler1, Call);
  EXPECT_CALL(handler2, Call);
  EXPECT_CALL(handler3, Call);
  EXPECT_CALL(handler4, Call);

  base::RunLoop run_loop;
  task_environment_.GetMainThreadTaskRunner()->PostDelayedTask(
      FROM_HERE, run_loop.QuitClosure(), kMaxTestDelay);
  run_loop.Run();
}

// Verify that BlockingCall() calls handler synchronously when called on the
// same thread.
TEST_F(ThreadWrapperTest, BlockingCallSameThread) {
  MockFunction<void()> handler;
  EXPECT_CALL(handler, Call);
  thread_->BlockingCall(handler.AsStdFunction());
}

void InitializeWrapperForNewThread(ThreadWrapper** thread,
                                   base::WaitableEvent* done_event) {
  ThreadWrapper::EnsureForCurrentMessageLoop();
  ThreadWrapper::current()->set_send_allowed(true);
  *thread = ThreadWrapper::current();
  done_event->Signal();
}

// Verify that BlockingCall() calls handler synchronously when called for a
// different thread.
TEST_F(ThreadWrapperTest, BlockingCallToOtherThread) {
  ThreadWrapper::current()->set_send_allowed(true);

  base::Thread second_thread("adWrapperTest");
  second_thread.Start();

  base::WaitableEvent initialized_event(
      base::WaitableEvent::ResetPolicy::MANUAL,
      base::WaitableEvent::InitialState::NOT_SIGNALED);
  ThreadWrapper* target;
  second_thread.task_runner()->PostTask(
      FROM_HERE, base::BindOnce(&InitializeWrapperForNewThread, &target,
                                &initialized_event));
  initialized_event.Wait();

  ASSERT_TRUE(target != nullptr);

  MockFunction<void()> handler;
  EXPECT_CALL(handler, Call);
  target->BlockingCall(handler.AsStdFunction());
}

// Verify that thread handles BlockingCall() while another BlockingCall() is
// pending. The test creates second thread and BlockingCall()s
// to that thread. handler calls PingMainThread() on the BlockingCall which
// tries to BlockingCall() to the main thread.
TEST_F(ThreadWrapperTest, BlockingCallDuringBlockingCall) {
  ThreadWrapper::current()->set_send_allowed(true);

  base::Thread second_thread("adWrapperTest");
  second_thread.Start();

  base::WaitableEvent initialized_event(
      base::WaitableEvent::ResetPolicy::MANUAL,
      base::WaitableEvent::InitialState::NOT_SIGNALED);
  ThreadWrapper* target;
  second_thread.task_runner()->PostTask(
      FROM_HERE, base::BindOnce(&InitializeWrapperForNewThread, &target,
                                &initialized_event));
  initialized_event.Wait();

  ASSERT_TRUE(target != nullptr);

  MockFunction<void()> handler;
  EXPECT_CALL(handler, Call)
      .WillOnce(Invoke(this, &ThreadWrapperTest::PingMainThread));
  target->BlockingCall(handler.AsStdFunction());
}

// Provider needed for the MetronomeLikeTaskQueueTest suite.
class ThreadWrapperProvider : public blink::MetronomeLikeTaskQueueProvider {
 public:
  void Initialize() override {
    ThreadWrapper::EnsureForCurrentMessageLoop();
    thread_ = webrtc::Thread::Current();
  }

  base::TimeDelta DeltaToNextTick() const override {
    base::TimeTicks now = base::TimeTicks::Now();
    return blink::TimerBasedTickProvider::TimeSnappedToNextTick(
               now, blink::TimerBasedTickProvider::kDefaultPeriod) -
           now;
  }
  base::TimeDelta MetronomeTick() const override {
    return blink::TimerBasedTickProvider::kDefaultPeriod;
  }
  webrtc::TaskQueueBase* TaskQueue() const override { return thread_; }

 private:
  // ThreadWrapper destroys itself when |message_loop_| is destroyed.
  raw_ptr<webrtc::Thread> thread_;
};

// Instantiate suite to run all tests defined in
// third_party/webrtc_overrides/test/metronome_like_task_queue_test.h
INSTANTIATE_TEST_SUITE_P(
    ThreadWrapper,
    MetronomeLikeTaskQueueTest,
    ::testing::Values(std::make_unique<ThreadWrapperProvider>));

class ThreadWrapperTaskQueueFactory : public TaskQueueFactory {
 public:
  std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
      std::string_view name,
      Priority priority) const override {
    std::unique_ptr<webrtc::Thread> thread = webrtc::Thread::Create();
    thread->Start();
    return std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter>(
        thread.release());
  }
};

std::unique_ptr<TaskQueueFactory> CreateTaskQueueFactory(
    const webrtc::FieldTrialsView*) {
  return std::make_unique<ThreadWrapperTaskQueueFactory>();
}

// Instantiate suite to run all tests defined in
// //third_party/webrtc/api/task_queue:task_queue_test.
INSTANTIATE_TEST_SUITE_P(ThreadWrapper,
                         TaskQueueTest,
                         ::testing::Values(CreateTaskQueueFactory));

}  // namespace

}  // namespace webrtc