New issue
Advanced search Search tips
Note: Color blocks (like or ) mean that a user may not be available. Tooltip shows the reason.

Issue 897975 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Oct 23
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: ----
Pri: 3
Type: Bug



Sign in to add a comment

Restarting timer in tests adds to delay instead of replacing it

Project Member Reported by michae...@chromium.org, Oct 22

Issue description

I'm seeing base::OneShotTimer behave strangely in a test environment using base::TestMockTimeTaskRunner.

Consider this minimal test:

static int count = 0;
void UpdateCount() {
  count++;
}

TEST(TimerTest, Foo) {
  scoped_refptr<base::TestMockTimeTaskRunner> task_runner(
      base::MakeRefCounted<base::TestMockTimeTaskRunner>());

  base::OneShotTimer timer;
  timer.SetTaskRunner(task_runner.get());
  timer.Start(FROM_HERE, base::TimeDelta::FromMinutes(5),
              base::Bind(&UpdateCount));
  timer.Start(FROM_HERE, base::TimeDelta::FromMinutes(15),
              base::Bind(&UpdateCount));

  for (int i = 0; i <= 20; i++) {
    LOG(ERROR) << "time " << i << ": " << count;
    task_runner->FastForwardBy(base::TimeDelta::FromMinutes(1));
  }
}


The callback is called after 20 fast-forwarded minutes instead of 15 fast-forwarded minutes:

time 0: 0
time 1: 0
time 2: 0
time 3: 0
time 4: 0
time 5: 0
time 6: 0
time 7: 0
time 8: 0
time 9: 0
time 10: 0
time 11: 0
time 12: 0
time 13: 0
time 14: 0
time 15: 0
time 16: 0
time 17: 0
time 18: 0
time 19: 0
time 20: 1


In a real environment, the callback would be invoked after 15 minutes.
 
Status: WontFix (was: Assigned)
This is because base::Timer uses a the real clock the recompute the missing delta when the original task fires. Setting Timer's TickClock to TestMockTimeTaskRunner's TickClock would solve this issue.

I'm working towards having ScopedTaskEnvironment::MOCK_TIME also mock "real" time in unit tests to make this easier but even when we get there, TestMockTimeTaskRunner itself will not mock real time.

Sign in to add a comment