Skip to content

Commit 85482e3

Browse files
committed
test(frontend): address recovery review feedback
Signed-off-by: Pxx500 <pbartulik@gmail.com>
1 parent b9da6dc commit 85482e3

3 files changed

Lines changed: 75 additions & 25 deletions

File tree

‎src/daemon/frontend.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static atomic_bool frontend_test_monitor_is_waiting = ATOMIC_VAR_INIT(false);
119119
static atomic_uint_fast64_t frontend_test_monitor_observation_count = ATOMIC_VAR_INIT(0);
120120
static atomic_uint_fast64_t frontend_test_worker_observation_count = ATOMIC_VAR_INIT(0);
121121
static atomic_uint_fast64_t frontend_test_worker_idle_count = ATOMIC_VAR_INIT(0);
122+
static atomic_uint_fast64_t frontend_test_routed_cancellation_count = ATOMIC_VAR_INIT(0);
122123

123124
void cbm_daemon_frontend_test_observer_reset(bool hold_monitor) {
124125
atomic_store_explicit(&frontend_test_monitor_observation_count, 0, memory_order_release);
@@ -147,6 +148,10 @@ uint64_t cbm_daemon_frontend_test_worker_observations(void) {
147148
uint64_t cbm_daemon_frontend_test_worker_idle_cycles(void) {
148149
return atomic_load_explicit(&frontend_test_worker_idle_count, memory_order_acquire);
149150
}
151+
152+
uint64_t cbm_daemon_frontend_test_routed_cancellations(void) {
153+
return atomic_load_explicit(&frontend_test_routed_cancellation_count, memory_order_acquire);
154+
}
150155
#endif
151156

152157
static cbm_version_cohort_maintenance_presence_t frontend_observe_maintenance(
@@ -478,6 +483,12 @@ static frontend_cancellation_route_t frontend_route_cancellation(frontend_state_
478483
}
479484
cbm_mutex_unlock(&state->mutex);
480485
cbm_jsonrpc_request_free(&request);
486+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
487+
if (route == FRONTEND_CANCELLATION_ACTIVE || route == FRONTEND_CANCELLATION_QUEUED) {
488+
atomic_fetch_add_explicit(&frontend_test_routed_cancellation_count, 1,
489+
memory_order_release);
490+
}
491+
#endif
481492
return route;
482493
}
483494

@@ -531,6 +542,11 @@ static bool frontend_recover_client(frontend_state_t *state, frontend_item_t *it
531542
}
532543

533544
cbm_daemon_bootstrap_result_t bootstrap = {0};
545+
/* Bootstrap can report launch failures on stderr from this worker.
546+
* Keep that diagnostic
547+
* to explain failed recovery. It never writes MCP
548+
* stdout, but blocked stderr can delay
549+
* exit. Do not add more logging here. */
534550
cbm_daemon_bootstrap_status_t bootstrap_status =
535551
cbm_daemon_bootstrap_execute(&state->session->bootstrap, &bootstrap);
536552
cbm_daemon_runtime_client_t *replacement =

‎src/daemon/frontend.h‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
typedef struct cbm_daemon_maintenance_monitor cbm_daemon_maintenance_monitor_t;
1616

17-
/* Everything the stdio frontend must replay when its authenticated daemon
18-
* connection is replaced. Pointer fields are borrowed for the complete
19-
* frontend call. */
17+
/* Connection settings replayed when the authenticated daemon is replaced.
18+
* Background activation
19+
* from initialize is not restored here.
20+
* Pointer fields are borrowed for the complete frontend
21+
* call. */
2022
typedef struct {
2123
cbm_daemon_bootstrap_config_t bootstrap;
2224
const char *session_root;
@@ -67,6 +69,10 @@ bool cbm_daemon_frontend_test_monitor_waiting(void);
6769
uint64_t cbm_daemon_frontend_test_monitor_observations(void);
6870
uint64_t cbm_daemon_frontend_test_worker_observations(void);
6971
uint64_t cbm_daemon_frontend_test_worker_idle_cycles(void);
72+
/* Counts matching cancellations marked under the frontend mutex.
73+
* Tests snapshot this monotonic
74+
* count before sending a notification. */
75+
uint64_t cbm_daemon_frontend_test_routed_cancellations(void);
7076
#endif
7177

7278
/* Takes ownership of client and borrows cohort_manager for the complete call.

‎tests/test_daemon_frontend.c‎

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ typedef struct {
3939
typedef struct {
4040
frontend_maintenance_fixture_t maintenance;
4141
char conflict_log[FRONTEND_TEST_PATH_CAP];
42+
char build_fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE];
43+
cbm_daemon_build_identity_t identity;
4244
cbm_daemon_runtime_service_t *service;
4345
cbm_daemon_runtime_client_t *client;
4446
} frontend_idle_fixture_t;
4547

4648
typedef struct {
4749
cbm_daemon_runtime_client_t *client;
4850
cbm_version_cohort_manager_t *manager;
51+
const cbm_daemon_frontend_session_config_t *session;
4952
FILE *input;
5053
FILE *output;
5154
int result;
@@ -99,22 +102,22 @@ static cbm_daemon_runtime_application_status_t frontend_idle_request(
99102
return CBM_DAEMON_RUNTIME_APPLICATION_OK;
100103
}
101104

102-
static void frontend_idle_request_cancel(
103-
void *context, cbm_daemon_runtime_application_session_t *session,
104-
cbm_daemon_runtime_application_token_t request_token) {
105+
static void frontend_idle_request_cancel(void *context,
106+
cbm_daemon_runtime_application_session_t *session,
107+
cbm_daemon_runtime_application_token_t request_token) {
105108
(void)context;
106109
(void)session;
107110
(void)request_token;
108111
}
109112

110-
static void frontend_idle_session_cancel(
111-
void *context, cbm_daemon_runtime_application_session_t *session) {
113+
static void frontend_idle_session_cancel(void *context,
114+
cbm_daemon_runtime_application_session_t *session) {
112115
(void)context;
113116
(void)session;
114117
}
115118

116-
static void frontend_idle_session_close(
117-
void *context, cbm_daemon_runtime_application_session_t *session) {
119+
static void frontend_idle_session_close(void *context,
120+
cbm_daemon_runtime_application_session_t *session) {
118121
(void)context;
119122
(void)session;
120123
}
@@ -128,22 +131,21 @@ static uint64_t frontend_test_process_id(void) {
128131
}
129132

130133
static bool frontend_idle_fixture_start(frontend_idle_fixture_t *fixture) {
131-
static const char cache[] =
132-
"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
134+
static const char cache[] = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";
133135
memset(fixture, 0, sizeof(*fixture));
134136
if (!frontend_maintenance_fixture_start(&fixture->maintenance, "idle-observer")) {
135137
return false;
136138
}
137-
char build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE];
138139
int log_written = snprintf(fixture->conflict_log, sizeof(fixture->conflict_log),
139140
"%s/conflicts.ndjson", fixture->maintenance.parent);
140141
if (log_written <= 0 || log_written >= (int)sizeof(fixture->conflict_log) ||
141-
!cbm_daemon_runtime_process_build_fingerprint(frontend_test_process_id(), build)) {
142+
!cbm_daemon_runtime_process_build_fingerprint(frontend_test_process_id(),
143+
fixture->build_fingerprint)) {
142144
return false;
143145
}
144-
cbm_daemon_build_identity_t identity = {
146+
fixture->identity = (cbm_daemon_build_identity_t){
145147
.semantic_version = "2.4.0",
146-
.build_fingerprint = build,
148+
.build_fingerprint = fixture->build_fingerprint,
147149
.cache_fingerprint = cache,
148150
.protocol_abi = 3,
149151
.store_abi = 11,
@@ -159,7 +161,7 @@ static bool frontend_idle_fixture_start(frontend_idle_fixture_t *fixture) {
159161
};
160162
cbm_daemon_runtime_service_config_t config = {
161163
.endpoint = fixture->maintenance.endpoint,
162-
.identity = identity,
164+
.identity = fixture->identity,
163165
.conflict_log_path = fixture->conflict_log,
164166
.conflict_log_cap_bytes = 64U * 1024U,
165167
.max_clients = 2,
@@ -170,10 +172,11 @@ static bool frontend_idle_fixture_start(frontend_idle_fixture_t *fixture) {
170172
};
171173
fixture->service = cbm_daemon_runtime_service_start(&config);
172174
cbm_daemon_runtime_connect_result_t connect_result = {0};
173-
fixture->client = fixture->service
174-
? cbm_daemon_runtime_client_connect(fixture->maintenance.endpoint, &identity,
175-
30000, &connect_result)
176-
: NULL;
175+
fixture->client =
176+
fixture->service
177+
? cbm_daemon_runtime_client_connect(fixture->maintenance.endpoint, &fixture->identity,
178+
30000, &connect_result)
179+
: NULL;
177180
return fixture->client && connect_result.status == CBM_DAEMON_RUNTIME_CONNECT_ACCEPTED;
178181
}
179182

@@ -213,8 +216,8 @@ static int frontend_test_close_fd(int fd) {
213216

214217
static void *frontend_idle_run(void *opaque) {
215218
frontend_idle_run_t *run = opaque;
216-
run->result =
217-
cbm_daemon_frontend_mcp_run(run->client, run->manager, run->input, run->output);
219+
run->result = cbm_daemon_frontend_mcp_run(run->client, run->manager, run->session, run->input,
220+
run->output);
218221
return NULL;
219222
}
220223
#endif
@@ -479,11 +482,13 @@ static void *frontend_eof_writer(void *opaque) {
479482

480483
static const char frontend_recovery_request[] =
481484
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\",\"params\":{}}\n";
485+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
482486
static const char frontend_recovery_cancel[] =
483487
"{\"jsonrpc\":\"2.0\",\"method\":\"notifications/cancelled\","
484488
"\"params\":{\"requestId\":1}}\n";
485489
static const char frontend_recovery_followup[] =
486490
"{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/list\",\"params\":{}}\n";
491+
#endif
487492

488493
static void *frontend_recovery_writer(void *opaque) {
489494
frontend_eof_writer_t *writer = opaque;
@@ -500,16 +505,24 @@ static void *frontend_recovery_writer(void *opaque) {
500505
ok = ok && atomic_load_explicit(&writer->application->first_request_started,
501506
memory_order_acquire);
502507
}
508+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
503509
if (writer->recovery_mode == FRONTEND_RECOVERY_CANCEL) {
510+
uint64_t cancellations = cbm_daemon_frontend_test_routed_cancellations();
504511
ok = ok && frontend_eof_write_all(writer->fd, frontend_recovery_cancel,
505512
sizeof(frontend_recovery_cancel) - 1U);
506513
/* Keep context blocked until the reader routes the cancellation. */
507-
cbm_usleep(100000);
514+
while (ok && cbm_daemon_frontend_test_routed_cancellations() == cancellations &&
515+
cbm_now_ms() < deadline) {
516+
cbm_usleep(1000);
517+
}
518+
ok = ok && cbm_daemon_frontend_test_routed_cancellations() > cancellations;
508519
atomic_store_explicit(&writer->application->release_first_request, true,
509520
memory_order_release);
510521
ok = ok && frontend_eof_write_all(writer->fd, frontend_recovery_followup,
511522
sizeof(frontend_recovery_followup) - 1U);
512-
} else if (writer->recovery_mode == FRONTEND_RECOVERY_CLEAN_EOF) {
523+
}
524+
#endif
525+
if (writer->recovery_mode == FRONTEND_RECOVERY_CLEAN_EOF) {
513526
ok = close(writer->fd) == 0 && ok;
514527
writer->fd = -1;
515528
cbm_usleep(FRONTEND_RECOVERY_EOF_RELEASE_MS * 1000U);
@@ -1537,9 +1550,20 @@ TEST(daemon_frontend_idle_uses_one_maintenance_observer) {
15371550
bool pipe_ready = fixture_ready && cbm_pipe(input_pipe) == 0;
15381551
FILE *input = pipe_ready ? frontend_test_fdopen_read(input_pipe[0]) : NULL;
15391552
FILE *output = input ? tmpfile() : NULL;
1553+
cbm_daemon_frontend_session_config_t session = {
1554+
.bootstrap = {.role = CBM_DAEMON_PROCESS_MCP_CLIENT,
1555+
.endpoint = fixture.maintenance.endpoint,
1556+
.identity = &fixture.identity,
1557+
.executable_path = "unused",
1558+
.connect_timeout_ms = 30000,
1559+
.startup_timeout_ms = 30000},
1560+
.session_root = fixture.maintenance.parent,
1561+
.tool_profile = CBM_MCP_TOOL_PROFILE_ALL,
1562+
};
15401563
frontend_idle_run_t run = {
15411564
.client = fixture.client,
15421565
.manager = fixture.maintenance.manager,
1566+
.session = &session,
15431567
.input = input,
15441568
.output = output,
15451569
.result = -1,
@@ -1859,10 +1883,12 @@ TEST(daemon_frontend_recovers_one_unsent_request_after_daemon_replacement) {
18591883
}
18601884

18611885
/* Context-replay cancellation must not strand a token or break the next request. */
1886+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
18621887
TEST(daemon_frontend_recovery_cancellation_preserves_next_request) {
18631888
ASSERT_TRUE(frontend_recovery_run_isolated(FRONTEND_RECOVERY_CANCEL));
18641889
PASS();
18651890
}
1891+
#endif
18661892

18671893
/* Clean EOF during context replay must preserve worker-owned client cleanup. */
18681894
TEST(daemon_frontend_clean_eof_during_recovery_preserves_client_ownership) {
@@ -1928,7 +1954,9 @@ SUITE(daemon_frontend) {
19281954
RUN_TEST(daemon_local_participant_monitor_allows_supervisor_containment_window);
19291955
RUN_TEST(daemon_frontend_over_capacity_input_backpressures_without_loss);
19301956
RUN_TEST(daemon_frontend_recovers_one_unsent_request_after_daemon_replacement);
1957+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
19311958
RUN_TEST(daemon_frontend_recovery_cancellation_preserves_next_request);
1959+
#endif
19321960
RUN_TEST(daemon_frontend_clean_eof_during_recovery_preserves_client_ownership);
19331961
RUN_TEST(daemon_frontend_eof_drain_timeout_cancels_and_returns_success);
19341962
RUN_TEST(daemon_frontend_stdout_backpressure_eof_fail_stops_and_cancels_session);

0 commit comments

Comments
 (0)