libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00031 #include <boost/date_time/posix_time/posix_time.hpp> 00032 #include <boost/bind.hpp> 00033 #include <boost/ref.hpp> 00034 #include <boost/test/unit_test.hpp> 00035 00036 #include "../../include/moost/utils/foreach.hpp" 00037 #include "../../include/moost/thread/async_batch_processor.hpp" 00038 00039 using namespace moost; 00040 00041 namespace { 00042 00043 void a_job(boost::mutex& mx, volatile size_t& value, const boost::posix_time::time_duration& time) 00044 { 00045 boost::this_thread::sleep(time); 00046 boost::mutex::scoped_lock lock(mx); 00047 ++value; 00048 } 00049 00050 void run_batch_processor_tests(size_t num_threads, const std::vector<boost::posix_time::time_duration>& sleep_times) 00051 { 00052 thread::async_batch_processor processor(num_threads); 00053 00054 foreach (const boost::posix_time::time_duration& t, sleep_times) 00055 { 00056 for (size_t num_jobs = 0; num_jobs <= 16; ++num_jobs) 00057 { 00058 thread::async_batch_processor::jobs_t jobs; 00059 boost::mutex mx; 00060 volatile size_t val = 0; 00061 00062 for (size_t j = 0; j < num_jobs; ++j) 00063 { 00064 jobs.push_back(boost::bind(&a_job, boost::ref(mx), boost::ref(val), boost::cref(t))); 00065 } 00066 00067 processor.dispatch(jobs); 00068 00069 BOOST_CHECK_EQUAL(val, num_jobs); 00070 } 00071 } 00072 } 00073 00074 } 00075 00076 BOOST_AUTO_TEST_SUITE(async_batch_processor_test) 00077 00078 BOOST_AUTO_TEST_CASE(async_batch_processor_test) 00079 { 00080 std::vector<boost::posix_time::time_duration> sleep_times; 00081 00082 sleep_times.push_back(boost::posix_time::milliseconds(0)); 00083 sleep_times.push_back(boost::posix_time::milliseconds(1)); 00084 sleep_times.push_back(boost::posix_time::milliseconds(10)); 00085 00086 for (size_t num_threads = 1; num_threads <= 8; ++num_threads) 00087 { 00088 run_batch_processor_tests(num_threads, sleep_times); 00089 } 00090 } 00091 00092 BOOST_AUTO_TEST_SUITE_END()