|
| 1 | +import { randomUUID as v4 } from 'crypto'; |
| 2 | +import fs from 'fs'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 5 | +import { StoreName } from '../../../src/datastore/DataStore'; |
| 6 | +import { LMDBStoreFactory } from '../../../src/datastore/LMDBStoreFactory'; |
| 7 | + |
| 8 | +describe('LMDB retry after recovery', () => { |
| 9 | + let testDir: string; |
| 10 | + let factory: LMDBStoreFactory; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + testDir = join(process.cwd(), 'node_modules', '.cache', 'lmdb-retry-test', v4()); |
| 14 | + fs.mkdirSync(testDir, { recursive: true }); |
| 15 | + factory = new LMDBStoreFactory(testDir); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(async () => { |
| 19 | + await factory.close(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('sync operations retry on transient failure', () => { |
| 23 | + it('should return data after transient get() failure triggers recovery and retry', async () => { |
| 24 | + const store = factory.get(StoreName.public_schemas); |
| 25 | + await store.put('key', 'value'); |
| 26 | + |
| 27 | + const lmdbStore = store as any; |
| 28 | + const realStore = lmdbStore.store; |
| 29 | + |
| 30 | + realStore.get = () => { |
| 31 | + throw new Error('MDB_CORRUPTED: Located page was wrong type'); |
| 32 | + }; |
| 33 | + |
| 34 | + // Recovery replaces the store handle, retry succeeds on the new handle |
| 35 | + const result = store.get<string>('key'); |
| 36 | + expect(result).toBe('value'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should throw if recovery fails and retry also fails', () => { |
| 40 | + const store = factory.get(StoreName.public_schemas); |
| 41 | + const lmdbStore = store as any; |
| 42 | + |
| 43 | + // Mock handleError to not actually recover |
| 44 | + const originalHandleError = (factory as any).handleError.bind(factory); |
| 45 | + (factory as any).handleError = () => { |
| 46 | + /* no-op: simulates recovery failure */ |
| 47 | + }; |
| 48 | + |
| 49 | + const realStore = lmdbStore.store; |
| 50 | + realStore.get = () => { |
| 51 | + throw new Error('MDB_CORRUPTED: permanent'); |
| 52 | + }; |
| 53 | + |
| 54 | + expect(() => store.get<string>('key')).toThrow('MDB_CORRUPTED: permanent'); |
| 55 | + (factory as any).handleError = originalHandleError; |
| 56 | + }); |
| 57 | + |
| 58 | + it('should retry keys() after transient failure', async () => { |
| 59 | + const store = factory.get(StoreName.public_schemas); |
| 60 | + await store.put('k1', 'v1'); |
| 61 | + |
| 62 | + const lmdbStore = store as any; |
| 63 | + const realStore = lmdbStore.store; |
| 64 | + const originalGetKeys = realStore.getKeys.bind(realStore); |
| 65 | + let callCount = 0; |
| 66 | + |
| 67 | + realStore.getKeys = (opts: any) => { |
| 68 | + callCount++; |
| 69 | + if (callCount === 1) throw new Error('MDB_BAD_TXN: Transaction must abort'); |
| 70 | + return originalGetKeys(opts); |
| 71 | + }; |
| 72 | + |
| 73 | + const keys = store.keys(10); |
| 74 | + expect(keys).toContain('k1'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('async operations retry on transient failure', () => { |
| 79 | + it('should succeed after transient put() failure', async () => { |
| 80 | + const store = factory.get(StoreName.public_schemas); |
| 81 | + const lmdbStore = store as any; |
| 82 | + const realStore = lmdbStore.store; |
| 83 | + |
| 84 | + realStore.put = () => { |
| 85 | + throw new Error('MDB_PAGE_NOTFOUND: Requested page not found'); |
| 86 | + }; |
| 87 | + |
| 88 | + await store.put('key', 'value'); |
| 89 | + expect(store.get('key')).toBe('value'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should throw on async if retry also fails', async () => { |
| 93 | + const store = factory.get(StoreName.public_schemas); |
| 94 | + const lmdbStore = store as any; |
| 95 | + |
| 96 | + (factory as any).handleError = () => { |
| 97 | + /* no-op */ |
| 98 | + }; |
| 99 | + |
| 100 | + const realStore = lmdbStore.store; |
| 101 | + realStore.put = () => { |
| 102 | + throw new Error('MDB_PANIC: unrecoverable'); |
| 103 | + }; |
| 104 | + |
| 105 | + await expect(store.put('key', 'value')).rejects.toThrow('MDB_PANIC: unrecoverable'); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('recovery is called exactly once per failure', () => { |
| 110 | + it('should call handleError once on transient failure', async () => { |
| 111 | + const store = factory.get(StoreName.public_schemas); |
| 112 | + await store.put('key', 'value'); |
| 113 | + |
| 114 | + const handleErrorSpy = vi.spyOn(factory as any, 'handleError'); |
| 115 | + const lmdbStore = store as any; |
| 116 | + const realStore = lmdbStore.store; |
| 117 | + |
| 118 | + realStore.get = () => { |
| 119 | + throw new Error('MDB_CORRUPTED: test'); |
| 120 | + }; |
| 121 | + |
| 122 | + store.get<string>('key'); |
| 123 | + expect(handleErrorSpy).toHaveBeenCalledTimes(1); |
| 124 | + handleErrorSpy.mockRestore(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should work normally after a successful retry', async () => { |
| 128 | + const store = factory.get(StoreName.public_schemas); |
| 129 | + await store.put('key1', 'value1'); |
| 130 | + |
| 131 | + const lmdbStore = store as any; |
| 132 | + const realStore = lmdbStore.store; |
| 133 | + |
| 134 | + realStore.get = () => { |
| 135 | + throw new Error('MDB_BAD_TXN: abort'); |
| 136 | + }; |
| 137 | + |
| 138 | + expect(store.get<string>('key1')).toBe('value1'); |
| 139 | + await store.put('key2', 'value2'); |
| 140 | + expect(store.get<string>('key2')).toBe('value2'); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments