Skip to content

Commit 9f34cb6

Browse files
committed
refactor: update exit cause handling in Worker API
1 parent 8f1d9eb commit 9f34cb6

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

post-compute/src/api/worker_api.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,21 @@ impl WorkerApiClient {
6060
Self::new(&base_url)
6161
}
6262

63-
/// Sends an exit cause for a post-compute operation to the Worker API.
63+
/// Sends exit causes for a post-compute operation to the Worker API.
6464
///
65-
/// This method reports the exit cause of a post-compute operation to the Worker API,
65+
/// This method reports the exit causes of a post-compute operation to the Worker API,
6666
/// which can be used for tracking and debugging purposes.
6767
///
6868
/// # Arguments
6969
///
7070
/// * `authorization` - The authorization token to use for the API request
71-
/// * `chain_task_id` - The chain task ID for which to report the exit cause
72-
/// * `exit_cause` - The exit cause to report
71+
/// * `chain_task_id` - The chain task ID for which to report the exit causes
72+
/// * `exit_causes` - The exit causes to report
7373
///
7474
/// # Returns
7575
///
76-
/// * `Ok(())` - If the exit cause was successfully reported
77-
/// * `Err(ReplicateStatusCause)` - If the exit cause could not be reported due to an HTTP error
76+
/// * `Ok(())` - If the exit causes were successfully reported
77+
/// * `Err(ReplicateStatusCause)` - If the exit causes could not be reported due to an HTTP error
7878
///
7979
/// # Errors
8080
///
@@ -92,22 +92,22 @@ impl WorkerApiClient {
9292
/// let client = WorkerApiClient::new("http://worker:13100");
9393
/// let exit_causes = vec![ReplicateStatusCause::PostComputeInvalidTeeSignature];
9494
///
95-
/// match client.send_exit_cause_for_post_compute_stage(
95+
/// match client.send_exit_causes_for_post_compute_stage(
9696
/// "authorization_token",
9797
/// "0x123456789abcdef",
9898
/// &exit_causes,
9999
/// ) {
100-
/// Ok(()) => println!("Exit cause reported successfully"),
101-
/// Err(error) => eprintln!("Failed to report exit cause: {}", error),
100+
/// Ok(()) => println!("Exit causes reported successfully"),
101+
/// Err(error) => eprintln!("Failed to report exit causes: {}", error),
102102
/// }
103103
/// ```
104-
pub fn send_exit_cause_for_post_compute_stage(
104+
pub fn send_exit_causes_for_post_compute_stage(
105105
&self,
106106
authorization: &str,
107107
chain_task_id: &str,
108108
exit_causes: &[ReplicateStatusCause],
109109
) -> Result<(), ReplicateStatusCause> {
110-
let url = format!("{}/compute/post/{chain_task_id}/exit", self.base_url);
110+
let url = format!("{}/compute/post/{chain_task_id}/exit-causes", self.base_url);
111111
match self
112112
.client
113113
.post(&url)
@@ -122,13 +122,13 @@ impl WorkerApiClient {
122122
let status = response.status();
123123
let body = response.text().unwrap_or_default();
124124
error!(
125-
"Failed to send exit cause to worker: [status:{status:?}, body:{body:#?}]"
125+
"Failed to send exit causes to worker: [status:{status:?}, body:{body:#?}]"
126126
);
127127
Err(ReplicateStatusCause::PostComputeFailedUnknownIssue)
128128
}
129129
}
130130
Err(e) => {
131-
error!("An error occured while sending exit cause to worker: {e}");
131+
error!("An error occured while sending exit causes to worker: {e}");
132132
Err(ReplicateStatusCause::PostComputeFailedUnknownIssue)
133133
}
134134
}
@@ -232,7 +232,7 @@ mod tests {
232232

233233
// region serialize List of ReplicateStatusCause
234234
#[test]
235-
fn should_serialize_list_of_exit_causes() {
235+
fn replicate_status_cause_serializes_as_json_array_when_multiple_causes() {
236236
let causes = vec![
237237
ReplicateStatusCause::PostComputeInvalidTeeSignature,
238238
ReplicateStatusCause::PostComputeWorkerAddressMissing,
@@ -243,7 +243,7 @@ mod tests {
243243
}
244244

245245
#[test]
246-
fn should_serialize_single_exit_cause() {
246+
fn replicate_status_cause_serializes_as_json_array_when_single_cause() {
247247
let causes = vec![ReplicateStatusCause::PostComputeFailedUnknownIssue];
248248
let serialized = to_string(&causes).expect("Failed to serialize");
249249
let expected = r#"[{"cause":"POST_COMPUTE_FAILED_UNKNOWN_ISSUE","message":"Unexpected error occurred"}]"#;
@@ -253,7 +253,7 @@ mod tests {
253253

254254
// region get_worker_api_client
255255
#[test]
256-
fn should_get_worker_api_client_with_env_var() {
256+
fn from_env_creates_client_with_custom_url_when_env_var_set() {
257257
with_vars(
258258
vec![(WorkerHostEnvVar.name(), Some("custom-worker-host:9999"))],
259259
|| {
@@ -264,24 +264,24 @@ mod tests {
264264
}
265265

266266
#[test]
267-
fn should_get_worker_api_client_without_env_var() {
267+
fn from_env_creates_client_with_default_url_when_env_var_missing() {
268268
with_vars(vec![(WorkerHostEnvVar.name(), None::<&str>)], || {
269269
let client = WorkerApiClient::from_env();
270270
assert_eq!(client.base_url, format!("http://{DEFAULT_WORKER_HOST}"));
271271
});
272272
}
273273
// endregion
274274

275-
// region send_exit_cause_for_post_compute_stage()
275+
// region send_exit_causes_for_post_compute_stage()
276276
#[tokio::test]
277-
async fn should_send_exit_cause() {
277+
async fn send_exit_causes_for_post_compute_stage_succeeds_when_server_responds_ok() {
278278
let mock_server = MockServer::start().await;
279279
let server_url = mock_server.uri();
280280

281281
let expected_body = json!([ReplicateStatusCause::PostComputeInvalidTeeSignature,]);
282282

283283
Mock::given(method("POST"))
284-
.and(path(format!("/compute/post/{CHAIN_TASK_ID}/exit")))
284+
.and(path(format!("/compute/post/{CHAIN_TASK_ID}/exit-causes")))
285285
.and(header("Authorization", CHALLENGE))
286286
.and(body_json(&expected_body))
287287
.respond_with(ResponseTemplate::new(200))
@@ -292,7 +292,7 @@ mod tests {
292292
let result = tokio::task::spawn_blocking(move || {
293293
let exit_causes = vec![ReplicateStatusCause::PostComputeInvalidTeeSignature];
294294
let worker_api_client = WorkerApiClient::new(&server_url);
295-
worker_api_client.send_exit_cause_for_post_compute_stage(
295+
worker_api_client.send_exit_causes_for_post_compute_stage(
296296
CHALLENGE,
297297
CHAIN_TASK_ID,
298298
&exit_causes,
@@ -306,7 +306,7 @@ mod tests {
306306

307307
#[tokio::test]
308308
#[serial]
309-
async fn should_not_send_exit_cause() {
309+
async fn send_exit_causes_for_post_compute_stage_fails_when_server_returns_404() {
310310
{
311311
let mut logger = TEST_LOGGER.lock().unwrap();
312312
while logger.pop().is_some() {}
@@ -315,7 +315,7 @@ mod tests {
315315
let server_url = mock_server.uri();
316316

317317
Mock::given(method("POST"))
318-
.and(path(format!("/compute/post/{CHAIN_TASK_ID}/exit")))
318+
.and(path(format!("/compute/post/{CHAIN_TASK_ID}/exit-causes")))
319319
.respond_with(ResponseTemplate::new(404))
320320
.expect(1)
321321
.mount(&mock_server)
@@ -324,7 +324,7 @@ mod tests {
324324
let result = tokio::task::spawn_blocking(move || {
325325
let exit_causes = vec![ReplicateStatusCause::PostComputeFailedUnknownIssue];
326326
let worker_api_client = WorkerApiClient::new(&server_url);
327-
worker_api_client.send_exit_cause_for_post_compute_stage(
327+
worker_api_client.send_exit_causes_for_post_compute_stage(
328328
CHALLENGE,
329329
CHAIN_TASK_ID,
330330
&exit_causes,
@@ -356,7 +356,7 @@ mod tests {
356356

357357
// region send_computed_file_to_host()
358358
#[tokio::test]
359-
async fn should_send_computed_file_successfully() {
359+
async fn send_computed_file_to_host_succeeds_when_server_responds_ok() {
360360
let mock_server = MockServer::start().await;
361361
let server_uri = mock_server.uri();
362362

@@ -391,7 +391,7 @@ mod tests {
391391

392392
#[tokio::test]
393393
#[serial]
394-
async fn should_fail_send_computed_file_on_server_error() {
394+
async fn send_computed_file_to_host_fails_when_server_returns_500() {
395395
{
396396
let mut logger = TEST_LOGGER.lock().unwrap();
397397
while logger.pop().is_some() {}
@@ -445,7 +445,7 @@ mod tests {
445445

446446
#[tokio::test]
447447
#[serial]
448-
async fn should_handle_invalid_chain_task_id_in_url() {
448+
async fn send_computed_file_to_host_fails_when_chain_task_id_invalid() {
449449
{
450450
let mut logger = TEST_LOGGER.lock().unwrap();
451451
while logger.pop().is_some() {}
@@ -486,7 +486,7 @@ mod tests {
486486
}
487487

488488
#[tokio::test]
489-
async fn should_send_computed_file_with_minimal_data() {
489+
async fn send_computed_file_to_host_succeeds_when_minimal_data_provided() {
490490
let mock_server = MockServer::start().await;
491491
let server_uri = mock_server.uri();
492492

post-compute/src/compute/app_runner.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub enum ExitMode {
3131
pub trait PostComputeRunnerInterface {
3232
fn run_post_compute(&self, chain_task_id: &str) -> Result<(), ReplicateStatusCause>;
3333
fn get_challenge(&self, chain_task_id: &str) -> Result<String, ReplicateStatusCause>;
34-
fn send_exit_cause(
34+
fn send_exit_causes(
3535
&self,
3636
authorization: &str,
3737
chain_task_id: &str,
@@ -99,14 +99,14 @@ impl PostComputeRunnerInterface for DefaultPostComputeRunner {
9999
get_challenge(chain_task_id)
100100
}
101101

102-
fn send_exit_cause(
102+
fn send_exit_causes(
103103
&self,
104104
authorization: &str,
105105
chain_task_id: &str,
106106
exit_causes: &[ReplicateStatusCause],
107107
) -> Result<(), ReplicateStatusCause> {
108108
self.worker_api_client
109-
.send_exit_cause_for_post_compute_stage(authorization, chain_task_id, exit_causes)
109+
.send_exit_causes_for_post_compute_stage(authorization, chain_task_id, exit_causes)
110110
}
111111

112112
fn send_computed_file(&self, computed_file: &ComputedFile) -> Result<(), ReplicateStatusCause> {
@@ -191,10 +191,10 @@ pub fn start_with_runner<R: PostComputeRunnerInterface>(runner: &R) -> ExitMode
191191

192192
let exit_causes = vec![exit_cause.clone()];
193193

194-
match runner.send_exit_cause(&authorization, &chain_task_id, &exit_causes) {
194+
match runner.send_exit_causes(&authorization, &chain_task_id, &exit_causes) {
195195
Ok(()) => ExitMode::ReportedFailure,
196196
Err(_) => {
197-
error!("Failed to report exit cause [exitCause:{exit_cause}]");
197+
error!("Failed to report exit causes [exitCauses:{exit_causes:?}]");
198198
ExitMode::UnreportedFailure
199199
}
200200
}
@@ -273,7 +273,7 @@ mod tests {
273273
self
274274
}
275275

276-
fn with_send_exit_cause_failure(mut self) -> Self {
276+
fn with_send_exit_causes_failure(mut self) -> Self {
277277
self.send_exit_cause_success = false;
278278
self
279279
}
@@ -298,7 +298,7 @@ mod tests {
298298
}
299299
}
300300

301-
fn send_exit_cause(
301+
fn send_exit_causes(
302302
&self,
303303
_authorization: &str,
304304
_chain_task_id: &str,
@@ -423,7 +423,7 @@ mod tests {
423423
}
424424

425425
#[test]
426-
fn start_return_2_when_exit_cause_not_transmitted() {
426+
fn start_return_2_when_exit_causes_not_transmitted() {
427427
with_vars(
428428
vec![(
429429
TeeSessionEnvironmentVariable::IexecTaskId.name(),
@@ -434,7 +434,7 @@ mod tests {
434434
.with_run_post_compute_failure(Some(
435435
ReplicateStatusCause::PostComputeInvalidTeeSignature,
436436
))
437-
.with_send_exit_cause_failure();
437+
.with_send_exit_causes_failure();
438438

439439
let result = start_with_runner(&runner);
440440
assert_eq!(

post-compute/src/compute/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod tests {
6262
#[test]
6363
fn error_variant_serialize_correctly() {
6464
let expected = json!({
65-
"cause": "POST_COMPUTE_TooLongResultFileName",
65+
"cause": "POST_COMPUTE_TOO_LONG_RESULT_FILE_NAME",
6666
"message": "Result file name too long"
6767
});
6868
let error_variant = ReplicateStatusCause::PostComputeTooLongResultFileName;

0 commit comments

Comments
 (0)