//! Registry simulation tests — cluster registry CRDT behavior under gossip. //! //! Tests that registry names propagate, converge, and resolve correctly //! across the cluster under various fault conditions. use simulation::distribution::properties::{ check_registry_propagation, check_registry_tombstones, }; use simulation::distribution::sim::{ run_simulation, run_simulation_with_nodes, DistributionSimConfig, NetworkFault, Partition, SimAction, }; fn default_config() -> DistributionSimConfig { DistributionSimConfig { actors_per_node: 0, // Registry tests don't need actors ..DistributionSimConfig::default() } } // ──────────────────────────────────────────────────────────────────────────── // 1. Registry name converges across 5-node cluster via gossip piggyback // ──────────────────────────────────────────────────────────────────────────── #[test] fn registry_name_converges_across_cluster() { // Given: 5-node cluster, node 0 registers "counter" at round 5 let config = DistributionSimConfig { name: "registry-convergence".into(), num_nodes: 5, num_rounds: 50, ticks_per_round: 3, action_schedule: vec![ (5, SimAction::RegisterName { node_idx: 0, name: "counter".into() }), ], ..default_config() }; // When: we run the simulation let (trace, nodes) = run_simulation_with_nodes(config); // Then: all alive nodes should have the registry entry let result = check_registry_propagation(&trace, 1); assert!( result.passed, "all nodes should see the 'counter' name: {}", result.actual ); // And: all nodes should resolve "counter" to the same actor let resolutions: Vec<_> = nodes .iter() .filter_map(|n| n.as_ref()) .map(|n| n.resolve_name("counter")) .collect(); assert!( resolutions.iter().all(|r| r.is_some()), "all nodes should resolve 'counter', got: {resolutions:?}" ); // All should agree on the same actor address let first = resolutions[0].unwrap().0; assert!( resolutions.iter().all(|r| r.unwrap().0 == first), "all nodes should agree on the same actor for 'counter'" ); } // ──────────────────────────────────────────────────────────────────────────── // 2. Split-brain naming — two sides register same name during partition // ──────────────────────────────────────────────────────────────────────────── #[test] fn split_brain_naming_converges_after_partition_heals() { // Given: 6-node cluster, partition {0,1,2} vs {3,4,5} at round 10 // Node 0 registers "leader" at round 12, node 3 registers "leader" at round 12 // Heal at round 40 let config = DistributionSimConfig { name: "split-brain-registry".into(), num_nodes: 6, num_rounds: 100, ticks_per_round: 3, network_faults: vec![ NetworkFault::Partition { round: 10, partition: Partition { side_a: vec![0, 1, 2], side_b: vec![3, 4, 5], asymmetric: false, }, }, NetworkFault::Heal { round: 40 }, ], action_schedule: vec![ (12, SimAction::RegisterName { node_idx: 0, name: "leader".into() }), (12, SimAction::RegisterName { node_idx: 3, name: "leader".into() }), ], swim: distribution::swim::probe::SwimConfig { probe_interval: 1, probe_timeout: 3, indirect_probes: 1, suspicion_timeout: 5, dead_reprobe_interval: 10, }, ..default_config() }; // When: we run the simulation let (_trace, nodes) = run_simulation_with_nodes(config); // Then: all alive nodes should resolve "leader" to the same value (LWW winner) let resolutions: Vec<_> = nodes .iter() .filter_map(|n| n.as_ref()) .map(|n| n.resolve_name("leader")) .collect(); // All should resolve to Some (the LWW winner) let resolved: Vec<_> = resolutions.iter().filter_map(|r| r.as_ref()).collect(); assert!( resolved.len() >= 4, "at least 4 of 6 nodes should resolve 'leader', got {} out of {}", resolved.len(), resolutions.len() ); // All resolving nodes should agree on the same actor if resolved.len() >= 2 { let first_actor = resolved[0].0; let agree = resolved.iter().all(|r| r.0 == first_actor); assert!( agree, "all nodes resolving 'leader' should agree on the same actor (LWW winner)" ); } } // ──────────────────────────────────────────────────────────────────────────── // 3. Tombstone propagation on node death // ──────────────────────────────────────────────────────────────────────────── #[test] fn tombstone_propagates_when_name_owner_dies() { // Given: 5-node cluster, node 0 registers "svc" at round 5, killed at round 15 let config = DistributionSimConfig { name: "tombstone-propagation".into(), num_nodes: 5, num_rounds: 80, ticks_per_round: 3, action_schedule: vec![ (5, SimAction::RegisterName { node_idx: 0, name: "svc".into() }), ], kill_schedule: vec![(15, 0)], ..default_config() }; // When: we run the simulation let (trace, nodes) = run_simulation_with_nodes(config); // Then: surviving nodes should have tombstoned "svc" let result = check_registry_tombstones(&trace, 1); assert!( result.passed, "survivors should have tombstones after node death: {}", result.actual ); // And: resolve_name("svc") should return None on all survivors let resolutions: Vec<_> = nodes .iter() .filter_map(|n| n.as_ref()) .map(|n| n.resolve_name("svc")) .collect(); assert!( resolutions.iter().all(|r| r.is_none()), "all survivors should resolve 'svc' to None after owner dies, got: {resolutions:?}" ); } // ──────────────────────────────────────────────────────────────────────────── // 4. Rapid re-registration converges to latest value // ──────────────────────────────────────────────────────────────────────────── #[test] fn rapid_re_registration_converges_to_latest() { // Given: 5-node cluster, node 0 registers "svc" three times rapidly use swactor::actor::ActorAddress; let actor_a = ActorAddress::new_random(); let actor_b = ActorAddress::new_random(); let actor_c = ActorAddress::new_random(); let config = DistributionSimConfig { name: "rapid-reregister".into(), num_nodes: 5, num_rounds: 60, ticks_per_round: 3, action_schedule: vec![ (5, SimAction::RegisterNameWithActor { node_idx: 0, name: "svc".into(), actor: actor_a }), (6, SimAction::RegisterNameWithActor { node_idx: 0, name: "svc".into(), actor: actor_b }), (7, SimAction::RegisterNameWithActor { node_idx: 0, name: "svc".into(), actor: actor_c }), ], ..default_config() }; // When: we run the simulation let (_trace, nodes) = run_simulation_with_nodes(config); // Then: all nodes should resolve "svc" to actor_c (the latest registration) let resolutions: Vec<_> = nodes .iter() .filter_map(|n| n.as_ref()) .map(|n| n.resolve_name("svc")) .collect(); assert!( resolutions.iter().all(|r| r.is_some()), "all nodes should resolve 'svc'" ); assert!( resolutions.iter().all(|r| r.unwrap().0 == actor_c), "all nodes should converge to the latest registration (actor_c)" ); } // ──────────────────────────────────────────────────────────────────────────── // 5. Simultaneous registration of same name on different nodes // ──────────────────────────────────────────────────────────────────────────── #[test] fn simultaneous_registration_converges_deterministically() { // Given: 5-node cluster, node 0 and node 3 both register "mutex" at round 5 use swactor::actor::ActorAddress; let actor_a = ActorAddress::new_random(); let actor_b = ActorAddress::new_random(); let config = DistributionSimConfig { name: "simultaneous-register".into(), num_nodes: 5, num_rounds: 60, ticks_per_round: 3, action_schedule: vec![ (5, SimAction::RegisterNameWithActor { node_idx: 0, name: "mutex".into(), actor: actor_a }), (5, SimAction::RegisterNameWithActor { node_idx: 3, name: "mutex".into(), actor: actor_b }), ], ..default_config() }; // When: we run the simulation let (_trace, nodes) = run_simulation_with_nodes(config); // Then: all nodes should agree on one winner let resolutions: Vec<_> = nodes .iter() .filter_map(|n| n.as_ref()) .map(|n| n.resolve_name("mutex")) .collect(); assert!( resolutions.iter().all(|r| r.is_some()), "all nodes should resolve 'mutex'" ); // All should agree on the same actor (whichever won the LWW tiebreaker) let first_actor = resolutions[0].unwrap().0; assert!( resolutions.iter().all(|r| r.unwrap().0 == first_actor), "all nodes should agree on the LWW winner for 'mutex'" ); } // ──────────────────────────────────────────────────────────────────────────── // 6. Multiple names propagate correctly // ──────────────────────────────────────────────────────────────────────────── #[test] fn multiple_names_from_different_nodes_all_propagate() { // Given: 5-node cluster, each node registers a unique name let config = DistributionSimConfig { name: "multi-name-propagation".into(), num_nodes: 5, num_rounds: 60, ticks_per_round: 3, action_schedule: vec![ (5, SimAction::RegisterName { node_idx: 0, name: "svc-0".into() }), (5, SimAction::RegisterName { node_idx: 1, name: "svc-1".into() }), (5, SimAction::RegisterName { node_idx: 2, name: "svc-2".into() }), (5, SimAction::RegisterName { node_idx: 3, name: "svc-3".into() }), (5, SimAction::RegisterName { node_idx: 4, name: "svc-4".into() }), ], ..default_config() }; // When: we run the simulation let (trace, nodes) = run_simulation_with_nodes(config); // Then: all 5 nodes should have all 5 registry entries let result = check_registry_propagation(&trace, 5); assert!( result.passed, "all nodes should have all 5 registry entries: {}", result.actual ); // And: each node should resolve all 5 names for node in nodes.iter().filter_map(|n| n.as_ref()) { for i in 0..5 { let name = format!("svc-{i}"); assert!( node.resolve_name(&name).is_some(), "every node should resolve '{name}'" ); } } }