Set up a new rtc::Thread instance per test.
Several tests leave pending tasks behind after executing, which may affect the state of subsequent tests. This CL isolates each test in the sense that a dedicated Thread instance is created per test and then pending tasks are flushed and the Thread instance deleted. Down the line we may want to improve on this and flag those tests that leave pending tasks/timers etc. Change-Id: Ibaf3719a9974c57ac2169edca0e2a06a9ea6c78f Bug: webrtc:11574 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/175132 Commit-Queue: Tommi <tommi@webrtc.org> Reviewed-by: Artem Titov <titovartem@webrtc.org> Cr-Commit-Position: refs/heads/master@{#31268}
This commit is contained in:
@ -100,6 +100,40 @@ namespace {
|
||||
|
||||
class TestMainImpl : public TestMain {
|
||||
public:
|
||||
// In order to set up a fresh rtc::Thread state for each test and avoid
|
||||
// accidentally carrying over pending tasks that might be sent from one test
|
||||
// and executed while another test is running, we inject a TestListener
|
||||
// that sets up a new rtc::Thread instance for the main thread, per test.
|
||||
class TestListener : public ::testing::EmptyTestEventListener {
|
||||
public:
|
||||
TestListener() = default;
|
||||
|
||||
private:
|
||||
void OnTestStart(const ::testing::TestInfo& test_info) override {
|
||||
// Ensure that main thread gets wrapped as an rtc::Thread.
|
||||
// TODO(bugs.webrtc.org/9714): It might be better to avoid wrapping the
|
||||
// main thread, or leave it to individual tests that need it. But as long
|
||||
// as we have automatic thread wrapping, we need this to avoid that some
|
||||
// other random thread (which one depending on which tests are run) gets
|
||||
// automatically wrapped.
|
||||
thread_ = rtc::Thread::CreateWithSocketServer();
|
||||
thread_->WrapCurrent();
|
||||
RTC_DCHECK_EQ(rtc::Thread::Current(), thread_.get());
|
||||
}
|
||||
|
||||
void OnTestEnd(const ::testing::TestInfo& test_info) override {
|
||||
// Terminate the message loop. Note that if the test failed to clean
|
||||
// up pending messages, this may execute part of the test. Ideally we
|
||||
// should print a warning message here, or even fail the test if it leaks.
|
||||
thread_->Quit(); // Signal quit.
|
||||
thread_->Run(); // Flush + process Quit signal.
|
||||
thread_->UnwrapCurrent();
|
||||
thread_ = nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<rtc::Thread> thread_;
|
||||
};
|
||||
|
||||
int Init(int* argc, char* argv[]) override {
|
||||
::testing::InitGoogleMock(argc, argv);
|
||||
absl::ParseCommandLine(*argc, argv);
|
||||
@ -134,14 +168,7 @@ class TestMainImpl : public TestMain {
|
||||
rtc::InitializeSSL();
|
||||
rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
|
||||
|
||||
// Ensure that main thread gets wrapped as an rtc::Thread.
|
||||
// TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
|
||||
// thread, or leave it to individual tests that need it. But as long as we
|
||||
// have automatic thread wrapping, we need this to avoid that some other
|
||||
// random thread (which one depending on which tests are run) gets
|
||||
// automatically wrapped.
|
||||
rtc::ThreadManager::Instance()->WrapCurrentThread();
|
||||
RTC_CHECK(rtc::Thread::Current());
|
||||
::testing::UnitTest::GetInstance()->listeners().Append(new TestListener());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user