Make synchronization primitive tests more granular on timeouts, from seconds to miliseconds precission for Base Times. This should reduce CI times.

This commit is contained in:
Ion Gaztañaga
2024-01-09 00:42:22 +01:00
parent 7f785fe334
commit f052680461
7 changed files with 61 additions and 71 deletions

View File

@@ -188,7 +188,7 @@ inline void thread_sleep_tick()
inline void thread_yield()
{ winapi::sched_yield(); }
inline void thread_sleep(unsigned int ms)
inline void thread_sleep_ms(unsigned int ms)
{ winapi::sleep(ms); }
//systemwide thread
@@ -425,7 +425,7 @@ inline void thread_sleep_tick()
::nanosleep(&rqt, 0);
}
inline void thread_sleep(unsigned int ms)
inline void thread_sleep_ms(unsigned int ms)
{
struct timespec rqt;
rqt.tv_sec = ms/1000u;

View File

@@ -97,7 +97,7 @@ void do_test_condition_notify_one()
boost::interprocess::ipcdetail::OS_thread_t thread;
boost::interprocess::ipcdetail::thread_launch(thread, bind_function(&condition_test_thread<Condition, Mutex>, &data));
//Make sure thread is blocked
boost::interprocess::ipcdetail::thread_sleep(1000);
boost::interprocess::ipcdetail::thread_sleep_ms(1*BaseMs);
{
boost::interprocess::scoped_lock<Mutex>
lock(data.mutex);
@@ -123,7 +123,7 @@ void do_test_condition_notify_all()
}
//Make sure all threads are blocked
boost::interprocess::ipcdetail::thread_sleep(1000);
boost::interprocess::ipcdetail::thread_sleep_ms(1*BaseMs);
{
boost::interprocess::scoped_lock<Mutex>
lock(data.mutex);
@@ -143,7 +143,7 @@ void do_test_condition_waits_step( condition_test_data<Condition, Mutex> &data
, boost::interprocess::scoped_lock<Mutex> &lock
, int awoken)
{
boost::interprocess::ipcdetail::thread_sleep(1000);
boost::interprocess::ipcdetail::thread_sleep_ms(1*BaseMs);
data.notified++;
data.condition.notify_one();
while (data.awoken != awoken)
@@ -176,7 +176,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test timed_wait
while (data->notified != 3)
data->condition.timed_wait(lock, ptime_delay(5));
data->condition.timed_wait(lock, ptime_delay_ms(5*BaseMs));
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 3);
data->awoken++;
@@ -184,7 +184,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test predicate timed_wait.
{
bool ret = data->condition.timed_wait(lock, boost_systemclock_delay(5), cond_predicate (data->notified, 4));
bool ret = data->condition.timed_wait(lock, boost_systemclock_delay_ms(5*BaseMs), cond_predicate (data->notified, 4));
BOOST_INTERPROCESS_CHECK(ret);(void)ret;
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 4);
@@ -194,7 +194,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test timed_wait
while (data->notified != 5)
data->condition.timed_wait(lock, std_systemclock_delay(5));
data->condition.timed_wait(lock, std_systemclock_delay_ms(5*BaseMs));
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 5);
data->awoken++;
@@ -202,7 +202,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test wait_until
while (data->notified != 6)
data->condition.wait_until(lock, ptime_delay(5));
data->condition.wait_until(lock, ptime_delay_ms(5*BaseMs));
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 6);
data->awoken++;
@@ -210,7 +210,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test predicate wait_until.
{
bool ret = data->condition.wait_until(lock, boost_systemclock_delay(5), cond_predicate (data->notified, 7));
bool ret = data->condition.wait_until(lock, boost_systemclock_delay_ms(5*BaseMs), cond_predicate (data->notified, 7));
BOOST_INTERPROCESS_CHECK(ret);(void)ret;
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 7);
@@ -220,7 +220,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test wait_for
while (data->notified != 8)
data->condition.wait_for(lock, ptime_seconds(5));
data->condition.wait_for(lock, ptime_ms(5*BaseMs));
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 8);
data->awoken++;
@@ -228,7 +228,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test predicate wait_for.
{
bool ret = data->condition.wait_for(lock, ptime_seconds(5), cond_predicate (data->notified, 9));
bool ret = data->condition.wait_for(lock, ptime_ms(5*BaseMs), cond_predicate (data->notified, 9));
BOOST_INTERPROCESS_CHECK(ret);(void)ret;
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 9);
@@ -238,7 +238,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test wait_for
while (data->notified != 10)
data->condition.wait_for(lock, boost_systemclock_seconds(5));
data->condition.wait_for(lock, boost_systemclock_ms(5*BaseMs));
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 10);
data->awoken++;
@@ -246,7 +246,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test predicate wait_for.
{
bool ret = data->condition.wait_for(lock, boost_systemclock_seconds(5), cond_predicate (data->notified, 11));
bool ret = data->condition.wait_for(lock, boost_systemclock_ms(5*BaseMs), cond_predicate (data->notified, 11));
BOOST_INTERPROCESS_CHECK(ret);(void)ret;
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 11);
@@ -256,7 +256,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test wait_for
while (data->notified != 12)
data->condition.wait_for(lock, std_systemclock_seconds(5));
data->condition.wait_for(lock, std_systemclock_ms(5*BaseMs));
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 12);
data->awoken++;
@@ -264,7 +264,7 @@ void condition_test_waits(condition_test_data<Condition, Mutex>* data)
// Test predicate wait_for.
{
bool ret = data->condition.wait_for(lock, std_systemclock_seconds(5), cond_predicate (data->notified, 13));
bool ret = data->condition.wait_for(lock, std_systemclock_ms(5*BaseMs), cond_predicate (data->notified, 13));
BOOST_INTERPROCESS_CHECK(ret);(void)ret;
BOOST_INTERPROCESS_CHECK(lock ? true : false);
BOOST_INTERPROCESS_CHECK(data->notified == 13);

View File

@@ -48,13 +48,13 @@ int main ()
scoped_lock<file_lock> sl(flock, try_to_lock);
}
{
scoped_lock<file_lock> sl(flock, test::ptime_delay(1));
scoped_lock<file_lock> sl(flock, test::ptime_delay_ms(1));
}
{
scoped_lock<file_lock> sl(flock, test::boost_systemclock_delay(1));
scoped_lock<file_lock> sl(flock, test::boost_systemclock_delay_ms(1));
}
{
scoped_lock<file_lock> sl(flock, test::std_systemclock_delay(1));
scoped_lock<file_lock> sl(flock, test::std_systemclock_delay_ms(1));
}
}
#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)

