| 1 |
// test_threads.cc |
| 2 |
// |
| 3 |
// Copyright (C) 2005-2006, 2009 Daniel Burrows |
| 4 |
// |
| 5 |
// This program is free software; you can redistribute it and/or |
| 6 |
// modify it under the terms of the GNU General Public License as |
| 7 |
// published by the Free Software Foundation; either version 2 of |
| 8 |
// the License, or (at your option) any later version. |
| 9 |
// |
| 10 |
// This program is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
// General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with this program; see the file COPYING. If not, write to |
| 17 |
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 |
// Boston, MA 02111-1307, USA. |
| 19 |
|
| 20 |
// TODO: threads code lives in cwidget now, this test should move |
| 21 |
// there. |
| 22 |
|
| 23 |
#include <cppunit/extensions/HelperMacros.h> |
| 24 |
|
| 25 |
#include <cwidget/generic/threads/event_queue.h> |
| 26 |
#include <cwidget/generic/threads/threads.h> |
| 27 |
#include <cwidget/generic/util/ssprintf.h> |
| 28 |
|
| 29 |
#include <iostream> |
| 30 |
|
| 31 |
#include <sys/time.h> |
| 32 |
#include <time.h> |
| 33 |
|
| 34 |
#include <unistd.h> |
| 35 |
|
| 36 |
namespace cw = cwidget; |
| 37 |
|
| 38 |
class TestThreads : public CppUnit::TestFixture |
| 39 |
{ |
| 40 |
CPPUNIT_TEST_SUITE(TestThreads); |
| 41 |
|
| 42 |
CPPUNIT_TEST(testBox); |
| 43 |
CPPUNIT_TEST(testTimedTake); |
| 44 |
CPPUNIT_TEST(testEventQueue); |
| 45 |
CPPUNIT_TEST(testAutoDetach); |
| 46 |
|
| 47 |
CPPUNIT_TEST_SUITE_END(); |
| 48 |
|
| 49 |
public: |
| 50 |
// Ye Olde Addition Thread |
| 51 |
struct add_thread |
| 52 |
{ |
| 53 |
int n; |
| 54 |
cw::threads::box<int> &count; |
| 55 |
public: |
| 56 |
add_thread(int _n, cw::threads::box<int> &_count) |
| 57 |
:n(_n), count(_count) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
void operator()() const |
| 62 |
{ |
| 63 |
for(int i = 0; i < n; ++i) |
| 64 |
count.put(count.take()+1); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
void do_testBox() |
| 69 |
{ |
| 70 |
const int thread_count_max = 50; |
| 71 |
const int thread_limit = 1000; |
| 72 |
|
| 73 |
long real_threads_max = sysconf(_SC_THREAD_THREADS_MAX); |
| 74 |
int thread_count; |
| 75 |
if(real_threads_max == -1) |
| 76 |
thread_count = thread_count_max; // No definite limit. |
| 77 |
else |
| 78 |
thread_count = std::min<int>((int)thread_count_max, (int)real_threads_max); |
| 79 |
|
| 80 |
cw::threads::box<int> b(100); |
| 81 |
|
| 82 |
CPPUNIT_ASSERT_EQUAL(100, b.take()); |
| 83 |
|
| 84 |
std::unique_ptr<cw::threads::thread> *writers = |
| 85 |
new std::unique_ptr<cw::threads::thread>[thread_count]; |
| 86 |
|
| 87 |
try |
| 88 |
{ |
| 89 |
for(int i = 0; i<thread_count; ++i) |
| 90 |
writers[i] = std::unique_ptr<cw::threads::thread>(new cw::threads::thread(add_thread(thread_limit, b))); |
| 91 |
|
| 92 |
int foo; |
| 93 |
CPPUNIT_ASSERT(!b.try_take(foo)); |
| 94 |
|
| 95 |
CPPUNIT_ASSERT(b.try_put(-1)); |
| 96 |
|
| 97 |
for(int i = 0; i < thread_count; ++i) |
| 98 |
writers[i]->join(); |
| 99 |
|
| 100 |
CPPUNIT_ASSERT_EQUAL(thread_count*thread_limit-1, b.take()); |
| 101 |
} |
| 102 |
catch(...) |
| 103 |
{ |
| 104 |
delete[] writers; |
| 105 |
throw; |
| 106 |
} |
| 107 |
|
| 108 |
delete[] writers; |
| 109 |
} |
| 110 |
|
| 111 |
void testBox() |
| 112 |
{ |
| 113 |
try |
| 114 |
{ |
| 115 |
do_testBox(); |
| 116 |
} |
| 117 |
catch(const cw::util::Exception &e) |
| 118 |
{ |
| 119 |
std::cerr << "Caught exception in testBox: " << e.errmsg() << std::endl; |
| 120 |
throw; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void testTimedTake() |
| 125 |
{ |
| 126 |
timeval now; |
| 127 |
|
| 128 |
cw::threads::box<int> b; |
| 129 |
|
| 130 |
gettimeofday(&now, NULL); |
| 131 |
timespec timeout; |
| 132 |
timeout.tv_sec = now.tv_sec + 2; |
| 133 |
timeout.tv_nsec = now.tv_usec * 1000; |
| 134 |
|
| 135 |
int dummy; |
| 136 |
|
| 137 |
// Test that timing out trying to take values from an empty box works: |
| 138 |
|
| 139 |
CPPUNIT_ASSERT(!b.timed_take(dummy, timeout)); |
| 140 |
|
| 141 |
timeval now2; |
| 142 |
gettimeofday(&now2, NULL); |
| 143 |
|
| 144 |
// pthread_cond_timedwait is allowed to return before the timeout |
| 145 |
// expires, according to Bastian Blank <[email protected]>, so this |
| 146 |
// invariant is not an invariant at all. |
| 147 |
// |
| 148 |
//CPPUNIT_ASSERT(now2.tv_sec >= now.tv_sec + 2); |
| 149 |
//CPPUNIT_ASSERT(now2.tv_sec > now.tv_sec + 2 || now2.tv_usec >= now.tv_usec); |
| 150 |
|
| 151 |
// Test that we can retrieve a value from a full box: |
| 152 |
|
| 153 |
timeout.tv_sec = now2.tv_sec + 2; |
| 154 |
timeout.tv_nsec = now2.tv_usec * 1000; |
| 155 |
b.put(5); |
| 156 |
CPPUNIT_ASSERT(b.timed_take(dummy, timeout)); |
| 157 |
CPPUNIT_ASSERT_EQUAL(5, dummy); |
| 158 |
} |
| 159 |
|
| 160 |
class event_queue_write_thread |
| 161 |
{ |
| 162 |
cw::threads::event_queue<std::pair<int, int> > &eq; |
| 163 |
|
| 164 |
int id, n; |
| 165 |
public: |
| 166 |
event_queue_write_thread(cw::threads::event_queue<std::pair<int, int> > &_eq, |
| 167 |
int _id, int _n) |
| 168 |
:eq(_eq), id(_id), n(_n) |
| 169 |
{ |
| 170 |
} |
| 171 |
|
| 172 |
void operator()() const |
| 173 |
{ |
| 174 |
for(int i = 0; i < n; ++i) |
| 175 |
eq.put(std::pair<int, int>(id, i)); |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
void testEventQueue() |
| 180 |
{ |
| 181 |
const int thread_count_max = 100; |
| 182 |
const int thread_limit = 1000; |
| 183 |
|
| 184 |
long real_threads_max = sysconf(_SC_THREAD_THREADS_MAX); |
| 185 |
int thread_count; |
| 186 |
if(real_threads_max == -1) |
| 187 |
thread_count = thread_count_max; // No definite limit. |
| 188 |
else |
| 189 |
thread_count = std::min<int>((int)thread_count_max, (int)real_threads_max); |
| 190 |
|
| 191 |
cw::threads::event_queue<std::pair<int, int> > eq; |
| 192 |
|
| 193 |
std::unique_ptr<cw::threads::thread> *writers = |
| 194 |
new std::unique_ptr<cw::threads::thread>[thread_count]; |
| 195 |
int *last_thread_msg = |
| 196 |
new int[thread_count]; |
| 197 |
|
| 198 |
try |
| 199 |
{ |
| 200 |
for(int i = 0; i < thread_count; ++i) |
| 201 |
last_thread_msg[i] = -1; |
| 202 |
|
| 203 |
for(int i = 0; i < thread_count; ++i) |
| 204 |
writers[i] = std::unique_ptr<cw::threads::thread>(new cw::threads::thread(event_queue_write_thread(eq, i, thread_limit))); |
| 205 |
|
| 206 |
for(int i = 0; i < thread_count * thread_limit; ++i) |
| 207 |
{ |
| 208 |
std::pair<int, int> next = eq.get(); |
| 209 |
|
| 210 |
CPPUNIT_ASSERT_EQUAL(next.second-1, last_thread_msg[next.first]); |
| 211 |
last_thread_msg[next.first] = next.second; |
| 212 |
} |
| 213 |
|
| 214 |
for(int i = 0; i < thread_count; ++i) |
| 215 |
writers[i]->join(); |
| 216 |
|
| 217 |
CPPUNIT_ASSERT(eq.empty()); |
| 218 |
} |
| 219 |
catch(...) |
| 220 |
{ |
| 221 |
delete[] writers; |
| 222 |
delete[] last_thread_msg; |
| 223 |
|
| 224 |
throw; |
| 225 |
} |
| 226 |
|
| 227 |
delete[] writers; |
| 228 |
delete[] last_thread_msg; |
| 229 |
} |
| 230 |
|
| 231 |
struct do_nothing |
| 232 |
{ |
| 233 |
public: |
| 234 |
void operator()() const |
| 235 |
{ |
| 236 |
} |
| 237 |
}; |
| 238 |
|
| 239 |
void testAutoDetach() |
| 240 |
{ |
| 241 |
cw::threads::thread t(do_nothing()); |
| 242 |
} |
| 243 |
}; |
| 244 |
|
| 245 |
CPPUNIT_TEST_SUITE_REGISTRATION(TestThreads); |