Test concurrency

This commit is contained in:
Adria Navarro 2024-02-29 15:51:42 +01:00
parent 35536592e6
commit 41dde9722f
2 changed files with 47 additions and 6 deletions

View File

@ -19,9 +19,9 @@ interface CacheItem {
} }
export class DocWritethrough { export class DocWritethrough {
db: Database private db: Database
docId: string private _docId: string
writeRateMs: number private writeRateMs: number
constructor( constructor(
db: Database, db: Database,
@ -29,10 +29,14 @@ export class DocWritethrough {
writeRateMs: number = DEFAULT_WRITE_RATE_MS writeRateMs: number = DEFAULT_WRITE_RATE_MS
) { ) {
this.db = db this.db = db
this.docId = docId this._docId = docId
this.writeRateMs = writeRateMs this.writeRateMs = writeRateMs
} }
get docId() {
return this._docId
}
private makeCacheItem(): CacheItem { private makeCacheItem(): CacheItem {
return { lastWrite: Date.now() } return { lastWrite: Date.now() }
} }

View File

@ -41,8 +41,9 @@ describe("docWritethrough", () => {
docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS) docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS)
}) })
it("patching will not persist if timeout does not hit", async () => { it("patching will not persist if timeout from the creation does not hit", async () => {
await config.doInTenant(async () => { await config.doInTenant(async () => {
travelForward(WRITE_RATE_MS)
await docWritethrough.patch(generatePatchObject(2)) await docWritethrough.patch(generatePatchObject(2))
await docWritethrough.patch(generatePatchObject(2)) await docWritethrough.patch(generatePatchObject(2))
travelForward(WRITE_RATE_MS - 1) travelForward(WRITE_RATE_MS - 1)
@ -116,7 +117,7 @@ describe("docWritethrough", () => {
await config.doInTenant(async () => { await config.doInTenant(async () => {
const patch1 = generatePatchObject(2) const patch1 = generatePatchObject(2)
await docWritethrough.patch(patch1) await docWritethrough.patch(patch1)
const time1 = travelForward(WRITE_RATE_MS) travelForward(WRITE_RATE_MS)
const patch2 = generatePatchObject(1) const patch2 = generatePatchObject(1)
await docWritethrough.patch(patch2) await docWritethrough.patch(patch2)
@ -144,5 +145,41 @@ describe("docWritethrough", () => {
) )
}) })
}) })
it("concurrent patches to multiple DocWritethrough will not contaminate each other", async () => {
await config.doInTenant(async () => {
const secondDocWritethrough = new DocWritethrough(
db,
structures.db.id(),
WRITE_RATE_MS
)
const doc1Patch = generatePatchObject(2)
await docWritethrough.patch(doc1Patch)
const doc2Patch = generatePatchObject(1)
await secondDocWritethrough.patch(doc2Patch)
travelForward(WRITE_RATE_MS)
const doc1Patch2 = generatePatchObject(3)
await docWritethrough.patch(doc1Patch2)
const doc2Patch2 = generatePatchObject(3)
await secondDocWritethrough.patch(doc2Patch2)
expect(await db.get(docWritethrough.docId)).toEqual(
expect.objectContaining({
...doc1Patch,
...doc1Patch2,
})
)
expect(await db.get(secondDocWritethrough.docId)).toEqual(
expect.objectContaining({
...doc2Patch,
...doc2Patch2,
})
)
})
})
}) })
}) })