ts_RWLock.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #include <boost/test/unit_test.hpp>
00024 #include "icl_core_thread/Thread.h"
00025 #include "icl_core_thread/RWLock.h"
00026 #include "icl_core/os_time.h"
00027 
00028 using icl_core::TimeSpan;
00029 using icl_core::TimeStamp;
00030 using icl_core::thread::RWLock;
00031 using icl_core::thread::Mutex;
00032 using icl_core::thread::Thread;
00033 
00034 
00035 class RWLockTestThread : public Thread
00036 {
00037 public:
00038   RWLockTestThread(RWLock *rwlock, bool read_lock)
00039     : Thread("Test Thread"),
00040       m_has_run(false),
00041       m_read_lock(read_lock),
00042       m_rwlock(rwlock)
00043   { }
00044 
00045   virtual ~RWLockTestThread()
00046   { }
00047 
00048   virtual void run()
00049   {
00050     if (m_read_lock)
00051     {
00052       BOOST_CHECK(m_rwlock->tryReadLock());
00053       BOOST_CHECK(m_rwlock->readLock(icl_core::TimeSpan(1, 0)));
00054       BOOST_CHECK(m_rwlock->readLock(icl_core::TimeStamp::now() + icl_core::TimeSpan(1, 0)));
00055     }
00056     else
00057     {
00058       BOOST_CHECK(!m_rwlock->tryReadLock());
00059       BOOST_CHECK(!m_rwlock->readLock(icl_core::TimeSpan(1, 0)));
00060       BOOST_CHECK(!m_rwlock->readLock(icl_core::TimeStamp::now() + icl_core::TimeSpan(1, 0)));
00061     }
00062 
00063     BOOST_CHECK(!m_rwlock->tryWriteLock());
00064     BOOST_CHECK(!m_rwlock->writeLock(icl_core::TimeSpan(1, 0)));
00065     BOOST_CHECK(!m_rwlock->writeLock(icl_core::TimeStamp::now() + icl_core::TimeSpan(1, 0)));
00066 
00067     m_has_run = true;
00068   }
00069 
00070   bool hasRun() const { return m_has_run; }
00071 
00072 private:
00073   bool m_has_run;
00074   bool m_read_lock;
00075   RWLock *m_rwlock;
00076 };
00077 
00078 
00079 BOOST_AUTO_TEST_SUITE(ts_RWLock)
00080 
00081 BOOST_AUTO_TEST_CASE(ReadLock)
00082 {
00083   RWLock rwlock;
00084   BOOST_CHECK(rwlock.readLock());
00085   rwlock.unlock();
00086 }
00087 
00088 BOOST_AUTO_TEST_CASE(TryReadLock)
00089 {
00090   RWLock rwlock;
00091   BOOST_CHECK(rwlock.tryReadLock());
00092   rwlock.unlock();
00093 }
00094 
00095 BOOST_AUTO_TEST_CASE(ReadLockAbsoluteTimeout)
00096 {
00097   RWLock rwlock;
00098   BOOST_CHECK(rwlock.readLock(TimeStamp::now() + TimeSpan(1, 0)));
00099   rwlock.unlock();
00100 }
00101 
00102 BOOST_AUTO_TEST_CASE(ReadLockRelativeTimeout)
00103 {
00104   RWLock rwlock;
00105   BOOST_CHECK(rwlock.readLock(TimeSpan(1, 0)));
00106   rwlock.unlock();
00107 }
00108 
00109 BOOST_AUTO_TEST_CASE(WriteLock)
00110 {
00111   RWLock rwlock;
00112   BOOST_CHECK(rwlock.writeLock());
00113   rwlock.unlock();
00114 }
00115 
00116 BOOST_AUTO_TEST_CASE(TryWriteLock)
00117 {
00118   RWLock rwlock;
00119   BOOST_CHECK(rwlock.tryWriteLock());
00120   rwlock.unlock();
00121 }
00122 
00123 BOOST_AUTO_TEST_CASE(WriteLockAbsoluteTimeout)
00124 {
00125   RWLock rwlock;
00126   BOOST_CHECK(rwlock.writeLock(TimeStamp::now() + TimeSpan(1, 0)));
00127   rwlock.unlock();
00128 }
00129 
00130 BOOST_AUTO_TEST_CASE(WriteLockRelativeTimeout)
00131 {
00132   RWLock rwlock;
00133   BOOST_CHECK(rwlock.writeLock(TimeSpan(1, 0)));
00134   rwlock.unlock();
00135 }
00136 
00137 BOOST_AUTO_TEST_CASE(ReadAndWriteLockWhileReadLock)
00138 {
00139   RWLock rwlock;
00140 
00141   BOOST_CHECK(rwlock.readLock());
00142 
00143   RWLockTestThread *testthread = new RWLockTestThread(&rwlock, true);
00144 
00145   testthread->start();
00146   testthread->join();
00147   BOOST_CHECK(testthread->hasRun());
00148 
00149   delete testthread;
00150 
00151   rwlock.unlock();
00152 }
00153 
00154 BOOST_AUTO_TEST_CASE(ReadAndWriteLockWhileWriteLock)
00155 {
00156   RWLock rwlock;
00157 
00158   BOOST_CHECK(rwlock.writeLock());
00159 
00160   RWLockTestThread *testthread = new RWLockTestThread(&rwlock, false);
00161 
00162   testthread->start();
00163   testthread->join();
00164   BOOST_CHECK(testthread->hasRun());
00165 
00166   delete testthread;
00167 
00168   rwlock.unlock();
00169 }
00170 
00171 BOOST_AUTO_TEST_SUITE_END()


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04