-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow disabling storage persistence (#1539)
- Loading branch information
1 parent
e3cb509
commit f65e3c6
Showing
13 changed files
with
128 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { access } from 'node:fs/promises'; | ||
import { setTimeout } from 'node:timers/promises'; | ||
|
||
export async function waitTillWrittenToDisk(path: string): Promise<void> { | ||
try { | ||
await access(path); | ||
} catch { | ||
await setTimeout(50); | ||
return waitTillWrittenToDisk(path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { readdir, rm } from 'node:fs/promises'; | ||
import { resolve } from 'node:path'; | ||
import { MemoryStorage } from '@crawlee/memory-storage'; | ||
import { waitTillWrittenToDisk } from './__shared__'; | ||
|
||
describe('persistStorage option', () => { | ||
const tmpLocation = resolve(__dirname, './tmp/no-writing-to-disk'); | ||
|
||
afterAll(async () => { | ||
await rm(tmpLocation, { force: true, recursive: true }); | ||
}); | ||
|
||
describe('when false and writeMetadata is also false', () => { | ||
const localDataDirectory = resolve(tmpLocation, './no-metadata'); | ||
const storage = new MemoryStorage({ | ||
localDataDirectory, | ||
persistStorage: false, | ||
}); | ||
|
||
test('creating a key-value pair in a key-value store should not write data to the disk', async () => { | ||
const keyValueStoreInfo = await storage.keyValueStores().getOrCreate(); | ||
|
||
const keyValueStore = storage.keyValueStore(keyValueStoreInfo.id); | ||
await keyValueStore.setRecord({ key: 'foo', value: 'test' }); | ||
|
||
// We check that reading the directory for the store throws an error, which means it wasn't created on disk | ||
await expect(() => readdir(localDataDirectory)).rejects.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('when false and writeMetadata is true', () => { | ||
const localDataDirectory = resolve(tmpLocation, './with-metadata'); | ||
const storage = new MemoryStorage({ | ||
localDataDirectory, | ||
persistStorage: false, | ||
writeMetadata: true, | ||
}); | ||
|
||
test('creating a key-value pair in a key-value store should not write data to the disk, but it should write the __metadata__ file', async () => { | ||
const keyValueStoreInfo = await storage.keyValueStores().getOrCreate(); | ||
|
||
const keyValueStore = storage.keyValueStore(keyValueStoreInfo.id); | ||
await keyValueStore.setRecord({ key: 'foo', value: 'test' }); | ||
|
||
const storePath = resolve(storage.keyValueStoresDirectory, `${keyValueStoreInfo.id}`); | ||
|
||
await waitTillWrittenToDisk(storePath); | ||
|
||
const directoryFiles = await readdir(storePath); | ||
|
||
expect(directoryFiles).toHaveLength(1); | ||
expect(directoryFiles).toEqual([ | ||
'__metadata__.json', | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters