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
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356

base / test / test_mock_time_task_runner_unittest.cc [blame]

// 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 "base/test/test_mock_time_task_runner.h"

#include "base/cancelable_callback.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

// Basic usage should work the same from default and bound
// TestMockTimeTaskRunners.
TEST(TestMockTimeTaskRunnerTest, Basic) {
  static constexpr TestMockTimeTaskRunner::Type kTestCases[] = {
      TestMockTimeTaskRunner::Type::kStandalone,
      TestMockTimeTaskRunner::Type::kBoundToThread};

  for (auto type : kTestCases) {
    SCOPED_TRACE(static_cast<int>(type));

    auto mock_time_task_runner = MakeRefCounted<TestMockTimeTaskRunner>(type);
    int counter = 0;

    mock_time_task_runner->PostTask(
        FROM_HERE, base::BindOnce([](int* counter) { *counter += 1; },
                                  Unretained(&counter)));
    mock_time_task_runner->PostTask(
        FROM_HERE, base::BindOnce([](int* counter) { *counter += 32; },
                                  Unretained(&counter)));
    mock_time_task_runner->PostDelayedTask(
        FROM_HERE,
        base::BindOnce([](int* counter) { *counter += 256; },
                       Unretained(&counter)),
        Seconds(3));
    mock_time_task_runner->PostDelayedTask(
        FROM_HERE,
        base::BindOnce([](int* counter) { *counter += 64; },
                       Unretained(&counter)),
        Seconds(1));
    mock_time_task_runner->PostDelayedTask(
        FROM_HERE,
        base::BindOnce([](int* counter) { *counter += 1024; },
                       Unretained(&counter)),
        Minutes(20));
    mock_time_task_runner->PostDelayedTask(
        FROM_HERE,
        base::BindOnce([](int* counter) { *counter += 4096; },
                       Unretained(&counter)),
        Days(20));

    int expected_value = 0;
    EXPECT_EQ(expected_value, counter);
    mock_time_task_runner->RunUntilIdle();
    expected_value += 1;
    expected_value += 32;
    EXPECT_EQ(expected_value, counter);

    mock_time_task_runner->RunUntilIdle();
    EXPECT_EQ(expected_value, counter);

    mock_time_task_runner->FastForwardBy(Seconds(1));
    expected_value += 64;
    EXPECT_EQ(expected_value, counter);

    mock_time_task_runner->FastForwardBy(Seconds(5));
    expected_value += 256;
    EXPECT_EQ(expected_value, counter);

    mock_time_task_runner->FastForwardUntilNoTasksRemain();
    expected_value += 1024;
    expected_value += 4096;
    EXPECT_EQ(expected_value, counter);
  }
}

// A default TestMockTimeTaskRunner shouldn't result in a thread association.
TEST(TestMockTimeTaskRunnerTest, DefaultUnbound) {
  auto unbound_mock_time_task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
  EXPECT_FALSE(SingleThreadTaskRunner::HasCurrentDefault());
  EXPECT_FALSE(SequencedTaskRunner::HasCurrentDefault());
  EXPECT_DEATH_IF_SUPPORTED({ RunLoop().RunUntilIdle(); }, "");
}

