ts_Mutex.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/Mutex.h>
00025 #include <icl_core_thread/Thread.h>
00026 
00027 using icl_core::TimeSpan;
00028 using icl_core::TimeStamp;
00029 using icl_core::thread::Mutex;
00030 using icl_core::thread::Thread;
00031 
00032 const TimeSpan timeout(1, 0);
00033 
00034 class MutexTestThread : public Thread
00035 {
00036 public:
00037   MutexTestThread(Mutex *mutex) :
00038     Thread("Mutex Test Thread"),
00039     m_has_run(false),
00040     m_mutex(mutex)
00041   {
00042   }
00043 
00044   virtual ~MutexTestThread()
00045   {
00046   }
00047 
00048   virtual void run()
00049   {
00050     BOOST_CHECK(!m_mutex->tryLock());
00051     BOOST_CHECK(!m_mutex->lock(timeout));
00052     BOOST_CHECK(!m_mutex->lock(TimeStamp::now() + timeout));
00053 
00054     m_has_run = true;
00055   }
00056 
00057   bool hasRun() const { return m_has_run; }
00058 
00059 private:
00060   bool m_has_run;
00061   Mutex *m_mutex;
00062 };
00063 
00064 BOOST_AUTO_TEST_SUITE(ts_Mutex)
00065 
00066 BOOST_AUTO_TEST_CASE(MutexLock)
00067 {
00068   Mutex mutex;
00069   BOOST_CHECK(mutex.lock());
00070   mutex.unlock();
00071 }
00072 
00073 BOOST_AUTO_TEST_CASE(MutexTryLock)
00074 {
00075   Mutex mutex;
00076   BOOST_CHECK(mutex.tryLock());
00077   mutex.unlock();
00078 }
00079 
00080 BOOST_AUTO_TEST_CASE(MutexLockAbsoluteTimeout)
00081 {
00082   Mutex mutex;
00083   BOOST_CHECK(mutex.lock(TimeStamp::now() + TimeSpan(1, 0)));
00084   mutex.unlock();
00085 }
00086 
00087 BOOST_AUTO_TEST_CASE(MutexLockRelativeTimeout)
00088 {
00089   Mutex mutex;
00090   BOOST_CHECK(mutex.lock(TimeSpan(1, 0)));
00091   mutex.unlock();
00092 }
00093 
00094 BOOST_AUTO_TEST_CASE(MultiThreadMutexTest)
00095 {
00096   Mutex mutex;
00097 
00098   BOOST_CHECK(mutex.lock());
00099 
00100   MutexTestThread *thread = new MutexTestThread(&mutex);
00101 
00102   thread->start();
00103   thread->join();
00104   BOOST_CHECK(thread->hasRun());
00105 
00106   delete thread;
00107 
00108   mutex.unlock();
00109 }
00110 
00111 BOOST_AUTO_TEST_SUITE_END()


fzi_icl_core
Author(s):
autogenerated on Thu Jun 6 2019 20:22:24