Skip to content

Commit aab1070

Browse files
committed
Use SingletonPtr for the internal variables defaults and handlers in utest_harness.
1 parent caa4c4f commit aab1070

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

features/frameworks/utest/source/utest_harness.cpp

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,13 @@ namespace
4747
size_t case_failed = 0;
4848
size_t case_failed_before = 0;
4949

50-
handlers_t& defaults() {
51-
static handlers_t _handlers = default_handlers;
52-
return _handlers;
53-
}
50+
struct DefaultHandlers : public handlers_t {
51+
DefaultHandlers() : handlers_t(default_handlers) { }
52+
DefaultHandlers(const handlers_t& other) : handlers_t(other) { }
53+
};
5454

55-
handlers_t& handlers() {
56-
static handlers_t _handlers = default_handlers;
57-
return _handlers;
58-
}
55+
SingletonPtr<DefaultHandlers> defaults;
56+
SingletonPtr<DefaultHandlers> handlers;
5957

6058
location_t location = LOCATION_UNKNOWN;
6159

@@ -117,10 +115,10 @@ bool Harness::run(const Specification& specification)
117115
return false;
118116
test_cases = specification.cases;
119117
test_length = specification.length;
120-
defaults() = specification.defaults;
121-
handlers().test_setup = defaults().get_handler(specification.setup_handler);
122-
handlers().test_teardown = defaults().get_handler(specification.teardown_handler);
123-
handlers().test_failure = defaults().get_handler(specification.failure_handler);
118+
*defaults.get() = specification.defaults;
119+
handlers->test_setup = defaults->get_handler(specification.setup_handler);
120+
handlers->test_teardown = defaults->get_handler(specification.teardown_handler);
121+
handlers->test_failure = defaults->get_handler(specification.failure_handler);
124122

125123
test_index_of_case = 0;
126124
test_passed = 0;
@@ -134,16 +132,16 @@ bool Harness::run(const Specification& specification)
134132
int setup_status = 0;
135133
failure_t failure(REASON_NONE, location);
136134

