Line data Source code
1 : /*
2 : * test.cxx
3 : * Test
4 : *
5 : * Created by Matthias Wiesmann on 24.01.05.
6 : * Copyright 2005 CERN. All rights reserved.
7 : *
8 : */
9 :
10 : #include <ers/OutputStream.hpp>
11 : #include <ers/SampleIssues.hpp>
12 : #include <ers/StreamManager.hpp>
13 :
14 : #include <boost/lexical_cast.hpp>
15 : #include <csignal>
16 : #include <ers/ers.hpp>
17 : #include <stdexcept>
18 : #include <thread>
19 :
20 0 : ERS_DECLARE_ISSUE(ers, ExceptionTest, "Exception test", ERS_EMPTY)
21 : struct Test
22 : {
23 0 : void pass(int step)
24 : {
25 0 : ERS_RANGE_CHECK(0, step, 8);
26 :
27 0 : switch (step) {
28 0 : case 1: {
29 0 : ers::PermissionDenied issue(ERS_HERE, "foo2", 0x777);
30 0 : throw issue;
31 0 : }
32 0 : case 2: {
33 0 : ers::FileDoesNotExist issue(ERS_HERE, "foo3");
34 0 : throw issue;
35 0 : }
36 0 : case 3: {
37 0 : ers::FileDoesNotExist issue(ERS_HERE, "foo3");
38 0 : issue.add_qualifier("ers_test");
39 0 : throw issue;
40 0 : }
41 0 : case 4: {
42 0 : throw ers::CantOpenFile(ERS_HERE, "foo3");
43 : }
44 0 : case 5: {
45 0 : throw std::runtime_error("std::out_of_range error");
46 : }
47 : case 6: {
48 : ERS_ASSERT_MSG(step <= 6, "ERS_ASSERT_MSG is broken");
49 : } break;
50 : case 7: {
51 : ERS_ASSERT_MSG(step > 6, "ERS_ASSERT_MSG is broken");
52 : } break;
53 0 : default: {
54 0 : ers::info(ers::ExceptionTest(ERS_HERE));
55 0 : struct UnhandledException
56 : {};
57 0 : throw UnhandledException();
58 : }
59 : }
60 0 : }
61 : };
62 :
63 : void
64 0 : test_function(int /* index */)
65 : {
66 0 : usleep(10000);
67 0 : ers::error(ers::FileDoesNotExist(ERS_HERE, "error file"));
68 0 : usleep(10000);
69 0 : ers::fatal(ers::FileDoesNotExist(ERS_HERE, "fatal file"));
70 0 : usleep(10000);
71 0 : ers::warning(ers::FileDoesNotExist(ERS_HERE, "warning file"));
72 0 : }
73 :
74 : struct IssueCatcher
75 : {
76 0 : void handler(const ers::Issue& issue)
77 : {
78 0 : std::cout << "IssueCatcher has been called for the following issue:" << std::endl;
79 0 : ers::error(issue);
80 0 : }
81 : };
82 :
83 : void
84 0 : test_local_catcher()
85 : {
86 0 : IssueCatcher catcher;
87 0 : std::unique_ptr<ers::IssueCatcherHandler> handler;
88 0 : try {
89 0 : handler.reset(ers::set_issue_catcher(std::bind(&IssueCatcher::handler, &catcher, std::placeholders::_1)));
90 0 : } catch (ers::IssueCatcherAlreadySet& ex) {
91 0 : ers::fatal(ex);
92 0 : return;
93 0 : }
94 :
95 0 : ers::CantOpenFile issue(ERS_HERE, "TEST");
96 0 : ers::warning(issue);
97 0 : ers::error(issue);
98 0 : ers::fatal(issue);
99 0 : }
100 :
101 : int
102 0 : main(int ac, char** av)
103 : {
104 0 : test_function(0);
105 0 : test_function(0);
106 :
107 0 : test_local_catcher();
108 :
109 0 : IssueCatcher catcher;
110 0 : std::unique_ptr<ers::IssueCatcherHandler> handler;
111 0 : try {
112 0 : handler.reset(ers::set_issue_catcher(std::bind(&IssueCatcher::handler, &catcher, std::placeholders::_1)));
113 0 : } catch (ers::IssueCatcherAlreadySet& ex) {
114 0 : ers::fatal(ex);
115 0 : return 1;
116 0 : }
117 :
118 0 : test_local_catcher();
119 :
120 0 : std::thread thr1(std::bind(test_function, 1));
121 0 : std::thread thr2(std::bind(test_function, 2));
122 0 : std::thread thr3(std::bind(test_function, 3));
123 0 : std::thread thr4(std::bind(test_function, 4));
124 0 : thr1.join();
125 0 : thr2.join();
126 0 : thr3.join();
127 0 : thr4.join();
128 :
129 0 : test_function(0);
130 0 : test_function(0);
131 :
132 0 : int steps = ac > 1 ? boost::lexical_cast<int>(av[1]) : 9;
133 0 : Test test;
134 0 : for (int step = 1; step < steps; ++step) {
135 0 : try {
136 0 : test.pass(step);
137 0 : usleep(100000);
138 0 : } catch (ers::PermissionDenied& ex) {
139 0 : ers::CantOpenFile issue(ERS_HERE, ex.get_file_name(), ex);
140 0 : issue.add_qualifier("q1");
141 0 : ers::warning(issue);
142 0 : } catch (ers::FileDoesNotExist& ex) {
143 0 : ers::CantOpenFile issue(ERS_HERE, ex.get_file_name(), ex);
144 0 : issue.add_qualifier("q2");
145 0 : ers::warning(issue);
146 0 : } catch (ers::Issue& ex) {
147 0 : ers::error(ex);
148 0 : } catch (std::exception& ex) {
149 0 : ers::CantOpenFile issue(ERS_HERE, "unknown", ex);
150 0 : issue.add_qualifier("q3");
151 0 : ers::warning(issue);
152 0 : handler.reset();
153 0 : }
154 : }
155 0 : return 0;
156 0 : }
|