CountBarrierTest.cc 1.2 KB

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