137-
if (handlers().test_setup) {
138-
setup_status = handlers().test_setup(test_length);
135+
if (handlers->test_setup) {
136+
setup_status = handlers->test_setup(test_length);
139137
if (setup_status == STATUS_CONTINUE) setup_status = 0;
140138
else if (setup_status < STATUS_CONTINUE) failure.reason = REASON_TEST_SETUP;
141139
else if (setup_status > signed(test_length)) failure.reason = REASON_CASE_INDEX;
142140
}
143141

144142
if (failure.reason != REASON_NONE) {
145-
if (handlers().test_failure) handlers().test_failure(failure);
146-
if (handlers().test_teardown) handlers().test_teardown(0, 0, failure);
143+
if (handlers->test_failure) handlers->test_failure(failure);
144+
if (handlers->test_teardown) handlers->test_teardown(0, 0, failure);
147145
test_cases = NULL;
148146
exit(1);
149147
return true;
@@ -157,8 +155,8 @@ bool Harness::run(const Specification& specification)
157155
scheduler.post(run_next_case, 0);
158156
if (scheduler.run() != 0) {
159157
failure.reason = REASON_SCHEDULER;
160-
if (handlers().test_failure) handlers().test_failure(failure);
161-
if (handlers().test_teardown) handlers().test_teardown(0, 0, failure);
158+
if (handlers->test_failure) handlers->test_failure(failure);
159+
if (handlers->test_teardown) handlers->test_teardown(0, 0, failure);
162160
test_cases = NULL;
163161
exit(1);
164162
return true;
@@ -174,8 +172,8 @@ void Harness::raise_failure(const failure_reason_t reason)
174172
if (test_cases == NULL) return;
175173

176174
utest::v1::status_t fail_status = STATUS_ABORT;
177-
if (handlers().test_failure) handlers().test_failure(failure_t(reason, location));
178-
if (handlers().case_failure) fail_status = handlers().case_failure(case_current, failure_t(reason, location));
175+
if (handlers->test_failure) handlers->test_failure(failure_t(reason, location));
176+
if (handlers->case_failure) fail_status = handlers->case_failure(case_current, failure_t(reason, location));
179177

180178
{
181179
UTEST_ENTER_CRITICAL_SECTION;
@@ -191,25 +189,25 @@ void Harness::raise_failure(const failure_reason_t reason)
191189
}
192190

193191
if (fail_status == STATUS_ABORT || reason & REASON_CASE_SETUP) {
194-
if (handlers().case_teardown && location != LOCATION_CASE_TEARDOWN) {
192+
if (handlers->case_teardown && location != LOCATION_CASE_TEARDOWN) {
195193
location_t fail_loc(location);
196194
location = LOCATION_CASE_TEARDOWN;
197195

198-
utest::v1::status_t teardown_status = handlers().case_teardown(case_current, case_passed, case_failed, failure_t(reason, fail_loc));
196+
utest::v1::status_t teardown_status = handlers->case_teardown(case_current, case_passed, case_failed, failure_t(reason, fail_loc));
199197
if (teardown_status < STATUS_CONTINUE) raise_failure(REASON_CASE_TEARDOWN);
200198
else if (teardown_status > signed(test_length)) raise_failure(REASON_CASE_INDEX);
201199
else if (teardown_status >= 0) case_index = teardown_status - 1;
202200

203201
// Restore case failure location once we have dealt with case teardown
204202
location = fail_loc;
205-
handlers().case_teardown = NULL;
203+
handlers->case_teardown = NULL;
206204
}
207205
}
208206
if (fail_status == STATUS_ABORT) {
209207
test_failed++;
210208
failure_t fail(reason, location);
211209
location = LOCATION_TEST_TEARDOWN;
212-
if (handlers().test_teardown) handlers().test_teardown(test_passed, test_failed, fail);
210+
if (handlers->test_teardown) handlers->test_teardown(test_passed, test_failed, fail);
213211
exit(test_failed);
214212
die();
215213
}
@@ -225,8 +223,8 @@ void Harness::schedule_next_case()
225223
if (case_control.repeat & REPEAT_SETUP_TEARDOWN || !(case_control.repeat & (REPEAT_ON_TIMEOUT | REPEAT_ON_VALIDATE))) {
226224
location = LOCATION_CASE_TEARDOWN;
227225

228-
if (handlers().case_teardown) {
229-
utest::v1::status_t status = handlers().case_teardown(case_current, case_passed, case_failed,
226+
if (handlers->case_teardown) {
227+
utest::v1::status_t status = handlers->case_teardown(case_current, case_passed, case_failed,
230228
case_failed ? failure_t(REASON_CASES, LOCATION_UNKNOWN) : failure_t(REASON_NONE));
231229
if (status < STATUS_CONTINUE) raise_failure(REASON_CASE_TEARDOWN);
232230
else if (status > signed(test_length)) raise_failure(REASON_CASE_INDEX);
@@ -305,9 +303,9 @@ void Harness::run_next_case()
305303
UTEST_LOG_FUNCTION();
306304
if(case_current < (test_cases + test_length))
307305
{
308-
handlers().case_setup = defaults().get_handler(case_current->setup_handler);
309-
handlers().case_teardown = defaults().get_handler(case_current->teardown_handler);
310-
handlers().case_failure = defaults().get_handler(case_current->failure_handler);
306+
handlers->case_setup = defaults->get_handler(case_current->setup_handler);
307+
handlers->case_teardown = defaults->get_handler(case_current->teardown_handler);
308+
handlers->case_failure = defaults->get_handler(case_current->failure_handler);
311309

312310
if (case_current->is_empty()) {
313311
location = LOCATION_UNKNOWN;
@@ -328,7 +326,7 @@ void Harness::run_next_case()
328326

329327
if (setup_repeat & REPEAT_SETUP_TEARDOWN) {
330328
location = LOCATION_CASE_SETUP;
331-
if (handlers().case_setup && (handlers().case_setup(case_current, test_index_of_case) != STATUS_CONTINUE)) {
329+
if (handlers->case_setup && (handlers->case_setup(case_current, test_index_of_case) != STATUS_CONTINUE)) {
332330
raise_failure(REASON_CASE_SETUP);
333331
schedule_next_case();
334332
return;
@@ -368,9 +366,9 @@ void Harness::run_next_case()
368366
UTEST_LEAVE_CRITICAL_SECTION;
369367
}
370368
}
371-
else if (handlers().test_teardown) {
369+
else if (handlers->test_teardown) {
372370
location = LOCATION_TEST_TEARDOWN;
373-
handlers().test_teardown(test_passed, test_failed, test_failed ? failure_t(REASON_CASES, LOCATION_UNKNOWN) : failure_t(REASON_NONE));
371+
handlers->test_teardown(test_passed, test_failed, test_failed ? failure_t(REASON_CASES, LOCATION_UNKNOWN) : failure_t(REASON_NONE));
374372
test_cases = NULL;
375373
exit(test_failed);
376374
} else {

0 commit comments

Comments
 (0)