Scroom  0.14
threadpool-destruction-tests.cc File Reference
#include <scroom/threadpool.hh>
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>
#include <scroom/function-additor.hh>
#include <scroom/semaphore.hh>
#include "helpers.hh"
Include dependency graph for threadpool-destruction-tests.cc:

Functions

const millisec short_timeout (250)
 
const millisec long_timeout (2000)
 
 BOOST_AUTO_TEST_CASE (destroy_threadpool_with_nonempty_queue)
 
 BOOST_AUTO_TEST_CASE (destroy_threadpool_with_nonempty_queue_with_completeAllJobsBeforeDestruction_true)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( destroy_threadpool_with_nonempty_queue  )
37 {
39  Semaphore guard(0);
40  Semaphore a(0);
41  Semaphore b(0);
42  Semaphore c(0);
43 
44  pool->schedule(clear(&a) + pass(&b));
45  pool->schedule(clear(&c));
46 
47  // Give the thread some time to start the job
48  a.P();
49 
50  boost::thread t(pass(&guard) + destroy(pool));
51  pool.reset();
52  guard.V();
53 
54  // Thread t destroys the threadpool without waiting for ThreadPool
55  // jobs to finish. Hence, it should terminate immediately, even
56  // though the threadpool is blocked on pass(&b)
57  BOOST_CHECK(t.timed_join(long_timeout));
58  BOOST_REQUIRE(boost::thread::id() == t.get_id());
59  b.V();
60  BOOST_CHECK(!c.P(long_timeout));
61 }
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( destroy_threadpool_with_nonempty_queue_with_completeAllJobsBeforeDestruction_true  )
64 {
65  ThreadPool::Ptr pool = ThreadPool::create(1, true);
66  Semaphore guard(0);
67  Semaphore a(0);
68  Semaphore b(0);
69  Semaphore c(0);
70 
71  pool->schedule(clear(&a) + pass(&b));
72  pool->schedule(clear(&c));
73 
74  // Give the thread some time to start the job
75  a.P();
76 
77  boost::thread t(pass(&guard) + destroy(pool));
78  pool.reset();
79  guard.V();
80 
81  // Thread t destroys the threadpool without waiting for ThreadPool
82  // jobs to finish. Hence, it should terminate immediately, even
83  // though the threadpool is blocked on pass(&b)
84  BOOST_CHECK(t.timed_join(long_timeout));
85  BOOST_REQUIRE(boost::thread::id() == t.get_id());
86  b.V();
87  BOOST_CHECK(c.P(long_timeout));
88 }
Here is the call graph for this function:

◆ long_timeout()

const millisec long_timeout ( 2000  )

Referenced by BOOST_AUTO_TEST_CASE().

Here is the caller graph for this function:

◆ short_timeout()

const millisec short_timeout ( 250  )
ThreadPool::Ptr
boost::shared_ptr< ThreadPool > Ptr
Definition: threadpool.hh:169
clear
boost::function< void()> clear(Semaphore *s)
Definition: helpers.cc:29
destroy
boost::function< void()> destroy(boost::shared_ptr< void > p)
Definition: helpers.cc:31
long_timeout
const millisec long_timeout(2000)
Scroom::Semaphore
Definition: semaphore.hh:15
ThreadPool::create
static ThreadPool::Ptr create(bool completeAllJobsBeforeDestruction=false)
Definition: threadpoolimpl.cc:228
BOOST_REQUIRE
BOOST_REQUIRE(originalColormap)
pass
boost::function< void()> pass(Semaphore *s)
Definition: helpers.cc:27
b
static void b(const B::Ptr &)
Definition: gtkhelper-tests.cc:32