Merge pull request #31650 from overleaf/bg-handle-timeouts-in-persistence-manager

add missing handling of timeouts in PersistenceManager

GitOrigin-RevId: 7fe74068f3ea647b27103393c3f0b243b4b25fe3
This commit is contained in:
Brian Gough
2026-02-20 09:05:50 +00:00
committed by Copybot
parent fd4e5c938f
commit 15a24db3c9
3 changed files with 131 additions and 5 deletions
@@ -916,4 +916,89 @@ describe('Setting a document', function () {
expect(MockWebApi.setDocumentController).to.be.calledTwice
})
})
describe('when the web api http request times out on the first request', function () {
beforeEach(async function () {
this.project_id = DocUpdaterClient.randomId()
this.doc_id = DocUpdaterClient.randomId()
MockWebApi.insertDoc(this.project_id, this.doc_id, {
lines: this.lines,
version: this.version,
projectHistoryId: this.project_id,
})
await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
const origSetDocumentController =
MockWebApi.setDocumentController.bind(MockWebApi)
const setDocumentStub = sinon
.stub(MockWebApi, 'setDocumentController')
.onFirstCall()
.callsFake(async (req, res, next) => {
await setTimeout(30_000)
})
setDocumentStub.onCall(1).callsFake(origSetDocumentController)
})
afterEach(function () {
MockWebApi.setDocumentController.restore()
})
it('should retry the request and return the document', async function () {
this.timeout(10000)
const returnedDoc = await DocUpdaterClient.setDocLines(
this.project_id,
this.doc_id,
this.newLines,
this.source,
this.user_id,
false
)
expect(returnedDoc).to.deep.include({ rev: '123' })
expect(MockWebApi.setDocumentController).to.be.calledTwice
})
})
describe('when the web api http request times out repeatedly', function () {
beforeEach(async function () {
this.project_id = DocUpdaterClient.randomId()
this.doc_id = DocUpdaterClient.randomId()
MockWebApi.insertDoc(this.project_id, this.doc_id, {
lines: this.lines,
version: this.version,
projectHistoryId: this.project_id,
})
await DocUpdaterClient.preloadDoc(this.project_id, this.doc_id)
sinon
.stub(MockWebApi, 'setDocumentController')
.callsFake(async (req, res, next) => {
await setTimeout(30_000)
})
})
afterEach(function () {
MockWebApi.setDocumentController.restore()
})
it('should return an error after two attempts', async function () {
this.timeout(15000)
const start = Date.now()
await expect(
DocUpdaterClient.setDocLines(
this.project_id,
this.doc_id,
this.newLines,
this.source,
this.user_id,
false
)
).to.be.rejectedWith('request failed')
const delta = Date.now() - start
expect(delta).to.be.above(10_000) // 2 * 5000ms timeout
expect(delta).to.be.below(20_000)
expect(MockWebApi.setDocumentController).to.be.calledTwice
})
})
})