File: executors_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (81 lines) | stat: -rw-r--r-- 2,631 bytes parent folder | download | duplicates (6)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cronet_c.h"

#include "base/check.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "components/cronet/native/test/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class ExecutorsTest : public ::testing::Test {
 public:
  ExecutorsTest() = default;

  ExecutorsTest(const ExecutorsTest&) = delete;
  ExecutorsTest& operator=(const ExecutorsTest&) = delete;

  ~ExecutorsTest() override = default;

 protected:
  static void TestRunnable_Run(Cronet_RunnablePtr self);
  bool runnable_called() const { return runnable_called_; }

  // Provide a task environment for use by TestExecutor instances. Do not
  // initialize the ThreadPool as this is done by the Cronet_Engine
  base::test::SingleThreadTaskEnvironment task_environment_;

 private:
  void set_runnable_called(bool value) { runnable_called_ = value; }

  bool runnable_called_ = false;
};

// App implementation of Cronet_Executor methods.
void TestExecutor_Execute(Cronet_ExecutorPtr self, Cronet_RunnablePtr command) {
  CHECK(self);
  Cronet_Runnable_Run(command);
  Cronet_Runnable_Destroy(command);
}

// Implementation of TestRunnable methods.
// static
void ExecutorsTest::TestRunnable_Run(Cronet_RunnablePtr self) {
  CHECK(self);
  Cronet_ClientContext context = Cronet_Runnable_GetClientContext(self);
  ExecutorsTest* test = static_cast<ExecutorsTest*>(context);
  CHECK(test);
  test->set_runnable_called(true);
}

// Test that custom Executor defined by the app runs the runnable.
TEST_F(ExecutorsTest, TestCustom) {
  ASSERT_FALSE(runnable_called());
  Cronet_RunnablePtr runnable =
      Cronet_Runnable_CreateWith(ExecutorsTest::TestRunnable_Run);
  Cronet_Runnable_SetClientContext(runnable, this);
  Cronet_ExecutorPtr executor =
      Cronet_Executor_CreateWith(TestExecutor_Execute);
  Cronet_Executor_Execute(executor, runnable);
  Cronet_Executor_Destroy(executor);
  base::RunLoop().RunUntilIdle();
  ASSERT_TRUE(runnable_called());
}

// Test that cronet::test::TestExecutor runs the runnable.
TEST_F(ExecutorsTest, TestTestExecutor) {
  ASSERT_FALSE(runnable_called());
  Cronet_RunnablePtr runnable = Cronet_Runnable_CreateWith(TestRunnable_Run);
  Cronet_Runnable_SetClientContext(runnable, this);
  Cronet_ExecutorPtr executor = cronet::test::CreateTestExecutor();
  Cronet_Executor_Execute(executor, runnable);
  Cronet_Executor_Destroy(executor);
  base::RunLoop().RunUntilIdle();
  ASSERT_TRUE(runnable_called());
}

}  // namespace