thread_pool_test.cc 1.0 KB

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