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()
00048 : done_protecting_(false)
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 boost::this_thread::sleep(boost::posix_time::microseconds(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 boost::thread spin_thread(boost::bind(&TestRunner::protectingThread, this));
00085
00086 {
00087 boost::mutex::scoped_lock lock(mutex_);
00088 while (!done_protecting_) {
00089 cond_.timed_wait(lock, boost::posix_time::milliseconds(100));
00090 }
00091 }
00092
00093 printf("About to destruct\n");
00094 guard_.destruct();
00095 printf("Done destructing\n");
00096
00097
00098 DestructionGuard::ScopedProtector protector(guard_);
00099 EXPECT_FALSE(protector.isProtected());
00100
00101 spin_thread.join();
00102 }
00103
00104
00105 TEST(DestructionGuard, easy_test) {
00106 DestructionGuard guard;
00107
00108 {
00109 DestructionGuard::ScopedProtector protector_1(guard);
00110 EXPECT_TRUE(protector_1.isProtected());
00111
00112 DestructionGuard::ScopedProtector protector_2(guard);
00113 EXPECT_TRUE(protector_2.isProtected());
00114 }
00115
00116 guard.destruct();
00117
00118 {
00119 DestructionGuard::ScopedProtector protector_3(guard);
00120 EXPECT_FALSE(protector_3.isProtected());
00121 }
00122 }
00123
00124
00125 int main(int argc, char ** argv)
00126 {
00127 testing::InitGoogleTest(&argc, argv);
00128
00129
00130 return RUN_ALL_TESTS();
00131 }