File: backtrace_test.cpp

package info (click to toggle)
ruby-passenger 3.0.13debian-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,920 kB
  • sloc: cpp: 99,104; ruby: 18,098; ansic: 9,846; sh: 8,632; python: 141; makefile: 30
file content (88 lines) | stat: -rw-r--r-- 2,573 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "../tut/tut.h"
#include "counter.hpp"
#include <oxt/backtrace.hpp>
#include <oxt/tracable_exception.hpp>
#include <oxt/thread.hpp>

using namespace oxt;
using namespace std;

namespace tut {
	struct backtrace_test {
	};
	
	DEFINE_TEST_GROUP(backtrace_test);

	TEST_METHOD(1) {
		// Test TRACE_POINT() and tracable_exception.
		struct {
			void foo() {
				TRACE_POINT();
				bar();
			}
			
			void bar() {
				TRACE_POINT();
				baz();
			}
			
			void baz() {
				TRACE_POINT();
				throw tracable_exception();
			}
		} object;
		
		try {
			object.foo();
			fail("tracable_exception expected.");
		} catch (const tracable_exception &e) {
			ensure("Backtrace contains foo()",
				e.backtrace().find("foo()") != string::npos);
			ensure("Backtrace contains bar()",
				e.backtrace().find("bar()") != string::npos);
			ensure("Backtrace contains baz()",
				e.backtrace().find("baz()") != string::npos);
		}
	}
	
	static void foo(CounterPtr parent_counter, CounterPtr child_counter) {
		TRACE_POINT();
		child_counter->increment();    // Tell parent that we've created the trace point.
		parent_counter->wait_until(1); // Wait until parent thread says we can exit.
	}
	
	static void bar(CounterPtr parent_counter, CounterPtr child_counter) {
		TRACE_POINT();
		child_counter->increment();    // Tell parent that we've created the trace point.
		parent_counter->wait_until(1); // Wait until parent thread says we can exit.
	}
	
	TEST_METHOD(2) {
		// Test whether oxt::thread's backtrace support works.
		CounterPtr parent_counter = Counter::create_ptr();
		CounterPtr child_counter  = Counter::create_ptr();
		oxt::thread foo_thread(boost::bind(foo, parent_counter, child_counter));
		oxt::thread bar_thread(boost::bind(bar, parent_counter, child_counter));
		
		// Wait until all threads have created trace points.
		child_counter->wait_until(2);
		
		ensure("Foo thread's backtrace contains foo()",
			foo_thread.backtrace().find("foo") != string::npos);
		ensure("Foo thread's backtrace doesn't contain bar()",
			foo_thread.backtrace().find("bar") == string::npos);
		ensure("Bar thread's backtrace contains bar()",
			bar_thread.backtrace().find("bar") != string::npos);
		ensure("Bar thread's backtrace doesn't contain foo()",
			bar_thread.backtrace().find("foo") == string::npos);
		
		string all_backtraces(oxt::thread::all_backtraces());
		ensure(all_backtraces.find("foo") != string::npos);
		ensure(all_backtraces.find("bar") != string::npos);
		
		parent_counter->increment(); // Tell threads to quit.
		foo_thread.join();
		bar_thread.join();
	}
}