base::Clock and base::TickClock are injection points for testing to override base::Time::Now() and base::TimeTicks::Now().
In common usage in Chromium:
- An injection class takes the ownership of the clock object, and
use it whenever Now() is needed.
- An owner of the injection target creates a base::DefaultClock or
base::DefaultTickClock instance, and passes it to the target
on the production code.
- A test class creates a SimpleTestClock or SimpleTestTickClock,
keeps an raw pointer in the test class, and passes the instance
to the injection target.
The problems here are:
- One clock instance per injection target is sometimes excessive.
Especially when the injection target is small. Even if the injection
targets share single clock instance, the right owner of the shared
instance may not always exist.
- A test holds raw pointer to an injected clock instance to manipulate
the clock for testing, but the ownership itself is passed to the
target. That looks hacky.
This ownership structure can be rewritten more cleaner way if
- The injection target always takes a raw pointer to the clock instance.
- In production code, the target takes a shared singleton instance of
base::DefaultClock or base::DefaultTickClock.
- In test code, the test class owns a SimpleTestClock or SimpleTestTickClock,
and passes a raw pointer to the target, rather than the ownership.
Comment 1 by tzik@chromium.org
, Nov 28 2017Status: Available (was: Untriaged)