Add rtc::Thread invoke policy.

Policy will allow explicitly specify thread between which invokes are
allowed, or explicitly forbid any invokes.

Change-Id: I360e7cba3ce1c21abd5047c6f175d8c4e0e99c6f
Bug: webrtc:11728
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/177526
Reviewed-by: Tommi <tommi@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31604}
This commit is contained in:
Artem Titov
2020-06-29 17:37:32 +02:00
committed by Commit Bot
parent afeb07030e
commit 26d4f9cd39
4 changed files with 124 additions and 0 deletions

View File

@ -288,6 +288,63 @@ TEST(ThreadTest, Wrap) {
ThreadManager::Instance()->SetCurrentThread(current_thread);
}
#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
TEST(ThreadTest, InvokeToThreadAllowedReturnsTrueWithoutPolicies) {
// Create and start the thread.
auto thread1 = Thread::CreateWithSocketServer();
auto thread2 = Thread::CreateWithSocketServer();
thread1->PostTask(ToQueuedTask(
[&]() { EXPECT_TRUE(thread1->IsInvokeToThreadAllowed(thread2.get())); }));
Thread* th_main = Thread::Current();
th_main->ProcessMessages(100);
}
TEST(ThreadTest, InvokeAllowedWhenThreadsAdded) {
// Create and start the thread.
auto thread1 = Thread::CreateWithSocketServer();
auto thread2 = Thread::CreateWithSocketServer();
auto thread3 = Thread::CreateWithSocketServer();
auto thread4 = Thread::CreateWithSocketServer();
thread1->AllowInvokesToThread(thread2.get());
thread1->AllowInvokesToThread(thread3.get());
thread1->PostTask(ToQueuedTask([&]() {
EXPECT_TRUE(thread1->IsInvokeToThreadAllowed(thread2.get()));
EXPECT_TRUE(thread1->IsInvokeToThreadAllowed(thread3.get()));
EXPECT_FALSE(thread1->IsInvokeToThreadAllowed(thread4.get()));
}));
Thread* th_main = Thread::Current();
th_main->ProcessMessages(100);
}
TEST(ThreadTest, InvokesDisallowedWhenDisallowAnyInvoke) {
// Create and start the thread.
auto thread1 = Thread::CreateWithSocketServer();
auto thread2 = Thread::CreateWithSocketServer();
thread1->DisallowAnyInvoke();
thread1->PostTask(ToQueuedTask([&]() {
EXPECT_FALSE(thread1->IsInvokeToThreadAllowed(thread2.get()));
}));
Thread* th_main = Thread::Current();
th_main->ProcessMessages(100);
}
#endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
TEST(ThreadTest, InvokesAllowedByDefault) {
// Create and start the thread.
auto thread1 = Thread::CreateWithSocketServer();
auto thread2 = Thread::CreateWithSocketServer();
thread1->PostTask(ToQueuedTask(
[&]() { EXPECT_TRUE(thread1->IsInvokeToThreadAllowed(thread2.get())); }));
Thread* th_main = Thread::Current();
th_main->ProcessMessages(100);
}
TEST(ThreadTest, Invoke) {
// Create and start the thread.
auto thread = Thread::CreateWithSocketServer();