exception_selftest.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 the <ORGANIZATION> 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 
35 #include <ros/ros.h>
36 
37 #include "diagnostic_msgs/SelfTest.h"
38 
39 #include "self_test/self_test.h"
40 
41 #include <stdexcept>
42 
43 // using namespace std;
44 
45 class MyNode
46 {
47 public:
48 
49  // self_test::TestRunner is the handles sequencing driver self-tests.
51 
52  // A value showing statefulness of tests
53  double some_val;
54 
56 
57  MyNode() : self_test_()
58  {
59  self_test_.add("Pretest", this, &MyNode::pretest );
60 
61  self_test_.add("ID Lookup", this, &MyNode::test1);
62  self_test_.add("Exception generating test", this, &MyNode::test2);
63  self_test_.add("Value generating test", this, &MyNode::test3);
64  self_test_.add("Value testing test", this, &MyNode::test4);
65 
66  self_test_.add("Posttest", this, &MyNode::pretest );
67  }
68 
70  {
71  ROS_INFO("Doing preparation stuff before we run our test.\n");
72  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Pretest completed successfully.");
73 
74  some_val = 1.0;
75  }
76 
78  {
79  // Look up ID here
80  char ID[] = "12345";
81  bool lookup_successful = true;
82 
83  if (lookup_successful)
84  {
85  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "ID Lookup successful");
86 
87  self_test_.setID(ID);
88 
89  } else {
90  status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "ID Lookup failed");
91  }
92  }
93 
94  // Throwing an exception
96  {
97  status.level = 0;
98 
99  throw std::runtime_error("we did something that threw an exception");
100 
101  // Here's where we would report success if we'd made it past
102  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We made it past the exception throwing statement.");
103  }
104 
106  {
107  some_val += 41.0;
108 
109  status.add("some value", some_val);
110  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We successfully changed the value.");
111  }
112 
114  {
115  if (some_val == 42.0)
116  {
117  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We observed the change in value");
118  }
119  else
120  {
121  status.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "We failed to observe the change in value, it is currently %f.", some_val);
122  }
123  }
124 
126  {
127  ROS_INFO("Doing cleanup stuff after we run our test.\n");
128  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Posttest completed successfully.");
129  }
130 
131  bool spin()
132  {
133  while (nh_.ok())
134  {
135  ros::Duration(1).sleep();
136 
137  self_test_.checkTest();
138  }
139  return true;
140  }
141 };
142 
143 int
144 main(int argc, char** argv)
145 {
146  ros::init(argc, argv, "my_node");
147 
148  MyNode n;
149 
150  n.spin();
151 
152  return(0);
153 }
double some_val
void test4(diagnostic_updater::DiagnosticStatusWrapper &status)
int main(int argc, char **argv)
void summary(unsigned char lvl, const std::string s)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void add(const std::string &name, TaskFunction f)
void test3(diagnostic_updater::DiagnosticStatusWrapper &status)
void pretest(diagnostic_updater::DiagnosticStatusWrapper &status)
ros::NodeHandle nh_
void summaryf(unsigned char lvl, const char *format,...)
void test2(diagnostic_updater::DiagnosticStatusWrapper &status)
Simple node with a self test that reports an error.
#define ROS_INFO(...)
void checkTest()
Check if a self-test is pending. If so, start it and wait for it to complete.
Definition: self_test.h:107
Class to facilitate the creation of component self-tests.
Definition: self_test.h:68
void setID(std::string id)
Sets the ID of the part being tested.
Definition: self_test.h:121
void test1(diagnostic_updater::DiagnosticStatusWrapper &status)
void posttest(diagnostic_updater::DiagnosticStatusWrapper &status)
bool ok() const
void add(const std::string &key, const T &val)
self_test::TestRunner self_test_


self_test
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs and Blaise Gassend
autogenerated on Wed Mar 27 2019 03:02:26