oauth-polling-clock-faults.cjs 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. // 只改变新测试 Host 的系统时间;performance.now 和真实定时器保持不变。
  2. const { appendFile } = require("node:fs/promises");
  3. const { join } = require("node:path");
  4. const { pathToFileURL } = require("node:url");
  5. const originalNow = Date.now;
  6. const originalFetch = globalThis.fetch;
  7. const origin = process.env.ZCODE_E2E_OAUTH_RACE_URL;
  8. const caseId = process.env.ZCODE_E2E_OAUTH_CLOCK_CASE;
  9. let offset = 0;
  10. Date.now = () => originalNow() + offset;
  11. globalThis.fetch = async (input, init) => {
  12. const response = await originalFetch(input, init);
  13. const url = new URL(input instanceof Request ? input.url : String(input));
  14. if (url.origin === origin && url.pathname === "/api/v1/oauth/cli/poll/e2e-race") {
  15. const value = response.headers.get("x-e2e-clock-offset-ms");
  16. if (value !== null) {
  17. offset = Number(value);
  18. await appendFile(
  19. join(process.env.ZCODE_E2E_HOST_FAULT_ROOT, `host-boundary-${process.pid}.jsonl`),
  20. JSON.stringify({ pid: process.pid, id: caseId, kind: "clock-shift", offset }) + "\n",
  21. );
  22. }
  23. }
  24. return response;
  25. };
  26. async function start() {
  27. await appendFile(
  28. join(process.env.ZCODE_E2E_HOST_FAULT_ROOT, `host-boundary-${process.pid}.jsonl`),
  29. JSON.stringify({ pid: process.pid, id: caseId, kind: "ready" }) + "\n",
  30. );
  31. await import(pathToFileURL(process.argv[2]).href);
  32. }
  33. void start();