~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/testsuite/src/ThreadPoolTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ThreadPoolTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/testsuite/src/ThreadPoolTest.cpp#2 $
 
5
//
 
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
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:
 
15
// 
 
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.
 
22
// 
 
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.
 
30
//
 
31
 
 
32
 
 
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"
 
40
 
 
41
 
 
42
using Poco::ThreadPool;
 
43
using Poco::RunnableAdapter;
 
44
using Poco::Thread;
 
45
 
 
46
 
 
47
ThreadPoolTest::ThreadPoolTest(const std::string& name): CppUnit::TestCase(name), _event(false)
 
48
{
 
49
}
 
50
 
 
51
 
 
52
ThreadPoolTest::~ThreadPoolTest()
 
53
{
 
54
}
 
55
 
 
56
 
 
57
void ThreadPoolTest::testThreadPool()
 
58
{
 
59
        ThreadPool pool(2, 3, 3);
 
60
        
 
61
        assert (pool.allocated() == 2);
 
62
        assert (pool.used() == 0);
 
63
        assert (pool.capacity() == 3);
 
64
        assert (pool.available() == 3);
 
65
        pool.addCapacity(1);
 
66
        assert (pool.allocated() == 2);
 
67
        assert (pool.used() == 0);
 
68
        assert (pool.capacity() == 4);
 
69
        assert (pool.available() == 4);
 
70
 
 
71
        RunnableAdapter<ThreadPoolTest> ra(*this, &ThreadPoolTest::count);
 
72
        pool.start(ra);
 
73
        assert (pool.allocated() == 2);
 
74
        assert (pool.used() == 1);
 
75
        assert (pool.capacity() == 4);
 
76
        assert (pool.available() == 3);
 
77
 
 
78
        pool.start(ra);
 
79
        assert (pool.allocated() == 2);
 
80
        assert (pool.used() == 2);
 
81
        assert (pool.capacity() == 4);
 
82
        assert (pool.available() == 2);
 
83
 
 
84
        pool.start(ra);
 
85
        assert (pool.allocated() == 3);
 
86
        assert (pool.used() == 3);
 
87
        assert (pool.capacity() == 4);
 
88
        assert (pool.available() == 1);
 
89
 
 
90
        pool.start(ra);
 
91
        assert (pool.allocated() == 4);
 
92
        assert (pool.used() == 4);
 
93
        assert (pool.capacity() == 4);
 
94
        assert (pool.available() == 0);
 
95
 
 
96
        try
 
97
        {
 
98
                pool.start(ra);
 
99
                failmsg("thread pool exhausted - must throw exception");
 
100
        }       
 
101
        catch (Poco::NoThreadAvailableException&)
 
102
        {
 
103
        }
 
104
        catch (...)
 
105
        {
 
106
                failmsg("wrong exception thrown");
 
107
        }
 
108
        
 
109
        _event.set(); // go!!!
 
110
        pool.joinAll();
 
111
        
 
112
        assert (_count == 40000);
 
113
        
 
114
        assert (pool.allocated() == 4);
 
115
        assert (pool.used() == 0);
 
116
        assert (pool.capacity() == 4);
 
117
        assert (pool.available() == 4);
 
118
        
 
119
        Thread::sleep(4000);
 
120
 
 
121
        pool.collect();
 
122
        assert (pool.allocated() == 2);
 
123
        assert (pool.used() == 0);
 
124
        assert (pool.capacity() == 4);
 
125
        assert (pool.available() == 4);
 
126
        
 
127
        _count = 0;
 
128
        _event.reset();
 
129
        pool.start(ra);
 
130
        assert (pool.allocated() == 2);
 
131
        assert (pool.used() == 1);
 
132
        assert (pool.capacity() == 4);
 
133
        assert (pool.available() == 3);
 
134
 
 
135
        pool.start(ra);
 
136
        assert (pool.allocated() == 2);
 
137
        assert (pool.used() == 2);
 
138
        assert (pool.capacity() == 4);
 
139
        assert (pool.available() == 2);
 
140
        _event.set(); // go!!!
 
141
        pool.joinAll();
 
142
 
 
143
        assert (_count == 20000);
 
144
        
 
145
        assert (pool.allocated() == 2);
 
146
        assert (pool.used() == 0);
 
147
        assert (pool.capacity() == 4);
 
148
        assert (pool.available() == 4);
 
149
}
 
150
 
 
151
 
 
152
void ThreadPoolTest::setUp()
 
153
{
 
154
        _event.reset();
 
155
        _count = 0;
 
156
}
 
157
 
 
158
 
 
159
void ThreadPoolTest::tearDown()
 
160
{
 
161
}
 
162
 
 
163
 
 
164
void ThreadPoolTest::count()
 
165
{
 
166
        _event.wait();
 
167
        for (int i = 0; i < 10000; ++i)
 
168
        {
 
169
                _mutex.lock();
 
170
                ++_count;
 
171
                _mutex.unlock();
 
172
        }
 
173
}
 
174
 
 
175
 
 
176
CppUnit::Test* ThreadPoolTest::suite()
 
177
{
 
178
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ThreadPoolTest");
 
179
 
 
180
        CppUnit_addTest(pSuite, ThreadPoolTest, testThreadPool);
 
181
 
 
182
        return pSuite;
 
183
}