CountBarrierTest.cc 1.3 KB

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