/* * Test4c * Verifies mailbox restart */ static void Test4c( void ) { char *msg = "mailbox start test"; int rc, count; TEST_INTRO( "Start Mailbox Test" ); // make sure we're empty to start drain_queue( SILENT ); verify_mailbox_count( 0, MBX_NO_STOP ); // Start the mailbox printf("Starting the mailbox...\n"); rc = ManageMailbox( MBX_NO_STOP, &count ); if (rc != 0) { printf("ManageMailbox() failed to start the mailbox! Reason=%s\n",mailbox_code(rc)); FAILURE_EXIT(); } // check that it's working send_recv_verify( msg, NOISY ); TEST_CLOSING(); }
/* * Test4a * Verifies the return count value of the ManageMailbox call. */ static void Test4a( void ) { char *msg = "test4a mailbox count"; TEST_INTRO( "Mailbox Count Testing" ); // make sure we're empty to start drain_queue( SILENT ); verify_mailbox_count( 0, MBX_NO_STOP ); send_msg( "msg one", MBX_NON_BLOCKING, NOISY, ON_FAILURE_EXIT ); verify_mailbox_count( 1, MBX_NO_STOP ); send_msg( "msg two", MBX_NON_BLOCKING, NOISY, ON_FAILURE_EXIT ); verify_mailbox_count( 2, MBX_NO_STOP ); send_msg( "msg three", MBX_NON_BLOCKING, NOISY, ON_FAILURE_EXIT ); verify_mailbox_count( 3, MBX_NO_STOP ); send_msg( "msg four", MBX_NON_BLOCKING, NOISY, ON_FAILURE_EXIT ); verify_mailbox_count( 4, MBX_NO_STOP ); send_msg( "msg five", MBX_NON_BLOCKING, NOISY, ON_FAILURE_EXIT ); verify_mailbox_count( 5, MBX_NO_STOP ); // fill the mailbox fill_mailbox( msg ); verify_mailbox_count( MAX_QUEUE_SIZE, MBX_NO_STOP ); // read a few messages and verify again and stop the mailbox burst_recv( 5, MBX_NON_BLOCKING, NOISY, ON_FAILURE_EXIT ); verify_mailbox_count( MAX_QUEUE_SIZE-5, MBX_NO_STOP ); TEST_CLOSING(); }
/* * Test4b * Verifies MAILBOX_STOPPED is reported correctly. */ static void Test4b( void ) { char recv_buf[MAX_QUEUE_SIZE]; char *msg = "test4b testing mailbox stop"; int rc; TEST_INTRO( "Stopped Mailbox Test" ); // make sure we're empty to start drain_queue( SILENT ); // fill the mailbox fill_mailbox( msg ); // verify and stop verify_mailbox_count( MAX_QUEUE_SIZE, MBX_STOP ); // NON-BLOCKING SEND should fail while MAILBOX_STOPPED printf("NON-BLOCKING SEND to stopped mailbox -- expecting failure ...\n"); rc = send_msg( msg, MBX_NON_BLOCKING, NOISY, ON_SUCCESS_EXIT ); if (rc != MAILBOX_STOPPED) { printf("Mailbox NON-BLOCKING SEND did not report %s as expected. Response was %s\n", mailbox_code(MAILBOX_STOPPED),mailbox_code(rc)); FAILURE_EXIT(); } // BLOCKING SEND should fail while MAILBOX_STOPPED printf("BLOCKING SEND to stopped mailbox -- expecting failure ...\n"); rc = send_msg( msg, MBX_BLOCK, NOISY, ON_SUCCESS_EXIT ); if (rc != MAILBOX_STOPPED) { printf("Mailbox BLOCKING SEND did not report %s as expected. Response was %s\n", mailbox_code(MAILBOX_STOPPED),mailbox_code(rc)); FAILURE_EXIT(); } // BLOCKING READ should succeed while MAILBOX_STOPPED if mailbox is not empty printf("BLOCKING READ to stopped mailbox with messages -- expecting success ...\n"); rc = recv_msg( recv_buf, MBX_BLOCK, NOISY, ON_FAILURE_EXIT ); if (rc != 0) { printf("Mailbox BLOCKING READ did not report %s as expected. Response was %s\n", mailbox_code(0),mailbox_code(rc)); FAILURE_EXIT(); } // should be able to read out the messages queued in the mailbox even though stopped printf("NON-BLOCKING READs to stopped mailbox with messages -- expecting success ...\n"); burst_recv( MAX_QUEUE_SIZE-1, MBX_NON_BLOCKING, NOISY, ON_FAILURE_EXIT ); // extra NON-BLOCKING READ should fail with MAILBOX_STOPPED printf("Extra NON-BLOCKING READ to stopped/empty mailbox -- expecting failure ...\n"); rc = recv_msg( recv_buf, MBX_NON_BLOCKING, NOISY, ON_SUCCESS_EXIT ); if (rc != MAILBOX_STOPPED) { printf("Mailbox NON-BLOCKING READ did not report %s as expected. Response was %s\n", mailbox_code(MAILBOX_STOPPED),mailbox_code(rc)); FAILURE_EXIT(); } // BLOCKING READ should fail with MAILBOX_STOPPED printf("Extra BLOCKING READ to stopped/empty mailbox -- expecting failure ...\n"); rc = recv_msg( recv_buf, MBX_BLOCK, NOISY, ON_SUCCESS_EXIT ); if (rc != MAILBOX_STOPPED) { printf("Mailbox BLOCKING READ did not report %s as expected. Response was %s\n", mailbox_code(MAILBOX_STOPPED),mailbox_code(rc)); FAILURE_EXIT(); } TEST_CLOSING(); }
int main(int argc, const char * argv[]) { try { if(argc < 2) { printf("Please provide a .tora file to parse!\n"); exit(1); } if(!tora_init()) { TORA_RUNTIME_EXCEPTION("Failed to initialise parser!"); } // Create an input stream to begin parsing our document TORAInputStream *input_stream = input_stream_from_file_contents(argv[1]); if(!input_stream) { TORA_RUNTIME_EXCEPTION("Failed to parse input stream!"); } // Create a token stream from our TORAInputStream TORATokenStream *token_stream = token_stream_from_input(input_stream); if(!token_stream) { free_input_stream(input_stream); TORA_RUNTIME_EXCEPTION("Failed to malloc input stream!"); } // Parse the top level of our program and generate an AST TORAParserProgExpression *prog = parse_top_level(token_stream); if(!prog) { free_token_stream(token_stream); free_input_stream(input_stream); TORA_RUNTIME_EXCEPTION("Failed to generate expression tree!"); } // Create a base environment for use when evaluating the AST TORAEnvironment *environment = create_environment(NULL); if(!environment) { free_expression(prog); free_token_stream(token_stream); free_input_stream(input_stream); TORA_RUNTIME_EXCEPTION("Failed to malloc root environment!"); } // Evaluate the program! bool return_encountered = false; evaluate(prog, environment, &return_encountered); // Cleanup free_expression(prog); drain_queue(environment_queue); drain_queue(interpretter_queue); free_token_stream(token_stream); free_input_stream(input_stream); if(num_malloc != num_free) { printf("Num malloc'd blocks: %i, num freed: %i, lost: %i\n", num_malloc, num_free, num_malloc-num_free); printf("Num un-freed tokens: %i\n", num_tok); } // Teardown tora_shutdown(); } catch(ParserException) { printf("Parser Exception: \"%s\"\n\tfile: %s\n\tline: %d\n", e4c.err.message, e4c.err.file, e4c.err.line); } catch(InterpretterException) { printf("Interpretter Exception: \"%s\"\n\tfile: %s\n\tline: %d\n", e4c.err.message, e4c.err.file, e4c.err.line); } catch(RuntimeException) { printf("Runtime Exception: \"%s\"\n\tfile: %s\n\tline: %d\n", e4c.err.message, e4c.err.file, e4c.err.line); } return 0; }