TEST(BasicThreadTest, RealTimings)
{
	MockThread m;
	m.start(nullptr);

	while (m.getCpuTime() < 1e-6);
	EXPECT_GT(m.getCpuTime(), 1e-6);
	EXPECT_GT(m.getUpdateCount(), 0u);

	// Ask the manager to idle for a while, just the time for us to reset the timer and check right after
	// what the timer contains (the delay between the reset and the checks could trigger a race condition).
	// Asking the thread to idle suppress this race condition.
	m.setIdle(true);

	// Reset the timer (=> no more frames in the timer queue)
	m.resetCpuTimeAndUpdateCount();
	EXPECT_NEAR(0.0, m.getCpuTime(), 1e-9);
	EXPECT_EQ(m.getUpdateCount(), 0u);

	// Resume the thread loop update.
	m.setIdle(false);

	while (m.getCpuTime() < 1e-6);
	EXPECT_GT(m.getCpuTime(), 1e-6);
	EXPECT_GT(m.getUpdateCount(), 0u);

	m.stop();
}
TEST(BasicThreadTest, Stop)
{
	MockThread m;
	m.start(nullptr);

	boost::this_thread::sleep(boost::posix_time::milliseconds(100));

	EXPECT_TRUE(m.isRunning());
	m.stop();

	EXPECT_TRUE(m.didBeforeStop);
	EXPECT_FALSE(m.isRunning());
}
TEST(BasicThreadTest, StopWithoutSleep)
{
	for (int i = 0;  i < 10;  ++i)
	{
		MockThread m;
		m.count = 1000000;
		m.start(nullptr);

		// Stopping right away should not create a race condition.
		m.stop();

		EXPECT_TRUE(m.didBeforeStop);
		EXPECT_FALSE(m.isRunning());
	}
}
TEST(BasicThreadTest, RunTimeManagement)
{
	MockThread m;
	EXPECT_EQ(-1, m.count);
	std::shared_ptr<Barrier> barrier = std::make_shared<Barrier>(2);
	EXPECT_FALSE(m.didInitialize);
	EXPECT_FALSE(m.didStartUp);
	EXPECT_FALSE(m.isRunning());
	m.start(barrier);
	boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	EXPECT_TRUE(m.isRunning());
	EXPECT_TRUE(m.didInitialize);
	EXPECT_FALSE(m.didStartUp);
	barrier->wait(true);
	boost::this_thread::sleep(boost::posix_time::milliseconds(100));
	EXPECT_TRUE(m.didInitialize);
	EXPECT_TRUE(m.didStartUp);
	barrier->wait(true);
	boost::this_thread::sleep(boost::posix_time::milliseconds(100));

	m.stop();
}