diff --git a/src/health_monitoring_lib/cpp/health_monitor.cpp b/src/health_monitoring_lib/cpp/health_monitor.cpp index 2fa82400..9984d250 100644 --- a/src/health_monitoring_lib/cpp/health_monitor.cpp +++ b/src/health_monitoring_lib/cpp/health_monitor.cpp @@ -26,8 +26,8 @@ using namespace score::hm::logic; FFICode health_monitor_builder_create(FFIHandle* health_monitor_builder_handle_out); FFICode health_monitor_builder_destroy(FFIHandle health_monitor_builder_handle); FFICode health_monitor_builder_build(FFIHandle health_monitor_builder_handle, - uint32_t supervisor_cycle_ms, - uint32_t internal_cycle_ms, + const uint64_t* supervisor_cycle_ms, + const uint64_t* internal_cycle_ms, FFIHandle* health_monitor_handle_out); FFICode health_monitor_builder_add_deadline_monitor(FFIHandle health_monitor_builder_handle, const MonitorTag* monitor_tag, @@ -115,13 +115,17 @@ HealthMonitorBuilder HealthMonitorBuilder::add_logic_monitor(const MonitorTag& m HealthMonitorBuilder HealthMonitorBuilder::with_internal_processing_cycle(std::chrono::milliseconds cycle_duration) && { - internal_processing_cycle_duration_ = cycle_duration; + auto count{cycle_duration.count()}; + SCORE_LANGUAGE_FUTURECPP_ASSERT_MESSAGE(count >= 0, "cycle duration must be positive"); + internal_processing_cycle_ms_ = count; return std::move(*this); } HealthMonitorBuilder HealthMonitorBuilder::with_supervisor_api_cycle(std::chrono::milliseconds cycle_duration) && { - supervisor_api_cycle_duration_ = cycle_duration; + auto count{cycle_duration.count()}; + SCORE_LANGUAGE_FUTURECPP_ASSERT_MESSAGE(count >= 0, "cycle duration must be positive"); + supervisor_api_cycle_ms_ = count; return std::move(*this); } @@ -130,12 +134,23 @@ score::cpp::expected HealthMonitorBuilder::build() && auto health_monitor_builder_handle = health_monitor_builder_handle_.drop_by_rust(); SCORE_LANGUAGE_FUTURECPP_PRECONDITION(health_monitor_builder_handle.has_value()); - uint32_t supervisor_duration_ms = static_cast(supervisor_api_cycle_duration_.count()); - uint32_t internal_duration_ms = static_cast(internal_processing_cycle_duration_.count()); + // Handle optional parameters. + const uint64_t* supervisor_api_cycle_ms{nullptr}; + const uint64_t* internal_processing_cycle_ms{nullptr}; + if (supervisor_api_cycle_ms_.has_value()) + { + supervisor_api_cycle_ms = &supervisor_api_cycle_ms_.value(); + } + if (internal_processing_cycle_ms_.has_value()) + { + internal_processing_cycle_ms = &internal_processing_cycle_ms_.value(); + } FFIHandle health_monitor_handle{nullptr}; - auto result{health_monitor_builder_build( - health_monitor_builder_handle.value(), supervisor_duration_ms, internal_duration_ms, &health_monitor_handle)}; + auto result{health_monitor_builder_build(health_monitor_builder_handle.value(), + supervisor_api_cycle_ms, + internal_processing_cycle_ms, + &health_monitor_handle)}; if (result != kSuccess) { return score::cpp::unexpected(static_cast(result)); diff --git a/src/health_monitoring_lib/cpp/include/score/hm/health_monitor.h b/src/health_monitoring_lib/cpp/include/score/hm/health_monitor.h index b76d4c39..e9792888 100644 --- a/src/health_monitoring_lib/cpp/include/score/hm/health_monitor.h +++ b/src/health_monitoring_lib/cpp/include/score/hm/health_monitor.h @@ -65,8 +65,8 @@ class HealthMonitorBuilder final private: internal::DroppableFFIHandle health_monitor_builder_handle_; - std::chrono::milliseconds supervisor_api_cycle_duration_; - std::chrono::milliseconds internal_processing_cycle_duration_; + std::optional supervisor_api_cycle_ms_; + std::optional internal_processing_cycle_ms_; }; class HealthMonitor final diff --git a/src/health_monitoring_lib/rust/deadline/ffi.rs b/src/health_monitoring_lib/rust/deadline/ffi.rs index 820acb2a..416f24e5 100644 --- a/src/health_monitoring_lib/rust/deadline/ffi.rs +++ b/src/health_monitoring_lib/rust/deadline/ffi.rs @@ -334,8 +334,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_deadline_monitor( @@ -383,8 +383,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_deadline_monitor( @@ -439,8 +439,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_deadline_monitor( @@ -485,8 +485,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_deadline_monitor( @@ -535,8 +535,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_deadline_monitor( @@ -584,8 +584,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_deadline_monitor( @@ -640,8 +640,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_deadline_monitor( diff --git a/src/health_monitoring_lib/rust/ffi.rs b/src/health_monitoring_lib/rust/ffi.rs index d94f762c..27948340 100644 --- a/src/health_monitoring_lib/rust/ffi.rs +++ b/src/health_monitoring_lib/rust/ffi.rs @@ -109,8 +109,8 @@ pub extern "C" fn health_monitor_builder_destroy(health_monitor_builder_handle: #[unsafe(no_mangle)] pub extern "C" fn health_monitor_builder_build( health_monitor_builder_handle: FFIHandle, - supervisor_cycle_ms: u32, - internal_cycle_ms: u32, + supervisor_cycle_ms: *const u64, + internal_cycle_ms: *const u64, health_monitor_handle_out: *mut FFIHandle, ) -> FFICode { if health_monitor_builder_handle.is_null() || health_monitor_handle_out.is_null() { @@ -124,8 +124,17 @@ pub extern "C" fn health_monitor_builder_build( let mut health_monitor_builder = unsafe { Box::from_raw(health_monitor_builder_handle as *mut HealthMonitorBuilder) }; - health_monitor_builder.with_internal_processing_cycle_internal(Duration::from_millis(internal_cycle_ms as u64)); - health_monitor_builder.with_supervisor_api_cycle_internal(Duration::from_millis(supervisor_cycle_ms as u64)); + // SAFETY: + // `supervisor_cycle_ms` and `internal_cycle_ms` must be a valid pointer when non-null. + // Values are considered unset on null. + if !internal_cycle_ms.is_null() { + let internal_cycle_ms = Duration::from_millis(unsafe { *internal_cycle_ms }); + health_monitor_builder.with_internal_processing_cycle_internal(internal_cycle_ms); + } + if !supervisor_cycle_ms.is_null() { + let supervisor_cycle_ms = Duration::from_millis(unsafe { *supervisor_cycle_ms }); + health_monitor_builder.with_supervisor_api_cycle_internal(supervisor_cycle_ms); + } // Build instance. match health_monitor_builder.build() { @@ -456,8 +465,40 @@ mod tests { let health_monitor_builder_build_result = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), + &mut health_monitor_handle as *mut FFIHandle, + ); + assert!(!health_monitor_handle.is_null()); + assert_eq!(health_monitor_builder_build_result, FFICode::Success); + + // Clean-up. + // NOTE: `health_monitor_destroy` positive path is already tested here. + let health_monitor_destroy_result = health_monitor_destroy(health_monitor_handle); + assert_eq!(health_monitor_destroy_result, FFICode::Success); + } + + #[test] + fn health_monitor_builder_build_valid_cycle_intervals_succeeds() { + let mut health_monitor_builder_handle: FFIHandle = null_mut(); + let mut health_monitor_handle: FFIHandle = null_mut(); + let mut deadline_monitor_builder_handle = null_mut(); + + let _ = health_monitor_builder_create(&mut health_monitor_builder_handle as *mut FFIHandle); + let deadline_monitor_tag = MonitorTag::from("deadline_monitor"); + let _ = deadline_monitor_builder_create(&mut deadline_monitor_builder_handle as *mut FFIHandle); + let _ = health_monitor_builder_add_deadline_monitor( + health_monitor_builder_handle, + &deadline_monitor_tag as *const MonitorTag, + deadline_monitor_builder_handle, + ); + + let supervisor_cycle_ms = 200u64; + let internal_cycle_ms = 100u64; + let health_monitor_builder_build_result = health_monitor_builder_build( + health_monitor_builder_handle, + &supervisor_cycle_ms as *const _, + &internal_cycle_ms as *const _, &mut health_monitor_handle as *mut FFIHandle, ); assert!(!health_monitor_handle.is_null()); @@ -476,10 +517,12 @@ mod tests { let _ = health_monitor_builder_create(&mut health_monitor_builder_handle as *mut FFIHandle); + let supervisor_cycle_ms = 123u64; + let internal_cycle_ms = 100u64; let health_monitor_builder_build_result = health_monitor_builder_build( health_monitor_builder_handle, - 123, - 100, + &supervisor_cycle_ms as *const _, + &internal_cycle_ms as *const _, &mut health_monitor_handle as *mut FFIHandle, ); assert!(health_monitor_handle.is_null()); @@ -497,8 +540,8 @@ mod tests { let health_monitor_builder_build_result = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); assert_eq!(health_monitor_builder_build_result, FFICode::WrongState); @@ -510,8 +553,12 @@ mod tests { fn health_monitor_builder_build_null_builder_handle() { let mut health_monitor_handle: FFIHandle = null_mut(); - let health_monitor_builder_build_result = - health_monitor_builder_build(null_mut(), 200, 100, &mut health_monitor_handle as *mut FFIHandle); + let health_monitor_builder_build_result = health_monitor_builder_build( + null_mut(), + null_mut(), + null_mut(), + &mut health_monitor_handle as *mut FFIHandle, + ); assert!(health_monitor_handle.is_null()); assert_eq!(health_monitor_builder_build_result, FFICode::NullParameter); } @@ -523,7 +570,7 @@ mod tests { let _ = health_monitor_builder_create(&mut health_monitor_builder_handle as *mut FFIHandle); let health_monitor_builder_build_result = - health_monitor_builder_build(health_monitor_builder_handle, 200, 100, null_mut()); + health_monitor_builder_build(health_monitor_builder_handle, null_mut(), null_mut(), null_mut()); assert_eq!(health_monitor_builder_build_result, FFICode::NullParameter); // Clean-up. @@ -808,8 +855,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -844,8 +891,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -889,8 +936,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -923,8 +970,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -956,8 +1003,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -989,8 +1036,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1025,8 +1072,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1070,8 +1117,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1104,8 +1151,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1137,8 +1184,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1169,8 +1216,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1204,8 +1251,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1248,8 +1295,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1281,8 +1328,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1313,8 +1360,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1346,8 +1393,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); @@ -1381,8 +1428,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); diff --git a/src/health_monitoring_lib/rust/heartbeat/ffi.rs b/src/health_monitoring_lib/rust/heartbeat/ffi.rs index 4001e53a..1f0324b1 100644 --- a/src/health_monitoring_lib/rust/heartbeat/ffi.rs +++ b/src/health_monitoring_lib/rust/heartbeat/ffi.rs @@ -163,8 +163,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_heartbeat_monitor( diff --git a/src/health_monitoring_lib/rust/logic/ffi.rs b/src/health_monitoring_lib/rust/logic/ffi.rs index e45cc41f..6be6e445 100644 --- a/src/health_monitoring_lib/rust/logic/ffi.rs +++ b/src/health_monitoring_lib/rust/logic/ffi.rs @@ -350,8 +350,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_logic_monitor( @@ -398,8 +398,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_logic_monitor( @@ -454,8 +454,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_logic_monitor( @@ -502,8 +502,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_logic_monitor( @@ -552,8 +552,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_logic_monitor( @@ -611,8 +611,8 @@ mod tests { ); let _ = health_monitor_builder_build( health_monitor_builder_handle, - 200, - 100, + null_mut(), + null_mut(), &mut health_monitor_handle as *mut FFIHandle, ); let _ = health_monitor_get_logic_monitor(