Permalink
Browse files

fix waiters not waking up, and regression

  • Loading branch information...
1 parent 7e3550c commit c6c91c7af889f26fbb70b150fac0decb1e64f2b6 @erikfrey erikfrey committed Aug 20, 2012
Showing with 31 additions and 2 deletions.
  1. +2 −0 src/queue/queue.cpp
  2. +6 −2 tests/fixtures/basic_queue.hpp
  3. +23 −0 tests/queue.cpp
View
@@ -50,6 +50,8 @@ queue::queue(asio::io_service& ios, const string& path)
void queue::wait(size_type wait_ms, const wait_callback& cb)
{
ptr_list<waiter>::iterator it = waiters_.insert(waiters_.end(), new waiter(ios_, wait_ms, cb));
+ if (wake_up_it_ == waiters_.end())
+ wake_up_it_ = it;
it->timer.async_wait(bind(&queue::waiter_wakeup, this, asio::placeholders::error, it));
}
@@ -18,7 +18,8 @@ class basic_queue
public:
basic_queue()
- : wait_cb_(boost::bind(&basic_queue::wait_cb, this, _1)),
+ : cb_count_(0),
+ wait_cb_(boost::bind(&basic_queue::wait_cb, this, _1)),
tmp_(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path())
{
boost::filesystem::create_directories(tmp_);
@@ -42,12 +43,15 @@ class basic_queue
void wait_cb(const boost::system::error_code& error)
{
- error_ = error;
+ if (!error_)
+ error_ = error;
+ ++cb_count_;
}
std::string pop_value_;
boost::system::error_code error_;
+ size_t cb_count_;
darner::queue::wait_callback wait_cb_;
boost::asio::io_service ios_;
View
@@ -38,12 +38,35 @@ BOOST_FIXTURE_TEST_CASE( test_pop_wait, fixtures::basic_queue )
{
string value = "sometimes I push the door close button on people running towards the elevator. I just need my own "
"elevator sometimes, my 7 floor sanctuary";
+ posix_time::ptime beg = boost::posix_time::microsec_clock::local_time();
deadline_timer timer(ios_, posix_time::milliseconds(10));
timer.async_wait(bind(&fixtures::basic_queue::delayed_push, this, ref(value), _1));
queue_->wait(100, wait_cb_);
ios_.run();
+ boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
BOOST_REQUIRE(!error_);
+ BOOST_REQUIRE_LT((end - beg).total_milliseconds(), 50); // should come back sooner than the timeout
+}
+
+// test multiple waiters
+BOOST_FIXTURE_TEST_CASE( test_multiple_pop_wait, fixtures::basic_queue )
+{
+ string value1 = "I’m just tryna keep it symmetrical";
+ string value2 = "Hotel robe got me feeling like a Sheik";
+ posix_time::ptime beg = boost::posix_time::microsec_clock::local_time();
+ deadline_timer timer1(ios_, posix_time::milliseconds(10));
+ timer1.async_wait(bind(&fixtures::basic_queue::delayed_push, this, ref(value1), _1));
+ deadline_timer timer2(ios_, posix_time::milliseconds(20));
+ timer2.async_wait(bind(&fixtures::basic_queue::delayed_push, this, ref(value2), _1));
+ queue_->wait(100, wait_cb_);
+ queue_->wait(100, wait_cb_);
+ ios_.run();
+ boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
+
+ BOOST_REQUIRE(!error_);
+ BOOST_REQUIRE_EQUAL(cb_count_, 2);
+ BOOST_REQUIRE_LT((end - beg).total_milliseconds(), 50); // should come back sooner than the timeout
}
// test the race condition where a wait callback does work that crosses it over the wait timeout

0 comments on commit c6c91c7

Please sign in to comment.