local_revision_test.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. use crate::revision_test::script::{RevisionScript::*, RevisionTest};
  2. #[tokio::test]
  3. async fn revision_sync_test() {
  4. let test = RevisionTest::new().await;
  5. let rev_id = 1;
  6. test.run_script(AddLocalRevision {
  7. content: "123".to_string(),
  8. })
  9. .await;
  10. test.run_script(AssertNextSyncRevisionId { rev_id: Some(rev_id) }).await;
  11. test.run_script(AckRevision { rev_id }).await;
  12. test.run_script(AssertNextSyncRevisionId { rev_id: None }).await;
  13. }
  14. #[tokio::test]
  15. async fn revision_compress_2_revisions_with_2_threshold_test() {
  16. let test = RevisionTest::new_with_configuration(2).await;
  17. test.run_script(AddLocalRevision2 {
  18. content: "123".to_string(),
  19. })
  20. .await;
  21. test.run_script(AddLocalRevision2 {
  22. content: "456".to_string(),
  23. })
  24. .await;
  25. test.run_scripts(vec![
  26. AssertNextSyncRevisionId { rev_id: Some(2) },
  27. AssertNextSyncRevisionContent {
  28. expected: "123456".to_string(),
  29. },
  30. AckRevision { rev_id: 2 },
  31. AssertNextSyncRevisionId { rev_id: None },
  32. ])
  33. .await;
  34. }
  35. #[tokio::test]
  36. async fn revision_compress_4_revisions_with_threshold_2_test() {
  37. let test = RevisionTest::new_with_configuration(2).await;
  38. test.run_script(AddLocalRevision {
  39. content: "1".to_string(),
  40. })
  41. .await;
  42. test.run_script(AddLocalRevision {
  43. content: "2".to_string(),
  44. })
  45. .await;
  46. test.run_script(AddLocalRevision {
  47. content: "3".to_string(),
  48. })
  49. .await;
  50. test.run_script(AddLocalRevision {
  51. content: "4".to_string(),
  52. })
  53. .await;
  54. test.run_scripts(vec![
  55. AssertNumberOfSyncRevisions { num: 2 },
  56. AssertNextSyncRevisionId { rev_id: Some(2) },
  57. AssertNextSyncRevisionContent {
  58. expected: "12".to_string(),
  59. },
  60. AckRevision { rev_id: 2 },
  61. AssertNextSyncRevisionId { rev_id: Some(4) },
  62. AssertNextSyncRevisionContent {
  63. expected: "34".to_string(),
  64. },
  65. ])
  66. .await;
  67. }
  68. #[tokio::test]
  69. async fn revision_compress_8_revisions_with_threshold_4_test() {
  70. let merge_len = 4;
  71. let test = RevisionTest::new_with_configuration(merge_len).await;
  72. test.run_script(AddLocalRevision {
  73. content: "1".to_string(),
  74. })
  75. .await;
  76. test.run_script(AddLocalRevision {
  77. content: "2".to_string(),
  78. })
  79. .await;
  80. test.run_script(AddLocalRevision {
  81. content: "3".to_string(),
  82. })
  83. .await;
  84. test.run_script(AddLocalRevision {
  85. content: "4".to_string(),
  86. })
  87. .await;
  88. test.run_script(AddLocalRevision {
  89. content: "a".to_string(),
  90. })
  91. .await;
  92. test.run_script(AddLocalRevision {
  93. content: "b".to_string(),
  94. })
  95. .await;
  96. test.run_script(AddLocalRevision {
  97. content: "c".to_string(),
  98. })
  99. .await;
  100. test.run_script(AddLocalRevision {
  101. content: "d".to_string(),
  102. })
  103. .await;
  104. test.run_scripts(vec![
  105. AssertNumberOfSyncRevisions { num: 2 },
  106. AssertNextSyncRevisionId {
  107. rev_id: Some(merge_len),
  108. },
  109. AssertNextSyncRevisionContent {
  110. expected: "1234".to_string(),
  111. },
  112. AckRevision { rev_id: merge_len },
  113. AssertNextSyncRevisionId {
  114. rev_id: Some(merge_len * 2),
  115. },
  116. AssertNextSyncRevisionContent {
  117. expected: "abcd".to_string(),
  118. },
  119. AckRevision { rev_id: merge_len * 2 },
  120. AssertNextSyncRevisionId { rev_id: None },
  121. ])
  122. .await;
  123. }
  124. #[tokio::test]
  125. async fn revision_merge_per_5_revision_test() {
  126. let merge_len = 5;
  127. let test = RevisionTest::new_with_configuration(merge_len).await;
  128. for i in 0..20 {
  129. let content = format!("{}", i);
  130. test.run_script(AddLocalRevision { content }).await;
  131. }
  132. test.run_scripts(vec![
  133. AssertNumberOfSyncRevisions { num: 4 },
  134. AssertNextSyncRevisionContent {
  135. expected: "01234".to_string(),
  136. },
  137. AckRevision { rev_id: merge_len },
  138. AssertNextSyncRevisionContent {
  139. expected: "56789".to_string(),
  140. },
  141. AckRevision { rev_id: merge_len * 2 },
  142. AssertNextSyncRevisionContent {
  143. expected: "1011121314".to_string(),
  144. },
  145. AckRevision { rev_id: merge_len * 3 },
  146. AssertNextSyncRevisionContent {
  147. expected: "1516171819".to_string(),
  148. },
  149. AckRevision { rev_id: merge_len * 4 },
  150. AssertNextSyncRevisionId { rev_id: None },
  151. ])
  152. .await;
  153. }
  154. #[tokio::test]
  155. async fn revision_merge_per_100_revision_test() {
  156. let test = RevisionTest::new_with_configuration(100).await;
  157. for i in 0..1000 {
  158. let content = format!("{}", i);
  159. test.run_script(AddLocalRevision { content }).await;
  160. }
  161. test.run_scripts(vec![AssertNumberOfSyncRevisions { num: 10 }]).await;
  162. }
  163. #[tokio::test]
  164. async fn revision_merge_per_100_revision_test2() {
  165. let test = RevisionTest::new_with_configuration(100).await;
  166. for i in 0..50 {
  167. test.run_script(AddLocalRevision {
  168. content: format!("{}", i),
  169. })
  170. .await;
  171. }
  172. test.run_scripts(vec![AssertNumberOfSyncRevisions { num: 50 }]).await;
  173. }
  174. #[tokio::test]
  175. async fn revision_merge_per_1000_revision_test() {
  176. let test = RevisionTest::new_with_configuration(1000).await;
  177. for i in 0..100000 {
  178. test.run_script(AddLocalRevision {
  179. content: format!("{}", i),
  180. })
  181. .await;
  182. }
  183. test.run_scripts(vec![AssertNumberOfSyncRevisions { num: 100 }]).await;
  184. }
  185. #[tokio::test]
  186. async fn revision_compress_revision_test() {
  187. let test = RevisionTest::new_with_configuration(2).await;
  188. test.run_scripts(vec![
  189. AddLocalRevision2 {
  190. content: "1".to_string(),
  191. },
  192. AddLocalRevision2 {
  193. content: "2".to_string(),
  194. },
  195. AddLocalRevision2 {
  196. content: "3".to_string(),
  197. },
  198. AddLocalRevision2 {
  199. content: "4".to_string(),
  200. },
  201. AssertNumberOfSyncRevisions { num: 2 },
  202. ])
  203. .await;
  204. }
  205. #[tokio::test]
  206. async fn revision_compress_revision_while_recv_ack_test() {
  207. let test = RevisionTest::new_with_configuration(2).await;
  208. test.run_scripts(vec![
  209. AddLocalRevision2 {
  210. content: "1".to_string(),
  211. },
  212. AckRevision { rev_id: 1 },
  213. AddLocalRevision2 {
  214. content: "2".to_string(),
  215. },
  216. AckRevision { rev_id: 2 },
  217. AddLocalRevision2 {
  218. content: "3".to_string(),
  219. },
  220. AckRevision { rev_id: 3 },
  221. AddLocalRevision2 {
  222. content: "4".to_string(),
  223. },
  224. AssertNumberOfSyncRevisions { num: 4 },
  225. ])
  226. .await;
  227. }