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/synchronization/count_down_latch.h" 9 #include "flutter/fml/thread.h" 10 #include "flutter/testing/testing.h" 11 12 namespace fml { 13 TEST(CountDownLatchTest,CanWaitOnZero)14TEST(CountDownLatchTest, CanWaitOnZero) { 15 CountDownLatch latch(0); 16 latch.Wait(); 17 } 18 TEST(CountDownLatchTest,CanWait)19TEST(CountDownLatchTest, CanWait) { 20 fml::Thread thread("test_thread"); 21 const size_t count = 100; 22 size_t current_count = 0; 23 CountDownLatch latch(count); 24 auto decrement_latch_on_thread = [runner = thread.GetTaskRunner(), &latch, 25 ¤t_count]() { 26 runner->PostTask([&latch, ¤t_count]() { 27 std::this_thread::sleep_for(std::chrono::microseconds(100)); 28 current_count++; 29 latch.CountDown(); 30 }); 31 }; 32 for (size_t i = 0; i < count; ++i) { 33 decrement_latch_on_thread(); 34 } 35 latch.Wait(); 36 ASSERT_EQ(current_count, count); 37 } 38 39 } // namespace fml 40