ThreadPoolTest.cc 993 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 Hainan {
  8. class ThreadPoolTest: public testing::Test
  9. {
  10. void SetUp()
  11. {
  12. pool.Start();
  13. }
  14. void TearDown()
  15. {
  16. }
  17. protected:
  18. ThreadPool pool;
  19. public:
  20. ThreadPoolTest() : pool(10)
  21. {
  22. }
  23. void Max(int a, int b, int* z)
  24. {
  25. *z = a > b? a : b;
  26. }
  27. };
  28. TEST_F(ThreadPoolTest, Test1)
  29. {
  30. std::vector<int> x(100, 8);
  31. std::vector<int> y(100, 9);
  32. std::vector<int> z(100, 0);
  33. for (int i = 0; i < 100; ++i)
  34. {
  35. pool.push_task(
  36. boost::bind(&ThreadPoolTest::Max, this, x[i], y[i], &z[i]));
  37. }
  38. pool.Stop();
  39. for (int i = 0; i < 100; ++i)
  40. {
  41. ASSERT_EQ(9, z[i])<< "i = " << i;
  42. }
  43. }
  44. } // namespace Hainan
  45. int main(int argc, char* argv[])
  46. {
  47. FLAGS_logtostderr = true;
  48. testing::InitGoogleTest(&argc, argv);
  49. google::ParseCommandLineFlags(&argc, &argv, true);
  50. google::InitGoogleLogging(argv[0]);
  51. return RUN_ALL_TESTS();
  52. }