36 #include <gtest/gtest.h>
44 #include <boost/atomic.hpp>
45 #include <boost/shared_ptr.hpp>
46 #include <boost/bind/bind.hpp>
47 #include <boost/thread.hpp>
48 #include <boost/function.hpp>
63 boost::mutex::scoped_lock lock(mutex);
75 CallbackQueue* queue,
bool& done, boost::atomic<size_t>* num_calls,
87 num_calls->fetch_add(i);
99 EXPECT_EQ(cb->count, 1U);
104 EXPECT_EQ(cb->count, 2U);
107 EXPECT_EQ(cb->count, 2U);
110 EXPECT_EQ(cb->count, 2U);
117 for (uint32_t i = 0; i < 1000; ++i)
124 EXPECT_EQ(cb->count, 1000U);
131 for (uint32_t i = 0; i < 1000; ++i)
136 for (uint32_t i = 0; i < 1000; ++i)
139 EXPECT_EQ(cb->count, i + 1);
153 EXPECT_EQ(cb1->count, 0U);
154 EXPECT_EQ(cb2->count, 1U);
197 EXPECT_EQ(cb1->count, 1U);
198 EXPECT_EQ(cb2->count, 1U);
225 const uint64_t owner_id = 1;
226 const uint64_t unrelated_id = 2;
239 boost::atomic<size_t> calls(0);
241 boost::thread
t = boost::thread(boost::bind(&
callAvailableThread, &queue, boost::ref(done), &calls, call_one_timeout));
254 cb1->mutex_.unlock();
265 EXPECT_EQ(cb1->count, 1U);
266 EXPECT_EQ(cb2->count, 1U);
267 EXPECT_EQ(cb3->count, 0U);
269 cb1->mutex_.unlock();
278 , use_available(use_available)
316 EXPECT_EQ(cb->count, 3U);
328 EXPECT_EQ(cb->count, 3U);
340 EXPECT_EQ(cb->count, 3U);
352 EXPECT_EQ(cb->count, 3U);
358 size_t* num_calls = NULL,
size_t num_threads = 10,
364 boost::thread_group tg;
366 boost::atomic<size_t> calls(0);
368 for (uint32_t i = 0; i < num_threads; ++i)
370 tg.create_thread(boost::bind(
threadFunc, &queue, boost::ref(done), &calls, call_one_timeout));
379 if (!pause_between_callbacks.isZero())
381 pause_between_callbacks.sleep();
404 EXPECT_EQ(cb->count, i);
408 CallbackQueue* queue,
bool& done, boost::atomic<size_t>* num_calls,
420 num_calls->fetch_add(i);
429 EXPECT_EQ(cb->count, i);
436 const std::string& topic, int32_t queue_size,
437 bool allow_concurrent_callbacks)
444 ready_count.fetch_add(1);
470 const size_t num_threads =
param.num_threads;
477 const size_t queue_size =
static_cast<size_t>(test_duration.
toSec()) + 1;
481 boost::make_shared<CountingSubscriptionQueue>(
"test", queue_size,
false));
490 for (
size_t i = 0; i < queue_size; ++i)
497 size_t num_call_one_calls = 0;
500 num_threads, test_duration, pause_between_callbacks, call_one_timeout);
502 const uint32_t num_callbacks_called = helper->calls_;
503 const size_t num_ready_called = cb->ready_count;
519 num_callbacks_called <<
" callbacks out of " << num_callbacks_to_call);
520 ROS_INFO_STREAM(
"callOne() was called " << num_call_one_calls <<
" times.");
521 ROS_INFO_STREAM(
"ready() was called " << num_ready_called <<
" times.");
523 EXPECT_EQ(num_callbacks_called, queue_size);
524 EXPECT_LE(num_call_one_calls, 2 * num_callbacks_to_call + num_threads * (1/call_one_timeout.
toSec()) * queue_size);
600 boost::thread_group tg;
602 boost::atomic<size_t> calls(0);
604 for (uint32_t i = 0; i < 2; ++i)
622 : id(0), queue(_queue) {
623 condition_sync.store(
true);
624 condition_one.store(
false);
625 condition_stop.store(
false);
641 : condition_object(_condition_object), id(_id)
646 condition_object->condition_one.store(
false);
656 while(!condition_stop.load())
658 if(condition_sync.load() && queue->
isEmpty())
660 condition_one.store(
true);
662 queue->
addCallback(boost::make_shared<RaceConditionCallback>(
this, &
id),
id);
664 boost::this_thread::sleep(boost::posix_time::microseconds(1));
674 for(
unsigned int i = 0; i < 1000000; ++i)
693 int main(
int argc,
char** argv)
695 testing::InitGoogleTest(&argc, argv);
696 return RUN_ALL_TESTS();