forked from cpp-netlib/cpp-netlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_thread_pool.cpp
53 lines (46 loc) · 1.54 KB
/
utils_thread_pool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2010 Dean Michael Berris.
// Copyright 2012 Google, Inc.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifdef BUILD_SHARED_LIBS
# define BOOST_TEST_DYN_LINK
#endif
#define BOOST_TEST_MODULE utils thread pool test
#include <boost/config/warning_disable.hpp>
#include <boost/test/unit_test.hpp>
#include <network/utils/thread_pool.hpp>
#include <boost/bind.hpp>
using namespace network;
// This test specifies the requirements for a thread pool interface. At the
// very least any thread pool implementation should be able to pass the simple
// tests that this unit test requires of thread pools. Ultimately the
// requirements will show up in the Concept documentation, but this test is the
// canonical definition of what a thread pool should look like at least
// syntactically.
//
BOOST_AUTO_TEST_CASE( default_constructor ) {
utils::thread_pool pool;
BOOST_CHECK_EQUAL(pool.thread_count(), std::size_t(1));
}
struct foo {
foo() : val_(0) {}
void bar(int val) {
val_ += val;
}
int const val() const {
return val_;
}
protected:
int val_;
};
BOOST_AUTO_TEST_CASE( post_work ) {
foo instance;
{
utils::thread_pool pool;
BOOST_CHECK_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 1)));
BOOST_CHECK_NO_THROW(pool.post(boost::bind(&foo::bar, &instance, 2)));
// require that pool is destroyed here, RAII baby
}
BOOST_CHECK_EQUAL(instance.val(), 3);
}