Skip to content

Commit b79952d

Browse files
committed
tests: Fix flaky hotplug/async_fuzz cascade on macos-14
Shared mock channel buffers were never flushed between test suites, so stale bytes left in flight at teardown caused the next suite's fresh PD to read a non-zero sequence on the first packet, forcing the CP offline for 300s. This patch calls flush cp_to_pd_buf and pd_to_cp_buf at setup. Also fix the hotplug setup-failure paths that leaked async runners into async_fuzz, and replace two hardcoded 2s command waits in async_fuzz with condition polls. Signed-off-by: Siddharth Chandrasekaran <sidcha.dev@gmail.com>
1 parent 862b8bc commit b79952d

3 files changed

Lines changed: 34 additions & 23 deletions

File tree

tests/unit-tests/test-async-fuzz.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ static int async_command_callback(void *data, struct osdp_cmd *cmd)
5151
/* Declare external variables from test.c */
5252
extern work_t *g_test_works[];
5353

54+
static bool wait_for_cmd_received(struct async_test_data *data, int timeout_sec)
55+
{
56+
for (int i = 0; i < timeout_sec * 10; i++) {
57+
if (data->cmd_received)
58+
return true;
59+
usleep(100 * 1000);
60+
}
61+
return false;
62+
}
63+
5464
/* Helper function to wait for all work slots to be freed */
5565
static void wait_for_all_work_cleanup(void)
5666
{
@@ -179,11 +189,7 @@ static bool test_async_startup_order(enum async_order order)
179189
goto cleanup;
180190
}
181191

182-
/* Wait for command processing */
183-
usleep(2000 * 1000); /* 2 seconds */
184-
185-
/* Check result */
186-
if (data.cmd_received) {
192+
if (wait_for_cmd_received(&data, 5)) {
187193
printf(SUB_2 "Order %d: SUCCESS\n", order);
188194
result = true;
189195
} else {
@@ -288,11 +294,7 @@ static bool test_async_recovery(void)
288294
goto cleanup;
289295
}
290296

291-
/* Wait for command processing */
292-
usleep(2000 * 1000); /* 2 seconds */
293-
294-
/* Check result */
295-
if (data.cmd_received) {
297+
if (wait_for_cmd_received(&data, 5)) {
296298
printf(SUB_2 "Recovery: SUCCESS\n");
297299
result = true;
298300
} else {

tests/unit-tests/test-hotplug.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct test_hotplug_ctx {
3131

3232
static struct test_hotplug_ctx g_test_ctx = {0};
3333

34+
static bool wait_for_pd_online(int timeout_sec);
35+
3436
int test_hotplug_event_callback(void *arg, int pd, struct osdp_event *ev)
3537
{
3638
ARG_UNUSED(pd);
@@ -69,22 +71,24 @@ static int setup_test_environment(struct test *t)
6971

7072
if (g_test_ctx.cp_runner < 0 || g_test_ctx.pd_runner < 0) {
7173
printf(SUB_1 "Failed to created CP/PD runners\n");
74+
if (g_test_ctx.cp_runner >= 0)
75+
async_runner_stop(g_test_ctx.cp_runner);
76+
if (g_test_ctx.pd_runner >= 0)
77+
async_runner_stop(g_test_ctx.pd_runner);
78+
osdp_cp_teardown(g_test_ctx.cp_ctx);
79+
osdp_pd_teardown(g_test_ctx.pd_ctx);
80+
memset(&g_test_ctx, 0, sizeof(g_test_ctx));
7281
return -1;
7382
}
7483

75-
/* Wait for devices to come online */
76-
int rc = 0;
77-
uint8_t status = 0;
78-
while (1) {
79-
if (rc > 10) {
80-
printf(SUB_1 "PD failed to come online\n");
81-
return -1;
82-
}
83-
osdp_get_status_mask(g_test_ctx.cp_ctx, &status);
84-
if (status & 1)
85-
break;
86-
usleep(1000 * 1000);
87-
rc++;
84+
if (!wait_for_pd_online(10)) {
85+
printf(SUB_1 "PD failed to come online\n");
86+
async_runner_stop(g_test_ctx.cp_runner);
87+
async_runner_stop(g_test_ctx.pd_runner);
88+
osdp_cp_teardown(g_test_ctx.cp_ctx);
89+
osdp_pd_teardown(g_test_ctx.pd_ctx);
90+
memset(&g_test_ctx, 0, sizeof(g_test_ctx));
91+
return -1;
8892
}
8993

9094
return 0;

tests/unit-tests/test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,11 @@ int test_setup_devices_ext(struct test *t, osdp_t **cp, osdp_t **pd,
617617
ARG_UNUSED(t);
618618
#endif /* OPT_OSDP_LOG_MINIMAL */
619619

620+
/* Shared mock channel; drop stale bytes left by the previous suite so a
621+
* fresh PD does not read a non-zero sequence on its first packet. */
622+
CIRCBUF_FLUSH(cp_to_pd_buf);
623+
CIRCBUF_FLUSH(pd_to_cp_buf);
624+
620625
uint8_t scbk[16] = {
621626
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
622627
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f

0 commit comments

Comments
 (0)