|
| 1 | +import { getTmpPath, updateJSONFile, updateJSONFileBatch } from '../json-write-file' |
| 2 | +import { promises as fs } from 'fs' |
| 3 | + |
| 4 | +const FILE_A = 'file_a.json' |
| 5 | +async function cleanup() { |
| 6 | + await Promise.all([unlinkIfExists(FILE_A), unlinkIfExists(getLockPath(FILE_A)), unlinkIfExists(getTmpPath(FILE_A))]) |
| 7 | +} |
| 8 | + |
| 9 | +beforeEach(cleanup) |
| 10 | +afterEach(cleanup) |
| 11 | + |
| 12 | +test('updateJSONFile: single write', async () => { |
| 13 | + const cbManipulate = jest.fn(() => { |
| 14 | + return { |
| 15 | + a: 1, |
| 16 | + } |
| 17 | + }) |
| 18 | + await updateJSONFile(FILE_A, cbManipulate) |
| 19 | + |
| 20 | + expect(cbManipulate).toBeCalledTimes(1) |
| 21 | + expect(await readIfExists(FILE_A)).toBe( |
| 22 | + JSON.stringify({ |
| 23 | + a: 1, |
| 24 | + }) |
| 25 | + ) |
| 26 | +}) |
| 27 | + |
| 28 | +test('updateJSONFile: 2 writes', async () => { |
| 29 | + const cbManipulate = jest.fn((o) => { |
| 30 | + o = o || [] |
| 31 | + o.push('a') |
| 32 | + return o |
| 33 | + }) |
| 34 | + |
| 35 | + const p0 = updateJSONFile(FILE_A, cbManipulate) |
| 36 | + await sleep(5) |
| 37 | + |
| 38 | + const p1 = updateJSONFile(FILE_A, cbManipulate) |
| 39 | + |
| 40 | + await Promise.all([p0, p1]) |
| 41 | + |
| 42 | + expect(cbManipulate).toBeCalledTimes(2) |
| 43 | + expect(await readIfExists(FILE_A)).toBe(JSON.stringify(['a', 'a'])) |
| 44 | +}) |
| 45 | +test('updateJSONFile: 10 writes', async () => { |
| 46 | + const cbManipulate = jest.fn((o) => { |
| 47 | + o = o || [] |
| 48 | + o.push('b') |
| 49 | + return o |
| 50 | + }) |
| 51 | + |
| 52 | + const config = { |
| 53 | + retryTimeout: 30, |
| 54 | + retryCount: 3, |
| 55 | + } |
| 56 | + |
| 57 | + // This should be an impossible tasks, because there will be too many locks, and not enough time to resolve them: |
| 58 | + |
| 59 | + let error: any |
| 60 | + try { |
| 61 | + await Promise.all([ |
| 62 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 63 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 64 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 65 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 66 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 67 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 68 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 69 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 70 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 71 | + updateJSONFile(FILE_A, cbManipulate, config), |
| 72 | + ]) |
| 73 | + } catch (e) { |
| 74 | + error = e |
| 75 | + } |
| 76 | + expect(error + '').toMatch(/Failed to lock file/) |
| 77 | + |
| 78 | + // Wait for the lock functions to finish retrying: |
| 79 | + await sleep(config.retryTimeout * config.retryCount) |
| 80 | +}) |
| 81 | + |
| 82 | +test('updateJSONFileBatch: single write', async () => { |
| 83 | + const cbManipulate = jest.fn(() => { |
| 84 | + return { |
| 85 | + b: 1, |
| 86 | + } |
| 87 | + }) |
| 88 | + await updateJSONFileBatch(FILE_A, cbManipulate) |
| 89 | + |
| 90 | + expect(cbManipulate).toBeCalledTimes(1) |
| 91 | + expect(await readIfExists(FILE_A)).toBe( |
| 92 | + JSON.stringify({ |
| 93 | + b: 1, |
| 94 | + }) |
| 95 | + ) |
| 96 | +}) |
| 97 | + |
| 98 | +test('updateJSONFileBatch: 3 writes', async () => { |
| 99 | + const v = await readIfExists(FILE_A) |
| 100 | + expect(v).toBe(undefined) |
| 101 | + |
| 102 | + const cbManipulate = jest.fn((o) => { |
| 103 | + o = o || [] |
| 104 | + o.push('a') |
| 105 | + return o |
| 106 | + }) |
| 107 | + |
| 108 | + const p0 = updateJSONFileBatch(FILE_A, cbManipulate) |
| 109 | + await sleep(5) |
| 110 | + |
| 111 | + const p1 = updateJSONFileBatch(FILE_A, cbManipulate) |
| 112 | + const p2 = updateJSONFileBatch(FILE_A, cbManipulate) |
| 113 | + |
| 114 | + await Promise.all([p0, p1, p2]) |
| 115 | + |
| 116 | + expect(cbManipulate).toBeCalledTimes(3) |
| 117 | + expect(await readIfExists(FILE_A)).toBe(JSON.stringify(['a', 'a', 'a'])) |
| 118 | +}) |
| 119 | +test('updateJSONFileBatch: 20 writes', async () => { |
| 120 | + const cbManipulate = jest.fn((o) => { |
| 121 | + o = o || [] |
| 122 | + o.push('a') |
| 123 | + return o |
| 124 | + }) |
| 125 | + |
| 126 | + const config = { |
| 127 | + retryTimeout: 30, |
| 128 | + retryCount: 3, |
| 129 | + } |
| 130 | + |
| 131 | + const ps: Promise<void>[] = [] |
| 132 | + let expectResult: string[] = [] |
| 133 | + for (let i = 0; i < 20; i++) { |
| 134 | + ps.push(updateJSONFileBatch(FILE_A, cbManipulate, config)) |
| 135 | + expectResult.push('a') |
| 136 | + } |
| 137 | + |
| 138 | + await Promise.all(ps) |
| 139 | + |
| 140 | + expect(cbManipulate).toBeCalledTimes(20) |
| 141 | + expect(await readIfExists(FILE_A)).toBe(JSON.stringify(expectResult)) |
| 142 | +}) |
| 143 | + |
| 144 | +async function readIfExists(filePath: string): Promise<string | undefined> { |
| 145 | + try { |
| 146 | + return await fs.readFile(filePath, 'utf-8') |
| 147 | + } catch (e) { |
| 148 | + if ((e as any)?.code === 'ENOENT') { |
| 149 | + // not found |
| 150 | + return undefined |
| 151 | + } else throw e |
| 152 | + } |
| 153 | +} |
| 154 | +async function unlinkIfExists(filePath: string): Promise<void> { |
| 155 | + try { |
| 156 | + await fs.unlink(filePath) |
| 157 | + } catch (e) { |
| 158 | + if ((e as any)?.code === 'ENOENT') { |
| 159 | + // not found, that's okay |
| 160 | + } else throw e |
| 161 | + } |
| 162 | +} |
| 163 | +function getLockPath(filePath: string): string { |
| 164 | + return filePath + '.lock' |
| 165 | +} |
| 166 | +function sleep(duration: number): Promise<void> { |
| 167 | + return new Promise((r) => setTimeout(r, duration)) |
| 168 | +} |
0 commit comments