View File

@@ -59,7 +59,7 @@ void test_owner_dead_mutex_do_lock(M &mtx, EOwnerDeadLockType lock_type)
break;
case OwnerDeadTimedLock:
mtx.timed_lock(boost_systemclock_delay(10));
mtx.timed_lock(boost_systemclock_delay_ms(10));
break;
}
}

View File

@@ -106,7 +106,7 @@ struct test_timedlock
// Test the lock's constructors.
{
// Construct and initialize an ptime for a fast time out.
timed_lock_type lock(interprocess_mutex, ptime_delay(1*BaseSeconds));
timed_lock_type lock(interprocess_mutex, ptime_delay_ms(unsigned(1*BaseMs)));
BOOST_INTERPROCESS_CHECK(lock ? true : false);
}
{
@@ -123,7 +123,7 @@ struct test_timedlock
BOOST_INTERPROCESS_CHECK(lock ? true : false);
lock.unlock();
BOOST_INTERPROCESS_CHECK(!lock);
BOOST_INTERPROCESS_CHECK(lock.timed_lock(boost_systemclock_delay(3*BaseSeconds)));
BOOST_INTERPROCESS_CHECK(lock.timed_lock(boost_systemclock_delay_ms(1*BaseMs)));
BOOST_INTERPROCESS_CHECK(lock ? true : false);
}
};
@@ -158,15 +158,15 @@ struct test_recursive_lock
}
{
//This should always lock
lock_twice_timed<lock_type>(mx, ptime_delay(2*BaseSeconds));
lock_twice_timed<lock_type>(mx, ptime_delay_ms(2*BaseMs));
}
{
//This should always lock
lock_twice_timed<lock_type>(mx, boost_systemclock_delay(2*BaseSeconds));
lock_twice_timed<lock_type>(mx, boost_systemclock_delay_ms(2*BaseMs));
}
{
//This should always lock
lock_twice_timed<lock_type>(mx, std_systemclock_delay(2*BaseSeconds));
lock_twice_timed<lock_type>(mx, std_systemclock_delay_ms(2*BaseMs));
}
}
};
@@ -179,11 +179,11 @@ void lock_and_sleep(void *arg, M &sm)
{
data<M> *pdata = static_cast<data<M>*>(arg);
boost::interprocess::scoped_lock<M> l(sm);
if(pdata->m_secs){
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*pdata->m_secs));
if(pdata->m_msecs){
boost::interprocess::ipcdetail::thread_sleep_ms(unsigned(pdata->m_msecs));
}
else{
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*2*BaseSeconds));
boost::interprocess::ipcdetail::thread_sleep_ms(unsigned(2*BaseMs));
}
++shared_val;
@@ -193,21 +193,13 @@ void lock_and_sleep(void *arg, M &sm)
template<typename M>
void lock_and_catch_errors(void *arg, M &sm)
{
data<M> *pdata = static_cast<data<M>*>(arg);
BOOST_TRY
{
boost::interprocess::scoped_lock<M> l(sm);
if(pdata->m_secs){
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*pdata->m_secs));
}
else{
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*2*BaseSeconds));
}
++shared_val;
pdata->m_value = shared_val;
lock_and_sleep(arg, sm);
}
BOOST_CATCH(interprocess_exception const & e)
{
data<M>* pdata = static_cast<data<M>*>(arg);
pdata->m_error = e.get_error_code();
} BOOST_CATCH_END
}
@@ -218,7 +210,7 @@ void try_lock_and_sleep(void *arg, M &sm)
data<M> *pdata = static_cast<data<M>*>(arg);
boost::interprocess::scoped_lock<M> l(sm, boost::interprocess::defer_lock);
if (l.try_lock()){
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*2*BaseSeconds));
boost::interprocess::ipcdetail::thread_sleep_ms(unsigned(2*BaseMs));
++shared_val;
pdata->m_value = shared_val;
}
@@ -240,17 +232,17 @@ void timed_lock_and_sleep(void *arg, M &sm)
l (sm, boost::interprocess::defer_lock);
bool r = false;
if(pdata->m_flags == (int)TimedLock){
r = l.timed_lock(std_systemclock_delay(pdata->m_secs));
r = l.timed_lock(std_systemclock_delay_ms(unsigned(pdata->m_msecs)));
}
else if (pdata->m_flags == (int)TryLockUntil) {
r = l.try_lock_until(ptime_delay(pdata->m_secs));
r = l.try_lock_until(ptime_delay_ms(unsigned(pdata->m_msecs)));
}
else if (pdata->m_flags == (int)TryLockFor) {
r = l.try_lock_for(boost_systemclock_seconds(pdata->m_secs));
r = l.try_lock_for(boost_systemclock_ms(unsigned(pdata->m_msecs)));
}
if (r){
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*2*BaseSeconds));
boost::interprocess::ipcdetail::thread_sleep_ms(unsigned(2*BaseMs));
++shared_val;
pdata->m_value = shared_val;
}
@@ -266,21 +258,21 @@ void test_mutex_lock()
data<M> d1(1);
data<M> d2(2);
// Locker one launches, holds the lock for 2*BaseSeconds seconds.
// Locker one launches, holds the lock for 2*BaseMs seconds.
boost::interprocess::ipcdetail::OS_thread_t tm1;
boost::interprocess::ipcdetail::thread_launch(tm1, thread_adapter<M>(&lock_and_sleep, &d1, mtx));
//Wait 1*BaseSeconds
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*1*BaseSeconds));
//Wait 1*BaseMs
boost::interprocess::ipcdetail::thread_sleep_ms(unsigned(1*BaseMs));
// Locker two launches, but it won't hold the lock for 2*BaseSeconds seconds.
// Locker two launches, but it won't hold the lock for 2*BaseMs seconds.
boost::interprocess::ipcdetail::OS_thread_t tm2;
boost::interprocess::ipcdetail::thread_launch(tm2, thread_adapter<M>(&lock_and_sleep, &d2, mtx));
//Wait completion
boost::interprocess::ipcdetail::thread_join(tm1);
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*1*BaseSeconds));
boost::interprocess::ipcdetail::thread_sleep_ms(unsigned(1*BaseMs));
boost::interprocess::ipcdetail::thread_join(tm2);
BOOST_INTERPROCESS_CHECK(d1.m_value == 1);
@@ -294,28 +286,25 @@ void test_mutex_lock_timeout()
M mtx;
int wait_time_s = BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS / 1000;
if (wait_time_s == 0 )
wait_time_s = 1;
unsigned wait_time_ms = BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS;
data<M> d1(1, wait_time_s * 3);
data<M> d2(2, wait_time_s * 2);
data<M> d1(1, (int)wait_time_ms * 3);
data<M> d2(2, (int)wait_time_ms * 1);
// Locker one launches, and holds the lock for wait_time_s * 2 seconds.
// Locker one launches, and holds the lock for wait_time_ms * 3.
boost::interprocess::ipcdetail::OS_thread_t tm1;
boost::interprocess::ipcdetail::thread_launch(tm1, thread_adapter<M>(&lock_and_sleep, &d1, mtx));
//Wait 1*BaseSeconds
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*wait_time_s));
//Wait until tm1 acquires the lock
boost::interprocess::ipcdetail::thread_sleep_ms(wait_time_ms);
// Locker two launches, and attempts to hold the lock for wait_time_s * 2 seconds.
// Locker two launches, and attempts to hold the lock for wait_time_ms * 2.
boost::interprocess::ipcdetail::OS_thread_t tm2;
boost::interprocess::ipcdetail::thread_launch(tm2, thread_adapter<M>(&lock_and_catch_errors, &d2, mtx));
//Wait completion
boost::interprocess::ipcdetail::thread_join(tm1);
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*1*BaseSeconds));
boost::interprocess::ipcdetail::thread_join(tm2);
boost::interprocess::ipcdetail::thread_join(tm1);
BOOST_INTERPROCESS_CHECK(d1.m_value == 1);
BOOST_INTERPROCESS_CHECK(d2.m_value == -1);
@@ -333,12 +322,12 @@ void test_mutex_try_lock()
data<M> d1(1);
data<M> d2(2);
// Locker one launches, holds the lock for 2*BaseSeconds seconds.
// Locker one launches, holds the lock for 2*BaseMs seconds.
boost::interprocess::ipcdetail::OS_thread_t tm1;
boost::interprocess::ipcdetail::thread_launch(tm1, thread_adapter<M>(&try_lock_and_sleep, &d1, mtx));
//Wait 1*BaseSeconds
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*1*BaseSeconds));
//Wait 1*BaseMs
boost::interprocess::ipcdetail::thread_sleep_ms(unsigned(1*BaseMs));
// Locker two launches, but it should fail acquiring the lock
boost::interprocess::ipcdetail::OS_thread_t tm2;
@@ -362,17 +351,17 @@ void test_mutex_timed_lock()
M mtx, m2;
data<M> d1(1, 2*BaseSeconds, flag);
data<M> d2(2, 2*BaseSeconds, flag);
data<M> d1(1, 2*BaseMs, flag);
data<M> d2(2, 2*BaseMs, flag);
// Locker one launches, holds the lock for 2*BaseSeconds seconds.
// Locker one launches, holds the lock for 2*BaseMs seconds.
boost::interprocess::ipcdetail::OS_thread_t tm1;
boost::interprocess::ipcdetail::thread_launch(tm1, thread_adapter<M>(&timed_lock_and_sleep, &d1, mtx));
//Wait 1*BaseSeconds
boost::interprocess::ipcdetail::thread_sleep(unsigned(1000*1*BaseSeconds));
//Wait 1*BaseMs
boost::interprocess::ipcdetail::thread_sleep_ms(unsigned(1*BaseMs));
// Locker two launches, holds the lock for 2*BaseSeconds seconds.
// Locker two launches, holds the lock for 2*BaseMs seconds.
boost::interprocess::ipcdetail::OS_thread_t tm2;
boost::interprocess::ipcdetail::thread_launch(tm2, thread_adapter<M>(&timed_lock_and_sleep, &d2, mtx));

View File

@@ -9,9 +9,10 @@
//////////////////////////////////////////////////////////////////////////////
// enable timeout feature
#define BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS boost::interprocess::test::BaseMs
#define BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
#define BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS 6000
#include "util.hpp"
#include <boost/assert.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>

View File

@@ -68,13 +68,13 @@ int main ()
{
upgradable_lock<Mutex> u_lock(mut);
//This calls timed_unlock_upgradable_and_lock()
scoped_lock<Mutex> e_lock(boost::move(u_lock), test::boost_systemclock_delay(10));
scoped_lock<Mutex> e_lock(boost::move(u_lock), test::boost_systemclock_delay_ms(10));
}
{
upgradable_lock<Mutex> u_lock(mut);
//This calls timed_unlock_upgradable_and_lock()
scoped_lock<Mutex> e_lock(mut2);
scoped_lock<Mutex> moved(boost::move(u_lock), test::ptime_delay(10));
scoped_lock<Mutex> moved(boost::move(u_lock), test::ptime_delay_ms(10));
e_lock = boost::move(moved);
}
{