thread_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "ros/console.h"
31 
32 #include <gtest/gtest.h>
33 
34 #include <boost/thread.hpp>
35 
36 #include "log4cxx/appenderskeleton.h"
37 #include "log4cxx/spi/loggingevent.h"
38 
39 #include <vector>
40 
41 class TestAppender : public log4cxx::AppenderSkeleton
42 {
43 public:
44  struct Info
45  {
46  log4cxx::LevelPtr level_;
47  std::string message_;
48  std::string logger_name_;
49  };
50 
51  typedef std::vector<Info> V_Info;
52 
53  V_Info info_;
54 
55 protected:
56  virtual void append(const log4cxx::spi::LoggingEventPtr& event, log4cxx::helpers::Pool&)
57  {
58  Info info;
59  info.level_ = event->getLevel();
60  info.message_ = event->getMessage();
61  info.logger_name_ = event->getLoggerName();
62 
63  info_.push_back( info );
64  }
65 
66  virtual void close()
67  {
68  }
69  virtual bool requiresLayout() const
70  {
71  return false;
72  }
73 };
74 
75 void threadFunc(boost::barrier* b)
76 {
77  b->wait();
78  ROS_INFO("Hello");
79 }
80 
81 // Ensure all threaded calls go out
82 TEST(Rosconsole, threadedCalls)
83 {
84  log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
85 
86  TestAppender* appender = new TestAppender;
87  logger->addAppender( appender );
88 
89  boost::thread_group tg;
90  boost::barrier b(10);
91  for (uint32_t i = 0; i < 10; ++i)
92  {
93  tg.create_thread(boost::bind(threadFunc, &b));
94  }
95  tg.join_all();
96 
97  ASSERT_EQ(appender->info_.size(), 10ULL);
98 
99  logger->removeAppender(appender);
100 }
101 
102 int main(int argc, char **argv)
103 {
104  testing::InitGoogleTest(&argc, argv);
105  ros::Time::init();
106 
107  return RUN_ALL_TESTS();
108 }
TEST(Rosconsole, threadedCalls)
Definition: thread_test.cpp:82
virtual void close()
Definition: thread_test.cpp:66
std::string logger_name_
Definition: thread_test.cpp:48
int main(int argc, char **argv)
#define ROS_INFO(...)
log4cxx::LevelPtr level_
Definition: thread_test.cpp:46
std::vector< Info > V_Info
Definition: thread_test.cpp:51
static void init()
void threadFunc(boost::barrier *b)
Definition: thread_test.cpp:75
virtual bool requiresLayout() const
Definition: thread_test.cpp:69
virtual void append(const log4cxx::spi::LoggingEventPtr &event, log4cxx::helpers::Pool &)
Definition: thread_test.cpp:56
#define ROSCONSOLE_DEFAULT_NAME
Definition: console.h:299
std::string message_
Definition: thread_test.cpp:47


rosconsole
Author(s): Josh Faust
autogenerated on Mon Nov 2 2020 03:52:16