local_revision_test.rs 6.5 KB

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