quorum-test.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Test the QUORUM library. Requires that corosync is running and that we are root.
  2. extern crate rust_corosync as corosync;
  3. use corosync::{quorum, NodeId};
  4. fn quorum_fn(
  5. _handle: &quorum::Handle,
  6. quorate: bool,
  7. ring_id: quorum::RingId,
  8. member_list: Vec<NodeId>,
  9. ) {
  10. println!("TEST quorum_fn called. quorate = {quorate}");
  11. println!(" ring_id: {}/{}", ring_id.nodeid, ring_id.seq);
  12. println!(" members: {member_list:?}");
  13. }
  14. fn nodelist_fn(
  15. _handle: &quorum::Handle,
  16. ring_id: quorum::RingId,
  17. member_list: Vec<NodeId>,
  18. joined_list: Vec<NodeId>,
  19. left_list: Vec<NodeId>,
  20. ) {
  21. println!(
  22. "TEST nodelist_fn called for {}/{}",
  23. ring_id.nodeid, ring_id.seq
  24. );
  25. println!(" members: {member_list:?}");
  26. println!(" joined: {joined_list:?}");
  27. println!(" left: {left_list:?}");
  28. }
  29. fn main() {
  30. // Initialise the model data
  31. let md = quorum::ModelData::ModelV1(quorum::Model1Data {
  32. flags: quorum::Model1Flags::None,
  33. quorum_notification_fn: Some(quorum_fn),
  34. nodelist_notification_fn: Some(nodelist_fn),
  35. });
  36. let handle = match quorum::initialize(&md, 99_u64) {
  37. Ok((h, t)) => {
  38. println!("Quorum initialized; type = {}", t as u32);
  39. h
  40. }
  41. Err(e) => {
  42. println!("Error in QUORUM init: {e}");
  43. return;
  44. }
  45. };
  46. // Test context APIs
  47. let set_context: u64 = 0xabcdbeefcafe;
  48. if let Err(e) = quorum::context_set(&handle, set_context) {
  49. println!("Error in QUORUM context_set: {e}");
  50. return;
  51. }
  52. // NOTE This will fail on 32 bit systems because void* is not u64
  53. match quorum::context_get(&handle) {
  54. Ok(c) => {
  55. if c != set_context {
  56. println!("Error: context_get() returned {c:x}, context should be {set_context:x}");
  57. }
  58. }
  59. Err(e) => {
  60. println!("Error in QUORUM context_get: {e}");
  61. }
  62. }
  63. if let Err(e) = quorum::trackstart(&handle, corosync::TrackFlags::Changes) {
  64. println!("Error in QUORUM trackstart: {e}");
  65. return;
  66. }
  67. // Wait for events
  68. loop {
  69. if quorum::dispatch(&handle, corosync::DispatchFlags::One).is_err() {
  70. break;
  71. }
  72. }
  73. println!("ERROR: Corosync quit");
  74. }