Skip to content

Commit

Permalink
Test scheduler handler calls in isolation
Browse files Browse the repository at this point in the history
Move scheduler handler registrations to main.c (for now)
  • Loading branch information
rhargreaves committed Mar 30, 2024
1 parent f95ae5e commit 311fca8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@
#include "scheduler.h"
#include "sys.h"
#include "ui.h"
#include "comm_megawifi.h"
#include "everdrive_led.h"
#include "comm_demo.h"
#include <vdp.h>
#include <dma.h>

static void registerSchedulerHandlers()
{
scheduler_addTickHandler(*comm_megawifi_tick);
scheduler_addTickHandler(*midi_receiver_read_if_comm_ready);
scheduler_addFrameHandler(*midi_psg_tick);
scheduler_addFrameHandler(*ui_update);
scheduler_addFrameHandler(*everdrive_led_tick);
scheduler_addFrameHandler(*comm_megawifi_vsync);
scheduler_addFrameHandler(*comm_demo_vsync);
}

int main()
{
DMA_init();
scheduler_init();
registerSchedulerHandlers();
log_init();
comm_init();
midi_init(M_BANK_0, P_BANK_0, ENVELOPES);
Expand Down
7 changes: 0 additions & 7 deletions src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ void scheduler_init(void)
ticks = 0;
tickHandlersLength = 0;
frameHandlersLength = 0;
scheduler_addTickHandler(*comm_megawifi_tick);
scheduler_addTickHandler(*midi_receiver_read_if_comm_ready);
scheduler_addFrameHandler(*midi_psg_tick);
scheduler_addFrameHandler(*ui_update);
scheduler_addFrameHandler(*everdrive_led_tick);
scheduler_addFrameHandler(*comm_megawifi_vsync);
scheduler_addFrameHandler(*comm_demo_vsync);
}

void scheduler_vsync(void)
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ int main(void)

scheduler_test(test_scheduler_nothing_called_on_vsync),
scheduler_test(test_scheduler_processes_frame_events_once_after_vsync),
scheduler_test(test_scheduler_tick_runs_midi_receiver),
scheduler_test(test_scheduler_registered_frame_handler_called_on_vsync),
scheduler_test(test_scheduler_registered_tick_handler_called),
scheduler_test(
test_scheduler_multiple_registered_frame_handlers_called_on_vsync),
scheduler_test(test_scheduler_multiple_registered_tick_handlers_called),

applemidi_test(
test_applemidi_parses_rtpmidi_packet_with_single_midi_event),
Expand Down
75 changes: 65 additions & 10 deletions tests/unit/test_scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
extern void __real_scheduler_init(void);
extern void __real_scheduler_tick(void);

static void dummy_frame_handler()
{
function_called();
}

static void dummy_frame_handler_2()
{
function_called();
}

static void dummy_tick_handler()
{
function_called();
}

static void dummy_tick_handler_2()
{
function_called();
}

static int test_scheduler_setup(UNUSED void** state)
{
__real_scheduler_init();
Expand All @@ -20,25 +40,60 @@ static void test_scheduler_nothing_called_on_vsync(UNUSED void** state)
static void test_scheduler_processes_frame_events_once_after_vsync(
UNUSED void** state)
{
expect_function_call(__wrap_comm_megawifi_tick);
expect_function_call(__wrap_midi_receiver_read_if_comm_ready);
scheduler_addFrameHandler(*dummy_frame_handler);
scheduler_addTickHandler(*dummy_tick_handler);

expect_function_call(dummy_tick_handler);
__real_scheduler_tick();

scheduler_vsync();

expect_function_call(dummy_tick_handler);
expect_function_call(dummy_frame_handler);
__real_scheduler_tick();
}

static void test_scheduler_registered_frame_handler_called_on_vsync(
UNUSED void** state)
{
scheduler_vsync();

scheduler_addFrameHandler(*dummy_frame_handler);

expect_function_call(dummy_frame_handler);
__real_scheduler_tick();
}

static void test_scheduler_multiple_registered_frame_handlers_called_on_vsync(
UNUSED void** state)
{
scheduler_vsync();

expect_function_call(__wrap_comm_megawifi_tick);
expect_function_call(__wrap_midi_receiver_read_if_comm_ready);
expect_function_call(__wrap_midi_psg_tick);
expect_function_call(__wrap_ui_update);
expect_function_call(__wrap_comm_demo_vsync);
scheduler_addFrameHandler(*dummy_frame_handler);
scheduler_addFrameHandler(*dummy_frame_handler_2);

expect_function_call(dummy_frame_handler);
expect_function_call(dummy_frame_handler_2);
__real_scheduler_tick();
}

static void test_scheduler_tick_runs_midi_receiver(UNUSED void** state)
static void test_scheduler_registered_tick_handler_called(UNUSED void** state)
{
expect_function_call(__wrap_comm_megawifi_tick);
expect_function_call(__wrap_midi_receiver_read_if_comm_ready);
scheduler_addTickHandler(*dummy_tick_handler);

expect_function_call(dummy_tick_handler);
__real_scheduler_tick();
}

static void test_scheduler_multiple_registered_tick_handlers_called(
UNUSED void** state)
{
scheduler_vsync();

scheduler_addFrameHandler(*dummy_tick_handler);
scheduler_addFrameHandler(*dummy_tick_handler_2);

expect_function_call(dummy_tick_handler);
expect_function_call(dummy_tick_handler_2);
__real_scheduler_tick();
}

0 comments on commit 311fca8

Please sign in to comment.