ThreadPoolTest.cc 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <boost/function.hpp>
  2. #include <boost/bind.hpp>
  3. #include <gtest/gtest.h>
  4. #include <gflags/gflags.h>
  5. #include <glog/logging.h>
  6. #include "Thread/ThreadPool.h"
  7. namespace Egametang {
  8. class ThreadPoolTest: public testing::Test
  9. {
  10. protected:
  11. ThreadPool pool_;
  12. public:
  13. ThreadPoolTest() : pool_(10)
  14. {
  15. }
  16. void Max(int a, int b, int* z)
  17. {
  18. *z = a > b? a : b;
  19. }
  20. };
  21. TEST_F(ThreadPoolTest, Test1)
  22. {
  23. pool_.Start();
  24. std::vector<int> x(100, 8);
  25. std::vector<int> y(100, 9);
  26. std::vector<int> z(100, 0);
  27. for (int i = 0; i < 100; ++i)
  28. {
  29. pool_.PushTask(
  30. boost::bind(&ThreadPoolTest::Max, this, x[i], y[i], &z[i]));
  31. }
  32. pool_.Stop();
  33. for (int i = 0; i < 100; ++i)
  34. {
  35. ASSERT_EQ(9, z[i]) << "i = " << i;
  36. }
  37. }
  38. } // namespace Egametang
  39. int main(int argc, char* argv[])
  40. {
  41. FLAGS_logtostderr = true;
  42. testing::InitGoogleTest(&argc, argv);
  43. google::ParseCommandLineFlags(&argc, &argv, true);
  44. google::InitGoogleLogging(argv[0]);
  45. return RUN_ALL_TESTS();
  46. }