4
// $Id: //poco/1.2/Foundation/testsuite/src/ThreadPoolTest.cpp#2 $
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
33
#include "ThreadPoolTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/ThreadPool.h"
37
#include "Poco/RunnableAdapter.h"
38
#include "Poco/Exception.h"
39
#include "Poco/Thread.h"
42
using Poco::ThreadPool;
43
using Poco::RunnableAdapter;
47
ThreadPoolTest::ThreadPoolTest(const std::string& name): CppUnit::TestCase(name), _event(false)
52
ThreadPoolTest::~ThreadPoolTest()
57
void ThreadPoolTest::testThreadPool()
59
ThreadPool pool(2, 3, 3);
61
assert (pool.allocated() == 2);
62
assert (pool.used() == 0);
63
assert (pool.capacity() == 3);
64
assert (pool.available() == 3);
66
assert (pool.allocated() == 2);
67
assert (pool.used() == 0);
68
assert (pool.capacity() == 4);
69
assert (pool.available() == 4);
71
RunnableAdapter<ThreadPoolTest> ra(*this, &ThreadPoolTest::count);
73
assert (pool.allocated() == 2);
74
assert (pool.used() == 1);
75
assert (pool.capacity() == 4);
76
assert (pool.available() == 3);
79
assert (pool.allocated() == 2);
80
assert (pool.used() == 2);
81
assert (pool.capacity() == 4);
82
assert (pool.available() == 2);
85
assert (pool.allocated() == 3);
86
assert (pool.used() == 3);
87
assert (pool.capacity() == 4);
88
assert (pool.available() == 1);
91
assert (pool.allocated() == 4);
92
assert (pool.used() == 4);
93
assert (pool.capacity() == 4);
94
assert (pool.available() == 0);
99
failmsg("thread pool exhausted - must throw exception");
101
catch (Poco::NoThreadAvailableException&)
106
failmsg("wrong exception thrown");
109
_event.set(); // go!!!
112
assert (_count == 40000);
114
assert (pool.allocated() == 4);
115
assert (pool.used() == 0);
116
assert (pool.capacity() == 4);
117
assert (pool.available() == 4);
122
assert (pool.allocated() == 2);
123
assert (pool.used() == 0);
124
assert (pool.capacity() == 4);
125
assert (pool.available() == 4);
130
assert (pool.allocated() == 2);
131
assert (pool.used() == 1);
132
assert (pool.capacity() == 4);
133
assert (pool.available() == 3);
136
assert (pool.allocated() == 2);
137
assert (pool.used() == 2);
138
assert (pool.capacity() == 4);
139
assert (pool.available() == 2);
140
_event.set(); // go!!!
143
assert (_count == 20000);
145
assert (pool.allocated() == 2);
146
assert (pool.used() == 0);
147
assert (pool.capacity() == 4);
148
assert (pool.available() == 4);
152
void ThreadPoolTest::setUp()
159
void ThreadPoolTest::tearDown()
164
void ThreadPoolTest::count()
167
for (int i = 0; i < 10000; ++i)
176
CppUnit::Test* ThreadPoolTest::suite()
178
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ThreadPoolTest");
180
CppUnit_addTest(pSuite, ThreadPoolTest, testThreadPool);