LCOV - code coverage report
Current view: top level - util/test - worker_pool.cpp (source / functions) Hit Total Coverage
Test: CoherentDB code coverage Lines: 42 42 100.0 %
Date: 2011-02-13 Functions: 14 16 87.5 %

          Line data    Source code
       1             : /*
       2             :  * (C) Copyright 2011 Marek Dopiera
       3             :  * 
       4             :  * This file is part of CoherentDB.
       5             :  * 
       6             :  * CoherentDB is free software: you can redistribute it and/or modify it
       7             :  * under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation, either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  * 
      11             :  * CoherentDB is distributed in the hope that it will be useful, but
      12             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      14             :  * General Public License for more details.
      15             :  * 
      16             :  * You should have received a copy of the GNU General Public
      17             :  * License along with CoherentDB. If not, see
      18             :  * http://www.gnu.org/licenses/.
      19             :  */
      20             : 
      21             : #include <pthread.h>
      22             : 
      23             : #include <util/worker_pool.h>
      24             : #include <config/config.h>
      25             : #include <debug/asserts.h>
      26             : #include <log/log.h>
      27             : 
      28             : namespace coherent {
      29             : namespace util {
      30             : namespace unittests {
      31             : 
      32             : using namespace std;
      33             : using namespace boost;
      34             : using namespace log4cxx;
      35             : using namespace coherent::config;
      36             : using namespace coherent::log;
      37             : 
      38             : class dummy_worker : public worker<int>
      39         280 : {
      40             : public:
      41          40 :         dummy_worker(worker_pool<int> & pool) : worker<int>(pool)
      42             :         {
      43          40 :         }
      44             : 
      45         100 :         void handle(int const & i) const
      46             :         {
      47             :                 LOG(DEBUG, "thread " << ::pthread_self() << " handling " << i);
      48         100 :                 usleep(random() % 100000);
      49         100 :         }
      50             : };
      51             : 
      52             : class dummy_worker_factory : public worker_factory<int>
      53           8 : {
      54             : public:
      55          40 :         virtual worker_ptr create_worker(worker_pool<int> & pool) const
      56             :         {
      57          40 :                 return new thread(dummy_worker(pool));
      58             :         }
      59             : };
      60             : 
      61           1 : void worker_pool_start_stop()
      62             : {
      63             :         LOG(INFO, "========== worker_pool_start_stop");
      64           2 :         worker_pool<int> pool;
      65           4 :         for (uint32_t i = 0; i < 3; ++i)
      66             :         {
      67             :                 {
      68           6 :                         dummy_worker_factory const fact;
      69           3 :                         pool.start(10, fact);
      70             :                 }
      71           3 :                 pool.stop();
      72             :         }
      73           1 : }
      74             : 
      75           1 : void worker_pool_handle_some()
      76             : {
      77             :         LOG(INFO, "========== worker_pool_handle_some");
      78           2 :         worker_pool<int> pool;
      79           2 :         dummy_worker_factory const fact;
      80           1 :         pool.start(10, fact);
      81             : 
      82         101 :         for (uint32_t i = 0; i < 100; ++i)
      83             :         {
      84         100 :                 pool.schedule_work(i);
      85             :         }
      86             : 
      87           1 :         usleep(random() % 100000);
      88           1 :         pool.stop();
      89           1 : }
      90             : 
      91           1 : void sync_queue_single_threaded_test()
      92             : {
      93             :         LOG(INFO, "========== sync_queue_single_threaded_test");
      94           2 :         sync_queue<int> q;
      95           1 :         q.push(5);
      96           1 :         q.push(10);
      97           1 :         q.push(15);
      98             :         r_assert(q.pop().get() == 5, "queue doesn't work");
      99             :         r_assert(q.pop().get() == 10, "queue doesn't work");
     100           1 :         q.no_more_input();
     101             :         r_assert(q.pop().get() == 15, "queue doesn't work");
     102             :         r_assert(!q.pop(), "what?");
     103           1 : }
     104             : 
     105           1 : int start_test(const int argc, const char *const *const argv)
     106             : {
     107           2 :         scoped_test_enabler test_setup(argc, argv);
     108             : 
     109           1 :         Logger::getLogger("coherent.util")->setLevel(log_TRACE);   
     110             : 
     111           1 :         sync_queue_single_threaded_test();
     112           1 :         worker_pool_start_stop();
     113           1 :         worker_pool_handle_some();
     114             : 
     115           1 :         return 0;
     116             : }
     117             : 
     118             : } // namespace unittests
     119             : } // namespace util
     120             : } // namespace coherent
     121             : 
     122             : int main(const int argc, const char * const * const argv)
     123           1 : {
     124             :         return coherent::util::unittests::start_test(argc, argv);
     125           1 : }
     126           3 : 

Generated by: LCOV version 1.9