|
1 | | -import { readdirSync, mkdirSync, writeFileSync } from "fs-extra"; |
2 | 1 | import { join } from "path"; |
3 | 2 | import * as vscode from "vscode"; |
4 | 3 |
|
5 | 4 | import { extLogger } from "../../../../src/common"; |
6 | | -import { registerQueryHistoryScrubber } from "../../../../src/query-history/query-history-scrubber"; |
7 | 5 | import { QueryHistoryManager } from "../../../../src/query-history/query-history-manager"; |
8 | 6 | import { |
9 | 7 | QueryHistoryConfig, |
10 | 8 | QueryHistoryConfigListener, |
11 | 9 | } from "../../../../src/config"; |
12 | 10 | import { LocalQueryInfo } from "../../../../src/query-results"; |
13 | 11 | import { DatabaseManager } from "../../../../src/databases"; |
14 | | -import { dirSync } from "tmp-promise"; |
15 | | -import { |
16 | | - ONE_DAY_IN_MS, |
17 | | - ONE_HOUR_IN_MS, |
18 | | - THREE_HOURS_IN_MS, |
19 | | - TWO_HOURS_IN_MS, |
20 | | -} from "../../../../src/pure/time"; |
21 | 12 | import { tmpDir } from "../../../../src/helpers"; |
22 | 13 | import { HistoryItemLabelProvider } from "../../../../src/query-history/history-item-label-provider"; |
23 | 14 | import { RemoteQueriesManager } from "../../../../src/remote-queries/remote-queries-manager"; |
@@ -1468,189 +1459,6 @@ describe("QueryHistoryManager", () => { |
1468 | 1459 | }); |
1469 | 1460 | }); |
1470 | 1461 |
|
1471 | | - describe("query history scrubber", () => { |
1472 | | - const now = Date.now(); |
1473 | | - |
1474 | | - let deregister: vscode.Disposable | undefined; |
1475 | | - let mockCtx: vscode.ExtensionContext; |
1476 | | - let runCount = 0; |
1477 | | - |
1478 | | - // We don't want our times to align exactly with the hour, |
1479 | | - // so we can better mimic real life |
1480 | | - const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000; |
1481 | | - const tmpDir = dirSync({ |
1482 | | - unsafeCleanup: true, |
1483 | | - }); |
1484 | | - |
1485 | | - beforeEach(() => { |
1486 | | - jest.useFakeTimers({ |
1487 | | - doNotFake: ["setTimeout"], |
1488 | | - now, |
1489 | | - }); |
1490 | | - |
1491 | | - mockCtx = { |
1492 | | - globalState: { |
1493 | | - lastScrubTime: now, |
1494 | | - get(key: string) { |
1495 | | - if (key !== "lastScrubTime") { |
1496 | | - throw new Error(`Unexpected key: ${key}`); |
1497 | | - } |
1498 | | - return this.lastScrubTime; |
1499 | | - }, |
1500 | | - async update(key: string, value: any) { |
1501 | | - if (key !== "lastScrubTime") { |
1502 | | - throw new Error(`Unexpected key: ${key}`); |
1503 | | - } |
1504 | | - this.lastScrubTime = value; |
1505 | | - }, |
1506 | | - }, |
1507 | | - } as any as vscode.ExtensionContext; |
1508 | | - }); |
1509 | | - |
1510 | | - afterEach(() => { |
1511 | | - if (deregister) { |
1512 | | - deregister.dispose(); |
1513 | | - deregister = undefined; |
1514 | | - } |
1515 | | - }); |
1516 | | - |
1517 | | - it("should not throw an error when the query directory does not exist", async () => { |
1518 | | - registerScrubber("idontexist"); |
1519 | | - |
1520 | | - jest.advanceTimersByTime(ONE_HOUR_IN_MS); |
1521 | | - await wait(); |
1522 | | - // "Should not have called the scrubber" |
1523 | | - expect(runCount).toBe(0); |
1524 | | - |
1525 | | - jest.advanceTimersByTime(ONE_HOUR_IN_MS - 1); |
1526 | | - await wait(); |
1527 | | - // "Should not have called the scrubber" |
1528 | | - expect(runCount).toBe(0); |
1529 | | - |
1530 | | - jest.advanceTimersByTime(1); |
1531 | | - await wait(); |
1532 | | - // "Should have called the scrubber once" |
1533 | | - expect(runCount).toBe(1); |
1534 | | - |
1535 | | - jest.advanceTimersByTime(TWO_HOURS_IN_MS); |
1536 | | - await wait(); |
1537 | | - // "Should have called the scrubber a second time" |
1538 | | - expect(runCount).toBe(2); |
1539 | | - |
1540 | | - expect((mockCtx.globalState as any).lastScrubTime).toBe( |
1541 | | - now + TWO_HOURS_IN_MS * 2, |
1542 | | - ); |
1543 | | - }); |
1544 | | - |
1545 | | - it("should scrub directories", async () => { |
1546 | | - // create two query directories that are right around the cut off time |
1547 | | - const queryDir = createMockQueryDir( |
1548 | | - ONE_HOUR_IN_MS, |
1549 | | - TWO_HOURS_IN_MS, |
1550 | | - THREE_HOURS_IN_MS, |
1551 | | - ); |
1552 | | - registerScrubber(queryDir); |
1553 | | - |
1554 | | - jest.advanceTimersByTime(TWO_HOURS_IN_MS); |
1555 | | - await wait(); |
1556 | | - |
1557 | | - // should have deleted only the invalid locations |
1558 | | - expectDirectories( |
1559 | | - queryDir, |
1560 | | - toQueryDirName(ONE_HOUR_IN_MS), |
1561 | | - toQueryDirName(TWO_HOURS_IN_MS), |
1562 | | - toQueryDirName(THREE_HOURS_IN_MS), |
1563 | | - ); |
1564 | | - |
1565 | | - jest.advanceTimersByTime(LESS_THAN_ONE_DAY); |
1566 | | - await wait(); |
1567 | | - |
1568 | | - // nothing should have happened...yet |
1569 | | - expectDirectories( |
1570 | | - queryDir, |
1571 | | - toQueryDirName(ONE_HOUR_IN_MS), |
1572 | | - toQueryDirName(TWO_HOURS_IN_MS), |
1573 | | - toQueryDirName(THREE_HOURS_IN_MS), |
1574 | | - ); |
1575 | | - |
1576 | | - jest.advanceTimersByTime(1000); |
1577 | | - await wait(); |
1578 | | - |
1579 | | - // should have deleted the two older directories |
1580 | | - // even though they have different time stamps, |
1581 | | - // they both expire during the same scrubbing period |
1582 | | - expectDirectories(queryDir, toQueryDirName(THREE_HOURS_IN_MS)); |
1583 | | - |
1584 | | - // Wait until the next scrub time and the final directory is deleted |
1585 | | - jest.advanceTimersByTime(TWO_HOURS_IN_MS); |
1586 | | - await wait(); |
1587 | | - |
1588 | | - // should have deleted everything |
1589 | | - expectDirectories(queryDir); |
1590 | | - }); |
1591 | | - |
1592 | | - function expectDirectories(queryDir: string, ...dirNames: string[]) { |
1593 | | - const files = readdirSync(queryDir); |
1594 | | - expect(files.sort()).toEqual(dirNames.sort()); |
1595 | | - } |
1596 | | - |
1597 | | - function createMockQueryDir(...timestamps: number[]) { |
1598 | | - const dir = tmpDir.name; |
1599 | | - const queryDir = join(dir, "query"); |
1600 | | - // create qyuery directory and fill it with some query directories |
1601 | | - mkdirSync(queryDir); |
1602 | | - |
1603 | | - // create an invalid file |
1604 | | - const invalidFile = join(queryDir, "invalid.txt"); |
1605 | | - writeFileSync(invalidFile, "invalid"); |
1606 | | - |
1607 | | - // create a directory without a timestamp file |
1608 | | - const noTimestampDir = join(queryDir, "noTimestampDir"); |
1609 | | - mkdirSync(noTimestampDir); |
1610 | | - writeFileSync(join(noTimestampDir, "invalid.txt"), "invalid"); |
1611 | | - |
1612 | | - // create a directory with a timestamp file, but is invalid |
1613 | | - const invalidTimestampDir = join(queryDir, "invalidTimestampDir"); |
1614 | | - mkdirSync(invalidTimestampDir); |
1615 | | - writeFileSync(join(invalidTimestampDir, "timestamp"), "invalid"); |
1616 | | - |
1617 | | - // create a directories with a valid timestamp files from the args |
1618 | | - timestamps.forEach((timestamp) => { |
1619 | | - const dir = join(queryDir, toQueryDirName(timestamp)); |
1620 | | - mkdirSync(dir); |
1621 | | - writeFileSync(join(dir, "timestamp"), `${now + timestamp}`); |
1622 | | - }); |
1623 | | - |
1624 | | - return queryDir; |
1625 | | - } |
1626 | | - |
1627 | | - function toQueryDirName(timestamp: number) { |
1628 | | - return `query-${timestamp}`; |
1629 | | - } |
1630 | | - |
1631 | | - function registerScrubber(dir: string) { |
1632 | | - deregister = registerQueryHistoryScrubber( |
1633 | | - ONE_HOUR_IN_MS, |
1634 | | - TWO_HOURS_IN_MS, |
1635 | | - LESS_THAN_ONE_DAY, |
1636 | | - dir, |
1637 | | - { |
1638 | | - removeDeletedQueries: () => { |
1639 | | - return Promise.resolve(); |
1640 | | - }, |
1641 | | - } as QueryHistoryManager, |
1642 | | - mockCtx, |
1643 | | - { |
1644 | | - increment: () => runCount++, |
1645 | | - }, |
1646 | | - ); |
1647 | | - } |
1648 | | - |
1649 | | - async function wait(ms = 500) { |
1650 | | - return new Promise((resolve) => setTimeout(resolve, ms)); |
1651 | | - } |
1652 | | - }); |
1653 | | - |
1654 | 1462 | async function createMockQueryHistory( |
1655 | 1463 | allHistory: QueryHistoryInfo[], |
1656 | 1464 | credentials?: Credentials, |
|
0 commit comments