transactional-map.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // TransactionalMap:原子事件事务里共享的大侧表(docs/v4-refactor/02-projection.md「原子事件事务」)。
  2. // 回滚后内容与迭代顺序都必须回到事务开始时;提交后保留事务内的写入,与在副本上做同样操作的结果相同。
  3. import { describe, expect, it, vi } from "vitest";
  4. import { TransactionalMap } from "../src/zcode-protocol-v4/product-projection.js";
  5. type Op = readonly ["set", string, number] | readonly ["delete", string] | readonly ["clear"];
  6. function apply(map: Map<string, number>, ops: readonly Op[]): void {
  7. for (const op of ops) {
  8. if (op[0] === "set") map.set(op[1], op[2]);
  9. else if (op[0] === "delete") map.delete(op[1]);
  10. else map.clear();
  11. }
  12. }
  13. const START: ReadonlyArray<readonly [string, number]> = [
  14. ["a", 1],
  15. ["b", 2],
  16. ["c", 3],
  17. ];
  18. // 每组覆盖一种会影响顺序或内容的写入组合。
  19. const CASES: ReadonlyArray<readonly [string, readonly Op[]]> = [
  20. [
  21. "只新增",
  22. [
  23. ["set", "d", 4],
  24. ["set", "e", 5],
  25. ],
  26. ],
  27. [
  28. "改写已有键",
  29. [
  30. ["set", "b", 20],
  31. ["set", "b", 21],
  32. ],
  33. ],
  34. ["删除已有键", [["delete", "a"]]],
  35. [
  36. "删除后重新加入已有键(位置移到末尾)",
  37. [
  38. ["delete", "a"],
  39. ["set", "a", 10],
  40. ],
  41. ],
  42. [
  43. "先改写再删除已有键",
  44. [
  45. ["set", "b", 20],
  46. ["delete", "b"],
  47. ["set", "d", 4],
  48. ],
  49. ],
  50. [
  51. "新增后删除新增键",
  52. [
  53. ["set", "d", 4],
  54. ["delete", "d"],
  55. ["set", "d", 5],
  56. ],
  57. ],
  58. [
  59. "先新增再删除已有键",
  60. [
  61. ["set", "d", 4],
  62. ["set", "c", 30],
  63. ["delete", "a"],
  64. ["set", "e", 5],
  65. ],
  66. ],
  67. [
  68. "删除不存在的键",
  69. [
  70. ["delete", "zz"],
  71. ["set", "d", 4],
  72. ],
  73. ],
  74. ["清空后重建", [["set", "b", 20], ["clear"], ["set", "c", 3], ["set", "a", 1]]],
  75. ];
  76. describe("TransactionalMap", () => {
  77. it.each(CASES)("%s:回滚恢复内容与顺序,提交与副本上的结果相同", (_label, ops) => {
  78. const rolledBack = new TransactionalMap<string, number>(START);
  79. rolledBack.beginTransaction();
  80. apply(rolledBack, ops);
  81. rolledBack.rollbackTransaction();
  82. expect([...rolledBack]).toEqual(START);
  83. const committed = new TransactionalMap<string, number>(START);
  84. committed.beginTransaction();
  85. apply(committed, ops);
  86. committed.commitTransaction();
  87. const copy = new Map<string, number>(START);
  88. apply(copy, ops);
  89. expect([...committed]).toEqual([...copy]);
  90. });
  91. it("事务外的写入不记录;回滚只撤销本次事务", () => {
  92. const map = new TransactionalMap<string, number>(START);
  93. map.set("d", 4);
  94. map.beginTransaction();
  95. map.set("e", 5);
  96. map.rollbackTransaction();
  97. expect([...map]).toEqual([...START, ["d", 4]]);
  98. map.beginTransaction();
  99. map.delete("a");
  100. map.commitTransaction();
  101. map.beginTransaction();
  102. map.set("f", 6);
  103. map.rollbackTransaction();
  104. expect([...map]).toEqual([
  105. ["b", 2],
  106. ["c", 3],
  107. ["d", 4],
  108. ]);
  109. });
  110. it("回滚中途抛错时事务已经关闭,可以重新开启", () => {
  111. // 原子事件结束时逐张回滚;某张表回滚抛错后,它自己也不能残留事务,否则之后每个事件都会失败。
  112. const map = new TransactionalMap<string, number>(START);
  113. map.beginTransaction();
  114. map.set("b", 20);
  115. map.set("d", 4);
  116. const restoreSet = vi.spyOn(Map.prototype, "set").mockImplementationOnce(() => {
  117. throw new RangeError("injected restore failure");
  118. });
  119. let thrown: unknown;
  120. try {
  121. map.rollbackTransaction();
  122. } catch (error) {
  123. thrown = error;
  124. } finally {
  125. restoreSet.mockRestore();
  126. }
  127. expect(thrown).toBeInstanceOf(RangeError);
  128. expect(() => map.beginTransaction()).not.toThrow();
  129. map.rollbackTransaction();
  130. });
  131. it("不能嵌套开启事务", () => {
  132. const map = new TransactionalMap<string, number>();
  133. map.beginTransaction();
  134. expect(() => map.beginTransaction()).toThrow();
  135. });
  136. });