Delete FunctorMessageHandler, in tests use alternative ways to post functors
Bug: None Change-Id: I22c7623300c2370269651b67a919a3c4a2ff3926 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/161080 Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29969}
This commit is contained in:
parent
f2c0818fa2
commit
b877e71c41
@ -476,11 +476,9 @@ class ChannelTest : public ::testing::Test, public sigslot::has_slots<> {
|
|||||||
public:
|
public:
|
||||||
template <class FunctorT>
|
template <class FunctorT>
|
||||||
explicit ScopedCallThread(FunctorT&& functor)
|
explicit ScopedCallThread(FunctorT&& functor)
|
||||||
: thread_(rtc::Thread::Create()),
|
: thread_(rtc::Thread::Create()) {
|
||||||
task_(new rtc::FunctorMessageHandler<void, FunctorT>(
|
|
||||||
std::forward<FunctorT>(functor))) {
|
|
||||||
thread_->Start();
|
thread_->Start();
|
||||||
thread_->Post(RTC_FROM_HERE, task_.get());
|
thread_->PostTask(RTC_FROM_HERE, std::forward<FunctorT>(functor));
|
||||||
}
|
}
|
||||||
|
|
||||||
~ScopedCallThread() { thread_->Stop(); }
|
~ScopedCallThread() { thread_->Stop(); }
|
||||||
@ -489,7 +487,6 @@ class ChannelTest : public ::testing::Test, public sigslot::has_slots<> {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<rtc::Thread> thread_;
|
std::unique_ptr<rtc::Thread> thread_;
|
||||||
std::unique_ptr<rtc::MessageHandler> task_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool CodecMatches(const typename T::Codec& c1, const typename T::Codec& c2) {
|
bool CodecMatches(const typename T::Codec& c1, const typename T::Codec& c2) {
|
||||||
|
|||||||
@ -1234,6 +1234,7 @@ if (rtc_include_tests) {
|
|||||||
"../test:test_main",
|
"../test:test_main",
|
||||||
"../test:test_support",
|
"../test:test_support",
|
||||||
"memory:unittests",
|
"memory:unittests",
|
||||||
|
"task_utils:to_queued_task",
|
||||||
"third_party/base64",
|
"third_party/base64",
|
||||||
"third_party/sigslot",
|
"third_party/sigslot",
|
||||||
"//third_party/abseil-cpp/absl/base:core_headers",
|
"//third_party/abseil-cpp/absl/base:core_headers",
|
||||||
@ -1385,6 +1386,7 @@ if (rtc_include_tests) {
|
|||||||
"../test:test_support",
|
"../test:test_support",
|
||||||
"memory:fifo_buffer",
|
"memory:fifo_buffer",
|
||||||
"synchronization:synchronization_unittests",
|
"synchronization:synchronization_unittests",
|
||||||
|
"task_utils:to_queued_task",
|
||||||
"third_party/sigslot",
|
"third_party/sigslot",
|
||||||
"//third_party/abseil-cpp/absl/algorithm:container",
|
"//third_party/abseil-cpp/absl/algorithm:container",
|
||||||
"//third_party/abseil-cpp/absl/memory",
|
"//third_party/abseil-cpp/absl/memory",
|
||||||
|
|||||||
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "api/function_view.h"
|
||||||
#include "rtc_base/constructor_magic.h"
|
#include "rtc_base/constructor_magic.h"
|
||||||
#include "rtc_base/system/rtc_export.h"
|
#include "rtc_base/system/rtc_export.h"
|
||||||
|
|
||||||
@ -21,7 +22,6 @@ namespace rtc {
|
|||||||
struct Message;
|
struct Message;
|
||||||
|
|
||||||
// Messages get dispatched to a MessageHandler
|
// Messages get dispatched to a MessageHandler
|
||||||
|
|
||||||
class RTC_EXPORT MessageHandler {
|
class RTC_EXPORT MessageHandler {
|
||||||
public:
|
public:
|
||||||
virtual ~MessageHandler();
|
virtual ~MessageHandler();
|
||||||
@ -34,38 +34,6 @@ class RTC_EXPORT MessageHandler {
|
|||||||
RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandler);
|
RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandler);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper class to facilitate executing a functor on a thread.
|
|
||||||
template <class ReturnT, class FunctorT>
|
|
||||||
class FunctorMessageHandler : public MessageHandler {
|
|
||||||
public:
|
|
||||||
explicit FunctorMessageHandler(FunctorT&& functor)
|
|
||||||
: functor_(std::forward<FunctorT>(functor)) {}
|
|
||||||
virtual void OnMessage(Message* msg) { result_ = functor_(); }
|
|
||||||
const ReturnT& result() const { return result_; }
|
|
||||||
|
|
||||||
// Returns moved result. Should not call result() or MoveResult() again
|
|
||||||
// after this.
|
|
||||||
ReturnT MoveResult() { return std::move(result_); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
FunctorT functor_;
|
|
||||||
ReturnT result_;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Specialization for ReturnT of void.
|
|
||||||
template <class FunctorT>
|
|
||||||
class FunctorMessageHandler<void, FunctorT> : public MessageHandler {
|
|
||||||
public:
|
|
||||||
explicit FunctorMessageHandler(FunctorT&& functor)
|
|
||||||
: functor_(std::forward<FunctorT>(functor)) {}
|
|
||||||
virtual void OnMessage(Message* msg) { functor_(); }
|
|
||||||
void result() const {}
|
|
||||||
void MoveResult() {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
FunctorT functor_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace rtc
|
} // namespace rtc
|
||||||
|
|
||||||
#endif // RTC_BASE_MESSAGE_HANDLER_H_
|
#endif // RTC_BASE_MESSAGE_HANDLER_H_
|
||||||
|
|||||||
@ -20,12 +20,15 @@
|
|||||||
#include "rtc_base/null_socket_server.h"
|
#include "rtc_base/null_socket_server.h"
|
||||||
#include "rtc_base/ref_count.h"
|
#include "rtc_base/ref_count.h"
|
||||||
#include "rtc_base/ref_counted_object.h"
|
#include "rtc_base/ref_counted_object.h"
|
||||||
|
#include "rtc_base/task_utils/to_queued_task.h"
|
||||||
#include "rtc_base/thread.h"
|
#include "rtc_base/thread.h"
|
||||||
#include "rtc_base/time_utils.h"
|
#include "rtc_base/time_utils.h"
|
||||||
|
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
using ::webrtc::ToQueuedTask;
|
||||||
|
|
||||||
class MessageQueueTest : public ::testing::Test, public MessageQueue {
|
class MessageQueueTest : public ::testing::Test, public MessageQueue {
|
||||||
public:
|
public:
|
||||||
MessageQueueTest() : MessageQueue(SocketServer::CreateDefault(), true) {}
|
MessageQueueTest() : MessageQueue(SocketServer::CreateDefault(), true) {}
|
||||||
@ -132,26 +135,25 @@ TEST(MessageQueueManager, ProcessAllMessageQueues) {
|
|||||||
b->Start();
|
b->Start();
|
||||||
|
|
||||||
volatile int messages_processed = 0;
|
volatile int messages_processed = 0;
|
||||||
FunctorMessageHandler<void, std::function<void()>> incrementer(
|
auto incrementer = [&messages_processed,
|
||||||
[&messages_processed, &entered_process_all_message_queues] {
|
&entered_process_all_message_queues] {
|
||||||
// Wait for event as a means to ensure Increment doesn't occur outside
|
// Wait for event as a means to ensure Increment doesn't occur outside
|
||||||
// of ProcessAllMessageQueues. The event is set by a message posted to
|
// of ProcessAllMessageQueues. The event is set by a message posted to
|
||||||
// the main thread, which is guaranteed to be handled inside
|
// the main thread, which is guaranteed to be handled inside
|
||||||
// ProcessAllMessageQueues.
|
// ProcessAllMessageQueues.
|
||||||
entered_process_all_message_queues.Wait(Event::kForever);
|
entered_process_all_message_queues.Wait(Event::kForever);
|
||||||
AtomicOps::Increment(&messages_processed);
|
AtomicOps::Increment(&messages_processed);
|
||||||
});
|
};
|
||||||
FunctorMessageHandler<void, std::function<void()>> event_signaler(
|
auto event_signaler = [&entered_process_all_message_queues] {
|
||||||
[&entered_process_all_message_queues] {
|
|
||||||
entered_process_all_message_queues.Set();
|
entered_process_all_message_queues.Set();
|
||||||
});
|
};
|
||||||
|
|
||||||
// Post messages (both delayed and non delayed) to both threads.
|
// Post messages (both delayed and non delayed) to both threads.
|
||||||
a->Post(RTC_FROM_HERE, &incrementer);
|
a->PostTask(ToQueuedTask(incrementer));
|
||||||
b->Post(RTC_FROM_HERE, &incrementer);
|
b->PostTask(ToQueuedTask(incrementer));
|
||||||
a->PostDelayed(RTC_FROM_HERE, 0, &incrementer);
|
a->PostDelayedTask(ToQueuedTask(incrementer), 0);
|
||||||
b->PostDelayed(RTC_FROM_HERE, 0, &incrementer);
|
b->PostDelayedTask(ToQueuedTask(incrementer), 0);
|
||||||
rtc::Thread::Current()->Post(RTC_FROM_HERE, &event_signaler);
|
rtc::Thread::Current()->PostTask(ToQueuedTask(event_signaler));
|
||||||
|
|
||||||
MessageQueueManager::ProcessAllMessageQueuesForTesting();
|
MessageQueueManager::ProcessAllMessageQueuesForTesting();
|
||||||
EXPECT_EQ(4, AtomicOps::AcquireLoad(&messages_processed));
|
EXPECT_EQ(4, AtomicOps::AcquireLoad(&messages_processed));
|
||||||
@ -172,23 +174,21 @@ TEST(MessageQueueManager, ProcessAllMessageQueuesWithClearedQueue) {
|
|||||||
auto t = Thread::CreateWithSocketServer();
|
auto t = Thread::CreateWithSocketServer();
|
||||||
t->Start();
|
t->Start();
|
||||||
|
|
||||||
FunctorMessageHandler<void, std::function<void()>> clearer(
|
auto clearer = [&entered_process_all_message_queues] {
|
||||||
[&entered_process_all_message_queues] {
|
|
||||||
// Wait for event as a means to ensure Clear doesn't occur outside of
|
// Wait for event as a means to ensure Clear doesn't occur outside of
|
||||||
// ProcessAllMessageQueues. The event is set by a message posted to the
|
// ProcessAllMessageQueues. The event is set by a message posted to the
|
||||||
// main thread, which is guaranteed to be handled inside
|
// main thread, which is guaranteed to be handled inside
|
||||||
// ProcessAllMessageQueues.
|
// ProcessAllMessageQueues.
|
||||||
entered_process_all_message_queues.Wait(Event::kForever);
|
entered_process_all_message_queues.Wait(Event::kForever);
|
||||||
rtc::Thread::Current()->Clear(nullptr);
|
rtc::Thread::Current()->Clear(nullptr);
|
||||||
});
|
};
|
||||||
FunctorMessageHandler<void, std::function<void()>> event_signaler(
|
auto event_signaler = [&entered_process_all_message_queues] {
|
||||||
[&entered_process_all_message_queues] {
|
|
||||||
entered_process_all_message_queues.Set();
|
entered_process_all_message_queues.Set();
|
||||||
});
|
};
|
||||||
|
|
||||||
// Post messages (both delayed and non delayed) to both threads.
|
// Post messages (both delayed and non delayed) to both threads.
|
||||||
t->Post(RTC_FROM_HERE, &clearer);
|
t->PostTask(RTC_FROM_HERE, clearer);
|
||||||
rtc::Thread::Current()->Post(RTC_FROM_HERE, &event_signaler);
|
rtc::Thread::Current()->PostTask(RTC_FROM_HERE, event_signaler);
|
||||||
MessageQueueManager::ProcessAllMessageQueuesForTesting();
|
MessageQueueManager::ProcessAllMessageQueuesForTesting();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,6 +18,7 @@
|
|||||||
#include "rtc_base/helpers.h"
|
#include "rtc_base/helpers.h"
|
||||||
#include "rtc_base/location.h"
|
#include "rtc_base/location.h"
|
||||||
#include "rtc_base/message_handler.h"
|
#include "rtc_base/message_handler.h"
|
||||||
|
#include "rtc_base/task_utils/to_queued_task.h"
|
||||||
#include "rtc_base/thread.h"
|
#include "rtc_base/thread.h"
|
||||||
#include "test/gtest.h"
|
#include "test/gtest.h"
|
||||||
|
|
||||||
@ -269,11 +270,10 @@ TEST(FakeClock, SettingTimeWakesThreads) {
|
|||||||
|
|
||||||
// Post an event that won't be executed for 10 seconds.
|
// Post an event that won't be executed for 10 seconds.
|
||||||
Event message_handler_dispatched;
|
Event message_handler_dispatched;
|
||||||
auto functor = [&message_handler_dispatched] {
|
worker->PostDelayedTask(webrtc::ToQueuedTask([&message_handler_dispatched] {
|
||||||
message_handler_dispatched.Set();
|
message_handler_dispatched.Set();
|
||||||
};
|
}),
|
||||||
FunctorMessageHandler<void, decltype(functor)> handler(std::move(functor));
|
/*milliseconds=*/60000);
|
||||||
worker->PostDelayed(RTC_FROM_HERE, 60000, &handler);
|
|
||||||
|
|
||||||
// Wait for a bit for the worker thread to be started and enter its socket
|
// Wait for a bit for the worker thread to be started and enter its socket
|
||||||
// select(). Otherwise this test would be trivial since the worker thread
|
// select(). Otherwise this test would be trivial since the worker thread
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user