1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <chrono>
6#include <thread>
7
8#include "flutter/fml/build_config.h"
9#include "flutter/fml/synchronization/count_down_latch.h"
10#include "flutter/fml/thread.h"
11#include "flutter/testing/testing.h"
12
13namespace fml {
14
15TEST(CountDownLatchTest, CanWaitOnZero) {
16 CountDownLatch latch(0);
17 latch.Wait();
18}
19
20#if OS_FUCHSIA
21#define CanWait DISABLED_CanWait
22#else
23#define CanWait CanWait
24#endif
25TEST(CountDownLatchTest, CanWait) {
26 fml::Thread thread("test_thread");
27 const size_t count = 100;
28 size_t current_count = 0;
29 CountDownLatch latch(count);
30 auto decrement_latch_on_thread = [runner = thread.GetTaskRunner(), &latch,
31 &current_count]() {
32 runner->PostTask([&latch, &current_count]() {
33 std::this_thread::sleep_for(std::chrono::microseconds(100));
34 current_count++;
35 latch.CountDown();
36 });
37 };
38 for (size_t i = 0; i < count; ++i) {
39 decrement_latch_on_thread();
40 }
41 latch.Wait();
42 ASSERT_EQ(current_count, count);
43}
44
45} // namespace fml
46