ThreadPoolTest.cc 968 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. virtual ~ThreadPoolTest()
  17. {
  18. }
  19. void Max(int a, int b, int* z)
  20. {
  21. *z = a > b? a : b;
  22. }
  23. };
  24. TEST_F(ThreadPoolTest, Test1)
  25. {
  26. std::vector<int> x(100, 8);
  27. std::vector<int> y(100, 9);
  28. std::vector<int> z(100, 0);
  29. for (int i = 0; i < 100; ++i)
  30. {
  31. pool.Schedule(
  32. boost::bind(&ThreadPoolTest::Max,
  33. this, x[i], y[i], &z[i]));
  34. }
  35. pool.Wait();
  36. for (int i = 0; i < 100; ++i)
  37. {
  38. ASSERT_EQ(9, z[i]);
  39. }
  40. }
  41. } // namespace Egametang
  42. int main(int argc, char* argv[])
  43. {
  44. FLAGS_logtostderr = true;
  45. testing::InitGoogleTest(&argc, argv);
  46. google::ParseCommandLineFlags(&argc, &argv, true);
  47. google::InitGoogleLogging(argv[0]);
  48. return RUN_ALL_TESTS();
  49. }