stress.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //! Stress tests for liveness and consistency properties.
  2. use std::time::Duration;
  3. use anyhow::{anyhow, Result};
  4. use common::*;
  5. use log::info;
  6. use operational_transform::OperationSeq;
  7. use rustpad_server::{server, ServerConfig};
  8. use serde_json::{json, Value};
  9. use tokio::time::Instant;
  10. pub mod common;
  11. #[tokio::test]
  12. async fn test_lost_wakeups() -> Result<()> {
  13. pretty_env_logger::try_init().ok();
  14. let filter = server(ServerConfig::default());
  15. expect_text(&filter, "stress", "").await;
  16. let mut client = connect(&filter, "stress").await?;
  17. let msg = client.recv().await?;
  18. assert_eq!(msg, json!({ "Identity": 0 }));
  19. let mut client2 = connect(&filter, "stress").await?;
  20. let msg = client2.recv().await?;
  21. assert_eq!(msg, json!({ "Identity": 1 }));
  22. let mut revision = 0;
  23. for i in 0..100 {
  24. let num_edits = i % 5 + 1;
  25. for _ in 0..num_edits {
  26. let mut operation = OperationSeq::default();
  27. operation.retain(revision);
  28. operation.insert("a");
  29. let msg = json!({
  30. "Edit": {
  31. "revision": revision,
  32. "operation": operation
  33. }
  34. });
  35. client.send(&msg).await;
  36. revision += 1;
  37. }
  38. let start = Instant::now();
  39. let num_ops = |msg: &Value| -> Option<usize> {
  40. Some(msg.get("History")?.get("operations")?.as_array()?.len())
  41. };
  42. let mut total = 0;
  43. while total < num_edits {
  44. let msg = client.recv().await?;
  45. total += num_ops(&msg).ok_or_else(|| anyhow!("missing json key"))?;
  46. }
  47. let mut total2 = 0;
  48. while total2 < num_edits {
  49. let msg = client2.recv().await?;
  50. total2 += num_ops(&msg).ok_or_else(|| anyhow!("missing json key"))?;
  51. }
  52. info!("took {} ms", start.elapsed().as_millis());
  53. assert!(start.elapsed() <= Duration::from_millis(200));
  54. }
  55. expect_text(&filter, "stress", &"a".repeat(revision as usize)).await;
  56. Ok(())
  57. }
  58. #[tokio::test]
  59. async fn test_large_document() -> Result<()> {
  60. pretty_env_logger::try_init().ok();
  61. let filter = server(ServerConfig::default());
  62. expect_text(&filter, "stress", "").await;
  63. let mut client = connect(&filter, "stress").await?;
  64. let msg = client.recv().await?;
  65. assert_eq!(msg, json!({ "Identity": 0 }));
  66. let mut operation = OperationSeq::default();
  67. operation.insert(&"a".repeat(5000));
  68. let msg = json!({
  69. "Edit": {
  70. "revision": 0,
  71. "operation": operation
  72. }
  73. });
  74. client.send(&msg).await;
  75. client.recv().await?;
  76. let mut operation = OperationSeq::default();
  77. operation.insert(&"a".repeat(500000));
  78. let msg = json!({
  79. "Edit": {
  80. "revision": 0,
  81. "operation": operation
  82. }
  83. });
  84. client.send(&msg).await;
  85. client.recv_closed().await?;
  86. Ok(())
  87. }