CountBarrierTest.cc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <boost/detail/atomic_count.hpp>
  2. #include <boost/date_time.hpp>
  3. #include <boost/threadpool.hpp>
  4. #include <gtest/gtest.h>
  5. #include "Thread/CountBarrier.h"
  6. namespace Egametang {
  7. class CountBarrierTest: public testing::Test
  8. {
  9. protected:
  10. boost::detail::atomic_count count;
  11. public:
  12. CountBarrierTest(): count(0)
  13. {
  14. }
  15. virtual ~CountBarrierTest()
  16. {
  17. }
  18. void Wait(CountBarrier& barrier)
  19. {
  20. barrier.Wait();
  21. }
  22. void Signal(CountBarrier& barrier)
  23. {
  24. boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
  25. ++count;
  26. barrier.Signal();
  27. }
  28. };
  29. TEST_F(CountBarrierTest, Count)
  30. {
  31. CountBarrier barrier(10);
  32. ASSERT_EQ(10, barrier.Count());
  33. }
  34. TEST_F(CountBarrierTest, WaitAndSignal)
  35. {
  36. CountBarrier barrier(10);
  37. boost::threadpool::fifo_pool pool(10);
  38. for (int i = 0; i < 10; ++i)
  39. {
  40. pool.schedule(
  41. boost::bind(&CountBarrierTest::Signal,
  42. this, std::ref(barrier)));
  43. }
  44. ASSERT_EQ(0, this->count);
  45. barrier.Wait();
  46. ASSERT_EQ(10, this->count);
  47. pool.wait();
  48. }
  49. } // namespace Egametang
  50. int main(int argc, char* argv[])
  51. {
  52. testing::InitGoogleTest(&argc, argv);
  53. return RUN_ALL_TESTS();
  54. }