5#include "flutter/fml/synchronization/waitable_event.h"
15#include "flutter/fml/macros.h"
16#include "gtest/gtest.h"
31 std::this_thread::sleep_for(
32 std::chrono::nanoseconds(
duration.ToNanoseconds()));
35void EpsilonRandomSleep() {
43TEST(AutoResetWaitableEventTest, Basic) {
44 AutoResetWaitableEvent ev;
45 EXPECT_FALSE(ev.IsSignaledForTest());
49 EXPECT_FALSE(ev.IsSignaledForTest());
51 EXPECT_FALSE(ev.IsSignaledForTest());
55 EXPECT_FALSE(ev.IsSignaledForTest());
57 EXPECT_FALSE(ev.IsSignaledForTest());
59 EXPECT_FALSE(ev.IsSignaledForTest());
63 EXPECT_FALSE(ev.IsSignaledForTest());
65 EXPECT_FALSE(ev.IsSignaledForTest());
68 EXPECT_FALSE(ev.IsSignaledForTest());
71TEST(AutoResetWaitableEventTest, MultipleWaiters) {
72 AutoResetWaitableEvent ev;
74 for (
size_t i = 0u;
i < 5u;
i++) {
75 std::atomic_uint wake_count(0u);
76 std::vector<std::thread> threads;
77 for (
size_t j = 0u; j < 4u; j++) {
78 threads.push_back(std::thread([&ev, &wake_count]() {
79 if (rand() % 2 == 0) {
82 EXPECT_FALSE(ev.WaitWithTimeout(kActionTimeout));
84 wake_count.fetch_add(1u);
93 SleepFor(kTinyTimeout + kTinyTimeout);
95 for (
size_t j = 0u; j < threads.size(); j++) {
96 unsigned old_wake_count = wake_count.load();
97 EXPECT_EQ(j, old_wake_count);
103 while (wake_count.load() == old_wake_count) {
104 SleepFor(kEpsilonTimeout);
107 EXPECT_FALSE(ev.IsSignaledForTest());
111 SleepFor(kEpsilonTimeout);
113 EXPECT_EQ(old_wake_count + 1u, wake_count.load());
115 EXPECT_FALSE(ev.IsSignaledForTest());
120 SleepFor(kEpsilonTimeout);
123 for (
auto& thread : threads) {
133TEST(ManualResetWaitableEventTest, Basic) {
134 ManualResetWaitableEvent ev;
135 EXPECT_FALSE(ev.IsSignaledForTest());
141 EXPECT_FALSE(ev.IsSignaledForTest());
143 EXPECT_FALSE(ev.IsSignaledForTest());
145 EXPECT_FALSE(ev.IsSignaledForTest());
154TEST(ManualResetWaitableEventTest, SignalMultiple) {
155 ManualResetWaitableEvent ev;
157 for (
size_t i = 0u;
i < 10u;
i++) {
158 for (
size_t num_waiters = 1u; num_waiters < 5u; num_waiters++) {
159 std::vector<std::thread> threads;
160 for (
size_t j = 0u; j < num_waiters; j++) {
161 threads.push_back(std::thread([&ev]() {
162 EpsilonRandomSleep();
164 if (rand() % 2 == 0) {
167 EXPECT_FALSE(ev.WaitWithTimeout(kActionTimeout));
172 EpsilonRandomSleep();
178 for (
auto& thread : threads) {
static constexpr TimeDelta FromMilliseconds(int64_t millis)
static constexpr TimeDelta Zero()
TEST(MallocMapping, EmptyContructor)
#define EXPECT_TRUE(handle)