CountBarrierTest.cc 1.3 KB

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