TEST(TestMockTimeTaskRunnerTest, RunLoopDriveableWhenBound) {
  auto bound_mock_time_task_runner = MakeRefCounted<TestMockTimeTaskRunner>(
      TestMockTimeTaskRunner::Type::kBoundToThread);

  int counter = 0;
  SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce([](int* counter) { *counter += 1; },
                                Unretained(&counter)));
  SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce([](int* counter) { *counter += 32; },
                                Unretained(&counter)));
  SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce([](int* counter) { *counter += 256; },
                     Unretained(&counter)),
      Seconds(3));
  SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce([](int* counter) { *counter += 64; },
                     Unretained(&counter)),
      Seconds(1));
  SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce([](int* counter) { *counter += 1024; },
                     Unretained(&counter)),
      Minutes(20));
  SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce([](int* counter) { *counter += 4096; },
                     Unretained(&counter)),
      Days(20));

  int expected_value = 0;
  EXPECT_EQ(expected_value, counter);
  RunLoop().RunUntilIdle();
  expected_value += 1;
  expected_value += 32;
  EXPECT_EQ(expected_value, counter);

  RunLoop().RunUntilIdle();
  EXPECT_EQ(expected_value, counter);

  {
    RunLoop run_loop;
    SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE, run_loop.QuitClosure(), Seconds(1));
    SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE,
        base::BindOnce([](int* counter) { *counter += 8192; },
                       Unretained(&counter)),
        Seconds(1));

    // The QuitClosure() should be ordered between the 64 and the 8192
    // increments and should preempt the latter.
    run_loop.Run();
    expected_value += 64;
    EXPECT_EQ(expected_value, counter);

    // Running until idle should process the 8192 increment whose delay has
    // expired in the previous Run().
    RunLoop().RunUntilIdle();
    expected_value += 8192;
    EXPECT_EQ(expected_value, counter);
  }

  {
    RunLoop run_loop;
    SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE, run_loop.QuitWhenIdleClosure(), Seconds(5));
    SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE,
        base::BindOnce([](int* counter) { *counter += 16384; },
                       Unretained(&counter)),
        Seconds(5));

    // The QuitWhenIdleClosure() shouldn't preempt equally delayed tasks and as
    // such the 16384 increment should be processed before quitting.
    run_loop.Run();
    expected_value += 256;
    expected_value += 16384;
    EXPECT_EQ(expected_value, counter);
  }

  // Process the remaining tasks (note: do not mimic this elsewhere,
  // TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() is a better API to
  // do this, this is just done here for the purpose of extensively testing the
  // RunLoop approach).
  RunLoop run_loop;
  SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, run_loop.QuitWhenIdleClosure(), Days(50));

  run_loop.Run();
  expected_value += 1024;
  expected_value += 4096;
  EXPECT_EQ(expected_value, counter);
}

TEST(TestMockTimeTaskRunnerTest, AvoidCaptureWhenBound) {
  // Make sure that capturing the active task runner --- which sometimes happens
  // unknowingly due to ThreadsafeObserverList deep within some singleton ---
  // does not keep the entire TestMockTimeTaskRunner alive, as in bound mode
  // that's a RunLoop::Delegate, and leaking that renders any further tests that
  // need RunLoop support unrunnable.
  //
  // (This used to happen when code run from ProcessAllTasksNoLaterThan grabbed
  //  the runner.).
  scoped_refptr<SingleThreadTaskRunner> captured;
  {
    auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>(
        TestMockTimeTaskRunner::Type::kBoundToThread);

    task_runner->PostTask(FROM_HERE, base::BindLambdaForTesting([&] {
                            captured =
                                SingleThreadTaskRunner::GetCurrentDefault();
                          }));
    task_runner->RunUntilIdle();
  }

  {
    // This should not complain about RunLoop::Delegate already existing.
    auto task_runner2 = MakeRefCounted<TestMockTimeTaskRunner>(
        TestMockTimeTaskRunner::Type::kBoundToThread);
  }
}

// Regression test that receiving the quit-when-idle signal when already empty
// works as intended (i.e. that |TestMockTimeTaskRunner::tasks_lock_cv| is
// properly signaled).
TEST(TestMockTimeTaskRunnerTest, RunLoopQuitFromIdle) {
  auto bound_mock_time_task_runner = MakeRefCounted<TestMockTimeTaskRunner>(
      TestMockTimeTaskRunner::Type::kBoundToThread);

  Thread quitting_thread("quitting thread");
  quitting_thread.Start();

  RunLoop run_loop;
  quitting_thread.task_runner()->PostDelayedTask(
      FROM_HERE, run_loop.QuitWhenIdleClosure(), TestTimeouts::tiny_timeout());
  run_loop.Run();
}

TEST(TestMockTimeTaskRunnerTest, TakePendingTasks) {
  auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
  task_runner->PostTask(FROM_HERE, DoNothing());
  EXPECT_TRUE(task_runner->HasPendingTask());
  EXPECT_EQ(1u, task_runner->TakePendingTasks().size());
  EXPECT_FALSE(task_runner->HasPendingTask());
}

