| 1 | #include <cgreen/cgreen.h> |
| 2 | #include <cgreen/messaging.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #include "../src/utils.h" |
| 7 | |
| 8 | #include <signal.h> |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | using namespace cgreen; |
| 12 | #endif |
| 13 | |
| 14 | Ensure(highly_nested_test_suite_should_still_complete) { |
| 15 | assert_that(true); |
| 16 | } |
| 17 | |
| 18 | TestSuite *highly_nested_test_suite(void) { |
| 19 | int i; |
| 20 | TestSuite *suite = create_test_suite(); |
| 21 | add_test(suite, highly_nested_test_suite_should_still_complete); |
| 22 | for (i = 0; i < 1000; i++) { |
| 23 | TestSuite *nesting = create_test_suite(); |
| 24 | add_suite(nesting, suite); |
| 25 | suite = nesting; |
| 26 | } |
| 27 | return suite; |
| 28 | } |
| 29 | |
| 30 | Ensure(can_send_message) { |
| 31 | int messaging = start_cgreen_messaging(33); |
| 32 | send_cgreen_message(messaging, 99); |
| 33 | assert_that(receive_cgreen_message(messaging), is_equal_to(99)); |
| 34 | } |
| 35 | |
| 36 | static int signal_received = 0; |
| 37 | static void catch_signal(int s) { |
| 38 | (void)s; |
| 39 | signal_received = 1; |
| 40 | signal(SIGPIPE, SIG_DFL); |
| 41 | } |
| 42 | |
| 43 | Ensure(failure_reported_and_exception_thrown_when_messaging_would_block) { |
| 44 | const int LOOPS = 65537; |
| 45 | int messaging = start_cgreen_messaging(33); |
| 46 | int loop; |
| 47 | char panic_message[1000]; |
| 48 | |
| 49 | signal_received = 0; |
| 50 | signal(SIGPIPE, catch_signal); |
| 51 | |
| 52 | panic_set_output_buffer(panic_message); |
| 53 | |
| 54 | for (loop = 0; loop < LOOPS; loop++) { |
| 55 | send_cgreen_message(messaging, 99); |
| 56 | if (signal_received == 1) |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | assert_that(signal_received, is_equal_to(1)); |
| 61 | assert_that(loop, is_less_than(LOOPS)); |
| 62 | assert_that(panic_message, contains_string("Too many assertions")); |
| 63 | } |
| 64 | |
| 65 | TestSuite *messaging_tests(void) { |
| 66 | TestSuite *suite = create_test_suite(); |
| 67 | add_suite(suite, highly_nested_test_suite()); |
| 68 | add_test(suite, can_send_message); |
| 69 | #ifndef WIN32 // TODO: win32 needs non-blocking pipes like posix for this to pass |
| 70 | add_test(suite, failure_reported_and_exception_thrown_when_messaging_would_block); |
| 71 | #endif |
| 72 | return suite; |
| 73 | } |