Skip to content

Commit 15e64e4

Browse files
Added return code to fixture functions so errors can be handled durin… (awslabs#585)
* Added return code to fixture functions so errors can be handled during setup and shutdown. * Address pr feedback, the fixture teardown always runs now. Co-authored-by: Michael Graeb <[email protected]>
1 parent 4a6ae22 commit 15e64e4

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

include/aws/testing/aws_test_harness.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ static int total_failures;
311311
} \
312312
} while (0)
313313

314-
typedef void(aws_test_before_fn)(struct aws_allocator *allocator, void *ctx);
314+
typedef int(aws_test_before_fn)(struct aws_allocator *allocator, void *ctx);
315315
typedef int(aws_test_run_fn)(struct aws_allocator *allocator, void *ctx);
316-
typedef void(aws_test_after_fn)(struct aws_allocator *allocator, void *ctx);
316+
typedef int(aws_test_after_fn)(struct aws_allocator *allocator, int setup_result, void *ctx);
317317

318318
struct aws_test_harness {
319319
aws_test_before_fn *on_before;
@@ -378,17 +378,21 @@ static inline int s_aws_run_test_case(struct aws_test_harness *harness) {
378378
aws_logger_init_standard(&err_logger, aws_default_allocator(), &options);
379379
aws_logger_set(&err_logger);
380380

381+
int test_res = AWS_OP_ERR;
382+
int setup_res = AWS_OP_SUCCESS;
381383
if (harness->on_before) {
382-
harness->on_before(allocator, harness->ctx);
384+
setup_res = harness->on_before(allocator, harness->ctx);
383385
}
384386

385-
int ret_val = harness->run(allocator, harness->ctx);
387+
if (!setup_res) {
388+
test_res = harness->run(allocator, harness->ctx);
389+
}
386390

387391
if (harness->on_after) {
388-
harness->on_after(allocator, harness->ctx);
392+
test_res |= harness->on_after(allocator, setup_res, harness->ctx);
389393
}
390394

391-
if (!ret_val) {
395+
if (!test_res) {
392396
if (!harness->suppress_memcheck) {
393397
const size_t leaked_bytes = aws_mem_tracer_count(allocator);
394398
if (leaked_bytes) {
@@ -405,7 +409,7 @@ static inline int s_aws_run_test_case(struct aws_test_harness *harness) {
405409
aws_logger_set(NULL);
406410
aws_logger_clean_up(&err_logger);
407411

408-
if (!ret_val) {
412+
if (!test_res) {
409413
RETURN_SUCCESS("%s [ \033[32mOK\033[0m ]", harness->test_name);
410414
} else {
411415
FAIL("%s [ \033[31mFAILED\033[0m ]", harness->test_name);
@@ -464,9 +468,9 @@ static inline int enable_vt_mode(void) {
464468
}
465469

466470
#define AWS_TEST_CASE_FIXTURE_SUPPRESSION(name, b, fn, af, c, s) \
467-
static void b(struct aws_allocator *allocator, void *ctx); \
471+
static int b(struct aws_allocator *allocator, void *ctx); \
468472
static int fn(struct aws_allocator *allocator, void *ctx); \
469-
static void af(struct aws_allocator *allocator, void *ctx); \
473+
static int af(struct aws_allocator *allocator, int setup_result, void *ctx); \
470474
static struct aws_test_harness name##_test = { \
471475
b, \
472476
fn, \

tests/error_test.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,28 @@ static struct aws_error_info_list s_errors_list = {
2828
.count = AWS_ARRAY_SIZE(s_errors),
2929
};
3030

31-
static void s_setup_errors_test_fn(struct aws_allocator *allocator, void *ctx) {
31+
static int s_setup_errors_test_fn(struct aws_allocator *allocator, void *ctx) {
3232
(void)allocator;
3333
(void)ctx;
3434

3535
aws_reset_error();
3636
aws_set_global_error_handler_fn(NULL, NULL);
3737
aws_set_thread_local_error_handler_fn(NULL, NULL);
3838
aws_register_error_info(&s_errors_list);
39+
40+
return AWS_OP_SUCCESS;
3941
}
4042

41-
static void s_teardown_errors_test_fn(struct aws_allocator *allocator, void *ctx) {
43+
static int s_teardown_errors_test_fn(struct aws_allocator *allocator, int setup_res, void *ctx) {
4244
(void)allocator;
45+
(void)setup_res;
4346
(void)ctx;
4447

4548
aws_reset_error();
4649
aws_set_global_error_handler_fn(NULL, NULL);
4750
aws_set_thread_local_error_handler_fn(NULL, NULL);
51+
52+
return AWS_OP_SUCCESS;
4853
}
4954

5055
static int s_raise_errors_test_fn(struct aws_allocator *allocator, void *ctx) {

0 commit comments

Comments
 (0)