TEST(TestMockTimeTaskRunnerTest, CancelPendingTask) {
  auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
  CancelableOnceClosure task1(DoNothing());
  task_runner->PostDelayedTask(FROM_HERE, task1.callback(), Seconds(1));
  EXPECT_TRUE(task_runner->HasPendingTask());
  EXPECT_EQ(1u, task_runner->GetPendingTaskCount());
  EXPECT_EQ(Seconds(1), task_runner->NextPendingTaskDelay());
  task1.Cancel();
  EXPECT_FALSE(task_runner->HasPendingTask());

  CancelableOnceClosure task2(DoNothing());
  task_runner->PostDelayedTask(FROM_HERE, task2.callback(), Seconds(1));
  task2.Cancel();
  EXPECT_EQ(0u, task_runner->GetPendingTaskCount());

  CancelableOnceClosure task3(DoNothing());
  task_runner->PostDelayedTask(FROM_HERE, task3.callback(), Seconds(1));
  task3.Cancel();
  EXPECT_EQ(TimeDelta::Max(), task_runner->NextPendingTaskDelay());

  CancelableOnceClosure task4(DoNothing());
  task_runner->PostDelayedTask(FROM_HERE, task4.callback(), Seconds(1));
  task4.Cancel();
  EXPECT_TRUE(task_runner->TakePendingTasks().empty());
}

TEST(TestMockTimeTaskRunnerTest, NoFastForwardToCancelledTask) {
  auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
  TimeTicks start_time = task_runner->NowTicks();
  CancelableOnceClosure task(DoNothing());
  task_runner->PostDelayedTask(FROM_HERE, task.callback(), Seconds(1));
  EXPECT_EQ(Seconds(1), task_runner->NextPendingTaskDelay());
  task.Cancel();
  task_runner->FastForwardUntilNoTasksRemain();
  EXPECT_EQ(start_time, task_runner->NowTicks());
}

TEST(TestMockTimeTaskRunnerTest, AdvanceMockTickClockDoesNotRunTasks) {
  auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
  TimeTicks start_time = task_runner->NowTicks();
  task_runner->PostTask(FROM_HERE, BindOnce([] { ADD_FAILURE(); }));
  task_runner->PostDelayedTask(FROM_HERE, BindOnce([] { ADD_FAILURE(); }),
                               Seconds(1));

  task_runner->AdvanceMockTickClock(Seconds(3));
  EXPECT_EQ(start_time + Seconds(3), task_runner->NowTicks());
  EXPECT_EQ(2u, task_runner->GetPendingTaskCount());
}

TEST(TestMockTimeTaskRunnerTest, ProcessNextNTasks) {
  auto mock_time_task_runner = MakeRefCounted<TestMockTimeTaskRunner>(
      TestMockTimeTaskRunner::Type::kStandalone);
  int counter = 0;

  mock_time_task_runner->PostTask(
      FROM_HERE, base::BindOnce([](int* counter) { *counter += 1; },
                                Unretained(&counter)));
  mock_time_task_runner->PostTask(
      FROM_HERE, base::BindOnce([](int* counter) { *counter += 32; },
                                Unretained(&counter)));

  CancelableOnceClosure task1(base::BindOnce(
      [](int* counter) { *counter += 16; }, Unretained(&counter)));
  mock_time_task_runner->PostTask(FROM_HERE, task1.callback());

  mock_time_task_runner->PostDelayedTask(
      FROM_HERE,
      base::BindOnce([](int* counter) { *counter += 256; },
                     Unretained(&counter)),
      Seconds(3));
  mock_time_task_runner->PostDelayedTask(
      FROM_HERE,
      base::BindOnce([](int* counter) { *counter += 64; },
                     Unretained(&counter)),
      Seconds(1));
  mock_time_task_runner->PostDelayedTask(
      FROM_HERE,
      base::BindOnce([](int* counter) { *counter += 1024; },
                     Unretained(&counter)),
      Minutes(20));
  mock_time_task_runner->PostDelayedTask(
      FROM_HERE,
      base::BindOnce([](int* counter) { *counter += 4096; },
                     Unretained(&counter)),
      Days(20));
  task1.Cancel();

  int expected_value = 0;
  EXPECT_EQ(expected_value, counter);
  mock_time_task_runner->ProcessNextNTasks(0);
  EXPECT_EQ(expected_value, counter);

  mock_time_task_runner->ProcessNextNTasks(2);
  expected_value += 1;
  expected_value += 32;
  EXPECT_EQ(expected_value, counter);

  // The next task was canceled, so |counter| shouldn't change.
  mock_time_task_runner->ProcessNextNTasks(1);
  EXPECT_EQ(expected_value, counter);

  mock_time_task_runner->ProcessNextNTasks(1);
  expected_value += 64;
  EXPECT_EQ(expected_value, counter);

  mock_time_task_runner->ProcessNextNTasks(-1);
  expected_value += 256;
  expected_value += 1024;
  expected_value += 4096;
  EXPECT_EQ(expected_value, counter);
}
}  // namespace base