| | |
| | | const CONTROLLED_FIXTURE_ACK_TOPIC: &str = "controlled_fixture_attribute_ack"; |
| | | const CONTROLLED_FIXTURE_PROTOCOL_VERSION: u64 = 1; |
| | | const CONTROLLED_FIXTURE_GENERATION: u64 = 1; |
| | | const CONTROLLED_FIXTURE_PROBE_RECHECKS: usize = 3; |
| | | const CONTROLLED_FIXTURE_PROBE_RECHECK_DELAY: Duration = Duration::from_millis(25); |
| | | const CONTROLLED_FIXTURE_PROBE_TTL: Duration = Duration::from_millis(250); |
| | | |
| | |
| | | return Err("wrong_sequence"); |
| | | } |
| | | Ok(()) |
| | | } |
| | | |
| | | async fn observe_controlled_fixture_attributes<F>( |
| | | expires_at: Instant, |
| | | actual_participant: &str, |
| | | expected_participant: Option<&str>, |
| | | requested_sequence: &str, |
| | | mut read_attributes: F, |
| | | ) -> Result<(), &'static str> |
| | | where |
| | | F: FnMut() -> std::collections::HashMap<String, String>, |
| | | { |
| | | loop { |
| | | if Instant::now() > expires_at { |
| | | return Err("timeout"); |
| | | } |
| | | let decision = classify_controlled_fixture_attributes( |
| | | actual_participant, |
| | | expected_participant, |
| | | &read_attributes(), |
| | | requested_sequence, |
| | | ); |
| | | if decision.is_ok() || !matches!(decision, Err("missing_attributes")) { |
| | | return decision; |
| | | } |
| | | let Some(next_check) = Instant::now().checked_add(CONTROLLED_FIXTURE_PROBE_RECHECK_DELAY) |
| | | else { |
| | | return Err("timeout"); |
| | | }; |
| | | if next_check > expires_at { |
| | | return Err("timeout"); |
| | | } |
| | | sleep(CONTROLLED_FIXTURE_PROBE_RECHECK_DELAY).await; |
| | | } |
| | | } |
| | | |
| | | fn controlled_fixture_observer_gate(pending_probe: bool, probe_result: Option<bool>) -> bool { |
| | |
| | | if participant.identity() != probe.sender { |
| | | return Some(false); |
| | | } |
| | | let mut decision = Err("timeout"); |
| | | for attempt in 0..CONTROLLED_FIXTURE_PROBE_RECHECKS { |
| | | if Instant::now() > probe.expires_at { |
| | | break; |
| | | } |
| | | let attributes = participant.attributes(); |
| | | decision = classify_controlled_fixture_attributes( |
| | | &participant.identity().to_string(), |
| | | expected_participant, |
| | | &attributes, |
| | | &probe.sequence, |
| | | ); |
| | | if decision.is_ok() || !matches!(decision, Err("missing_attributes")) { |
| | | break; |
| | | } |
| | | if attempt + 1 < CONTROLLED_FIXTURE_PROBE_RECHECKS { |
| | | sleep(CONTROLLED_FIXTURE_PROBE_RECHECK_DELAY).await; |
| | | } |
| | | } |
| | | let decision = observe_controlled_fixture_attributes( |
| | | probe.expires_at, |
| | | &participant.identity().to_string(), |
| | | expected_participant, |
| | | &probe.sequence, |
| | | || participant.attributes(), |
| | | ) |
| | | .await; |
| | | let (result, input_source_category, reject_reason) = match decision { |
| | | Ok(()) => ("observed", Some("controlled_fixture"), None), |
| | | Err(reason) => ("rejected", None, Some(reason)), |
| | |
| | | ); |
| | | } |
| | | |
| | | #[tokio::test] |
| | | async fn production_attribute_observation_accepts_server_visibility_within_probe_ttl() { |
| | | let expected = HashMap::from([ |
| | | ( |
| | | "inputSourceCategory".to_string(), |
| | | "controlled_fixture".to_string(), |
| | | ), |
| | | ( |
| | | "clientFixtureSequence".to_string(), |
| | | "fixture-01".to_string(), |
| | | ), |
| | | ]); |
| | | let mut reads = 0; |
| | | let result = observe_controlled_fixture_attributes( |
| | | Instant::now() + CONTROLLED_FIXTURE_PROBE_TTL, |
| | | "user-1", |
| | | Some("user-1"), |
| | | "fixture-01", |
| | | || { |
| | | reads += 1; |
| | | if reads <= 3 { |
| | | HashMap::new() |
| | | } else { |
| | | expected.clone() |
| | | } |
| | | }, |
| | | ) |
| | | .await; |
| | | assert_eq!(result, Ok(())); |
| | | assert_eq!(reads, 4); |
| | | } |
| | | |
| | | #[tokio::test] |
| | | async fn production_attribute_observation_rejects_wrong_sequence_without_audio_effect() { |
| | | let attributes = HashMap::from([ |
| | | ( |
| | | "inputSourceCategory".to_string(), |
| | | "controlled_fixture".to_string(), |
| | | ), |
| | | ( |
| | | "clientFixtureSequence".to_string(), |
| | | "fixture-02".to_string(), |
| | | ), |
| | | ]); |
| | | let result = observe_controlled_fixture_attributes( |
| | | Instant::now() + CONTROLLED_FIXTURE_PROBE_TTL, |
| | | "user-1", |
| | | Some("user-1"), |
| | | "fixture-01", |
| | | || attributes.clone(), |
| | | ) |
| | | .await; |
| | | let mut observer_starts = 0; |
| | | assert_eq!(result, Err("wrong_sequence")); |
| | | assert!(!start_observer_after_controlled_fixture_probe( |
| | | true, |
| | | Some(result.is_ok()), |
| | | || observer_starts += 1, |
| | | )); |
| | | assert_eq!(observer_starts, 0); |
| | | } |
| | | |
| | | #[test] |
| | | fn controlled_fixture_ack_payload_is_reliable_and_redacted() { |
| | | let ack = ControlledFixtureAttributeAck { |