Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00036
00037 #include <gtest/gtest.h>
00038 #include <actionlib/destruction_guard.h>
00039 #include <boost/thread.hpp>
00040 #include <boost/bind.hpp>
00041
00042 using namespace actionlib;
00043
00044 class TestRunner : public testing::Test
00045 {
00046 public:
00047 TestRunner() : done_protecting_(false)
00048 {
00049
00050 }
00051
00052 void protectingThread()
00053 {
00054 DestructionGuard::ScopedProtector protector_1(guard_);
00055 EXPECT_TRUE(protector_1.isProtected());
00056
00057 DestructionGuard::ScopedProtector protector_2(guard_);
00058 EXPECT_TRUE(protector_2.isProtected());
00059
00060
00061 {
00062 boost::mutex::scoped_lock lock(mutex_);
00063 done_protecting_ = true;
00064 cond_.notify_all();
00065 }
00066
00067
00068
00069 printf("protecting thread is sleeping\n");
00070 usleep(5000000);
00071 printf("protecting thread is exiting\n");
00072 }
00073
00074 protected:
00075 DestructionGuard guard_;
00076
00077 bool done_protecting_;
00078 boost::mutex mutex_;
00079 boost::condition cond_;
00080 };
00081
00082
00083 TEST_F(TestRunner, threaded_test)
00084 {
00085 boost::thread spin_thread(boost::bind(&TestRunner::protectingThread, this));
00086
00087 {
00088 boost::mutex::scoped_lock lock(mutex_);
00089 while (!done_protecting_)
00090 {
00091 cond_.timed_wait(lock, boost::posix_time::milliseconds(100.0f));
00092 }
00093 }
00094
00095 printf("About to destruct\n");
00096 guard_.destruct();
00097 printf("Done destructing\n");
00098
00099
00100 DestructionGuard::ScopedProtector protector(guard_);
00101 EXPECT_FALSE(protector.isProtected());
00102
00103 spin_thread.join();
00104 }
00105
00106
00107 TEST(DestructionGuard, easy_test)
00108 {
00109 DestructionGuard guard;
00110
00111 {
00112 DestructionGuard::ScopedProtector protector_1(guard);
00113 EXPECT_TRUE(protector_1.isProtected());
00114
00115 DestructionGuard::ScopedProtector protector_2(guard);
00116 EXPECT_TRUE(protector_2.isProtected());
00117 }
00118
00119 guard.destruct();
00120
00121 {
00122 DestructionGuard::ScopedProtector protector_3(guard);
00123 EXPECT_FALSE(protector_3.isProtected());
00124 }
00125 }
00126
00127
00128 int main(int argc, char **argv){
00129 testing::InitGoogleTest(&argc, argv);
00130
00131
00132 return RUN_ALL_TESTS();
00133 }