local_revision_test.rs 6.6 KB

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