destruction_guard_test.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
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     // Let the main thread know that we've successfully created to protectors
00061     {
00062       boost::mutex::scoped_lock lock(mutex_);
00063       done_protecting_ = true;
00064       cond_.notify_all();
00065     }
00066 
00067     // Don't destruct the protectors immeadiately. Sleep for a little bit, and then destruct.
00068     //  This will force the main thread to have to wait in it's destruct() call
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   // Already 'destructed', so protector should fail
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 }


actionlib
Author(s): Eitan Marder-Eppstein, Vijay Pradeep
autogenerated on Sun Oct 5 2014 22:02:55