Lunchbox  1.17.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
test.h
1 
2 /* Copyright (c) 2005-2014, Stefan Eilemann <eile@equalizergraphics.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * - Neither the name of Eyescale Software GmbH nor the names of its
13  * contributors may be used to endorse or promote products derived from this
14  * software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef LBTEST_TEST_H
30 #define LBTEST_TEST_H
31 
32 #include <lunchbox/log.h>
33 #include <lunchbox/sleep.h>
34 #include <lunchbox/thread.h>
35 
36 #include <cstdlib>
37 #include <fstream>
38 #include <stdexcept>
39 
40 #define OUTPUT lunchbox::Log::instance(__FILE__, __LINE__)
41 
42 #define TEST(x) \
43  { \
44  LBVERB << "Test " << #x << std::endl; \
45  if (!(x)) \
46  { \
47  OUTPUT << #x << " failed (l." << __LINE__ << ')' << std::endl; \
48  lunchbox::abort(); \
49  ::exit(EXIT_FAILURE); \
50  } \
51  }
52 
53 #define TESTINFO(x, info) \
54  { \
55  LBVERB << "Test " << #x << ": " << info << std::endl; \
56  if (!(x)) \
57  { \
58  OUTPUT << #x << " failed (l." << __LINE__ << "): " << info \
59  << std::endl; \
60  lunchbox::abort(); \
61  ::exit(EXIT_FAILURE); \
62  } \
63  }
64 
65 #define TESTRESULT(x, type) \
66  { \
67  LBVERB << "Test " << #x << std::endl; \
68  const type& testRes = (x); \
69  if (!testRes) \
70  { \
71  OUTPUT << #x << " failed with " << testRes << " (l." << __LINE__ \
72  << ")" << std::endl; \
73  lunchbox::abort(); \
74  ::exit(EXIT_FAILURE); \
75  } \
76  }
77 
78 int testMain(int argc, char** argv);
79 
80 namespace
81 {
82 class Watchdog : public lunchbox::Thread
83 {
84 public:
85  explicit Watchdog(const std::string& name)
86  : _name(name)
87  {
88  }
89 
90  virtual void run()
91  {
92  lunchbox::Thread::setName("Watchdog");
93 #ifdef TEST_RUNTIME
94  lunchbox::sleep(TEST_RUNTIME * 1000);
95  std::cerr << "Watchdog triggered - " << _name
96  << " did not terminate within " << TEST_RUNTIME << "s"
97  << std::endl;
98 #else
99  lunchbox::sleep(60000);
100  std::cerr << "Watchdog triggered - " << _name
101  << " did not terminate within 1 minute" << std::endl;
102 #endif
103  lunchbox::abort(true /*dumpThreads*/);
104  }
105 
106 private:
107  const std::string _name;
108 };
109 }
110 
111 int main(int argc, char** argv)
112 {
113 #ifndef TEST_NO_WATCHDOG
114  Watchdog watchdog(argv[0]);
115  watchdog.start();
116 #endif
117 
118  try
119  {
120  const int result = testMain(argc, argv);
121  if (result != EXIT_SUCCESS)
122  return result;
123  }
124  catch (const std::runtime_error& e)
125  {
126  LBINFO << "Test exception: " << e.what() << std::endl;
127  return EXIT_FAILURE;
128  }
129 
130 #ifndef TEST_NO_WATCHDOG
131  watchdog.cancel();
132  lunchbox::sleep(10); // give watchdog time to terminate
133 #endif
134  return EXIT_SUCCESS;
135 }
136 
137 #define main testMain
138 
139 #endif // LBTEST_TEST_H
Utility class to execute code in a separate execution thread.
Definition: thread.h:44
void sleep(const uint32_t milliSeconds)
Sleep the current thread for a number of milliseconds.
#define LBINFO
Output an informational message to the per-thread Log.
Definition: log.h:199
This file contains logging classes.
virtual void run()=0
The entry function for the child thread.