destruction_guard_test.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
36 
37 #include <gtest/gtest.h>
39 #include <boost/thread.hpp>
40 #include <boost/bind.hpp>
41 
42 using namespace actionlib;
43 
44 class TestRunner : public testing::Test
45 {
46 public:
48  : done_protecting_(false)
49  {
50  }
51 
53  {
54  DestructionGuard::ScopedProtector protector_1(guard_);
55  EXPECT_TRUE(protector_1.isProtected());
56 
57  DestructionGuard::ScopedProtector protector_2(guard_);
58  EXPECT_TRUE(protector_2.isProtected());
59 
60  // Let the main thread know that we've successfully created to protectors
61  {
62  boost::mutex::scoped_lock lock(mutex_);
63  done_protecting_ = true;
64  cond_.notify_all();
65  }
66 
67  // Don't destruct the protectors immeadiately. Sleep for a little bit, and then destruct.
68  // This will force the main thread to have to wait in it's destruct() call
69  printf("protecting thread is sleeping\n");
70  boost::this_thread::sleep(boost::posix_time::microseconds(5000000));
71  printf("protecting thread is exiting\n");
72  }
73 
74 protected:
76 
78  boost::mutex mutex_;
79  boost::condition cond_;
80 };
81 
82 
83 TEST_F(TestRunner, threaded_test) {
84  boost::thread spin_thread(boost::bind(&TestRunner::protectingThread, this));
85 
86  {
87  boost::mutex::scoped_lock lock(mutex_);
88  while (!done_protecting_) {
89  cond_.timed_wait(lock, boost::posix_time::milliseconds(100));
90  }
91  }
92 
93  printf("About to destruct\n");
94  guard_.destruct();
95  printf("Done destructing\n");
96 
97  // Already 'destructed', so protector should fail
98  DestructionGuard::ScopedProtector protector(guard_);
99  EXPECT_FALSE(protector.isProtected());
100 
101  spin_thread.join();
102 }
103 
104 
105 TEST(DestructionGuard, easy_test) {
106  DestructionGuard guard;
107 
108  {
109  DestructionGuard::ScopedProtector protector_1(guard);
110  EXPECT_TRUE(protector_1.isProtected());
111 
112  DestructionGuard::ScopedProtector protector_2(guard);
113  EXPECT_TRUE(protector_2.isProtected());
114  }
115 
116  guard.destruct();
117 
118  {
119  DestructionGuard::ScopedProtector protector_3(guard);
120  EXPECT_FALSE(protector_3.isProtected());
121  }
122 }
123 
124 
125 int main(int argc, char ** argv)
126 {
127  testing::InitGoogleTest(&argc, argv);
128 
129 
130  return RUN_ALL_TESTS();
131 }
bool isProtected()
Checks if the ScopedProtector successfully protected the DestructionGuard.
TEST(DestructionGuard, easy_test)
This class protects an object from being destructed until all users of that object relinquish control...
int main(int argc, char **argv)
boost::condition cond_
Protects a DestructionGuard until this object goes out of scope.
TEST_F(TestRunner, threaded_test)
DestructionGuard guard_


actionlib
Author(s): Eitan Marder-Eppstein, Vijay Pradeep, Mikael Arguedas
autogenerated on Mon Feb 28 2022 21:34:38