Merge pull request #31444 from overleaf/bg-jpa-use-fetch-in-persistence-manager

remove requestretry from PersistenceManager

GitOrigin-RevId: 1fc9ffdfa7879d7ab4f0f4683544d09fe8526f3d
This commit is contained in:
Brian Gough
2026-02-19 09:05:45 +00:00
committed by Copybot
parent 4603722288
commit a9c94c4184
12 changed files with 669 additions and 799 deletions
@@ -5,6 +5,7 @@ const MockWebApi = require('./helpers/MockWebApi')
const DocUpdaterClient = require('./helpers/DocUpdaterClient')
const DocUpdaterApp = require('./helpers/DocUpdaterApp')
const { RequestFailedError } = require('@overleaf/fetch-utils')
const PersistenceManager = require('../../../app/js/PersistenceManager')
describe('Getting a document', function () {
before(async function () {
@@ -49,6 +50,53 @@ describe('Getting a document', function () {
})
})
describe('when the document is not loaded and the peek option is used', function () {
before(async function () {
const origGetDocumentController =
MockWebApi.getDocumentController.bind(MockWebApi)
sinon
.stub(MockWebApi, 'getDocumentController')
.callsFake((req, res, next) => {
expect(req.query.peek).to.equal('true')
return origGetDocumentController(req, res, next)
})
this.project_id = DocUpdaterClient.randomId()
this.doc_id = DocUpdaterClient.randomId()
sinon.spy(MockWebApi, 'getDocument')
MockWebApi.insertDoc(this.project_id, this.doc_id, {
lines: this.lines,
version: this.version,
})
// This is only used by the resync code and not exposed on the HTTP
// api so we are calling it directly.
this.returnedDoc = await PersistenceManager.promises.getDoc(
this.project_id,
this.doc_id,
{ peek: true }
)
})
after(function () {
MockWebApi.getDocumentController.restore()
MockWebApi.getDocument.restore()
})
it('should load the document from the web API with peek=true', function () {
MockWebApi.getDocument
.calledWith(this.project_id, this.doc_id)
.should.equal(true)
})
it('should return the document lines', function () {
this.returnedDoc.lines.should.deep.equal(this.lines)
})
it('should return the document at its current version', function () {
this.returnedDoc.version.should.equal(this.version)
})
})
describe('when the document is already loaded', function () {
before(async function () {
this.project_id = DocUpdaterClient.randomId()
@@ -146,11 +194,11 @@ describe('Getting a document', function () {
})
describe('when the web api returns an error', function () {
before(function () {
beforeEach(function () {
sinon.stub(MockWebApi, 'getDocument').rejects(new Error('oops'))
})
after(function () {
afterEach(function () {
MockWebApi.getDocument.restore()
})
@@ -161,6 +209,133 @@ describe('Getting a document', function () {
.to.be.rejectedWith(RequestFailedError)
.and.eventually.have.nested.property('response.status', 500)
})
it('should retry the request', async function () {
const projectId = DocUpdaterClient.randomId()
const docId = DocUpdaterClient.randomId()
await expect(DocUpdaterClient.getDoc(projectId, docId))
.to.be.rejectedWith(RequestFailedError)
.and.eventually.have.nested.property('response.status', 500)
expect(MockWebApi.getDocument).to.be.calledTwice
})
})
describe('when the web api returns a retryable error on the first attempt', function () {
beforeEach(function () {
const origGetDocumentController =
MockWebApi.getDocumentController.bind(MockWebApi)
const getDocumentStub = sinon
.stub(MockWebApi, 'getDocumentController')
.onCall(0)
.callsFake((req, res, next) => {
res.destroy() // simulate a network error
})
getDocumentStub.onCall(1).callsFake(origGetDocumentController)
})
afterEach(function () {
MockWebApi.getDocumentController.restore()
})
it('should return 200', async function () {
const projectId = DocUpdaterClient.randomId()
const docId = DocUpdaterClient.randomId()
MockWebApi.insertDoc(projectId, docId, {
lines: this.lines,
version: this.version,
})
await expect(
DocUpdaterClient.getDoc(projectId, docId)
).to.eventually.deep.include({ lines: this.lines, version: this.version })
})
it('should retry the request', async function () {
const projectId = DocUpdaterClient.randomId()
const docId = DocUpdaterClient.randomId()
MockWebApi.insertDoc(projectId, docId, {
lines: this.lines,
version: this.version,
})
await expect(
DocUpdaterClient.getDoc(projectId, docId)
).to.eventually.deep.include({ lines: this.lines, version: this.version })
expect(MockWebApi.getDocumentController).to.be.calledTwice
})
})
describe('when the web api returns a 413 error', function () {
beforeEach(function () {
sinon
.stub(MockWebApi, 'getDocumentController')
.callsFake((req, res, next) => {
res.sendStatus(413)
})
})
afterEach(function () {
MockWebApi.getDocumentController.restore()
})
it('should return 413', async function () {
const projectId = DocUpdaterClient.randomId()
const docId = DocUpdaterClient.randomId()
await expect(DocUpdaterClient.getDoc(projectId, docId))
.to.be.rejectedWith(RequestFailedError)
.and.eventually.have.nested.property('response.status', 413)
})
it('should not retry the request', async function () {
const projectId = DocUpdaterClient.randomId()
const docId = DocUpdaterClient.randomId()
await expect(DocUpdaterClient.getDoc(projectId, docId))
.to.be.rejectedWith(RequestFailedError)
.and.eventually.have.nested.property('response.status', 413)
expect(MockWebApi.getDocumentController).to.be.calledOnce
})
})
describe('when the web api returns an incomplete doc', function () {
afterEach(function () {
MockWebApi.getDocument.restore()
})
it('should return an error for missing lines', async function () {
const projectId = DocUpdaterClient.randomId()
const docId = DocUpdaterClient.randomId()
sinon
.stub(MockWebApi, 'getDocument')
.resolves({ version: 123, pathname: 'test' })
await expect(DocUpdaterClient.getDoc(projectId, docId))
.to.be.rejectedWith(RequestFailedError)
.and.eventually.have.nested.property('response.status', 422)
})
it('should return an error for missing version', async function () {
const projectId = DocUpdaterClient.randomId()
const docId = DocUpdaterClient.randomId()
sinon
.stub(MockWebApi, 'getDocument')
.resolves({ lines: [''], pathname: 'test' })
await expect(DocUpdaterClient.getDoc(projectId, docId))
.to.be.rejectedWith(RequestFailedError)
.and.eventually.have.nested.property('response.status', 422)
})
it('should return an error for missing pathname', async function () {
const projectId = DocUpdaterClient.randomId()
const docId = DocUpdaterClient.randomId()
sinon
.stub(MockWebApi, 'getDocument')
.resolves({ lines: [''], version: 123 })
await expect(DocUpdaterClient.getDoc(projectId, docId))
.to.be.rejectedWith(RequestFailedError)
.and.eventually.have.nested.property('response.status', 422)
})
})
describe('when the web api http request takes a long time', function () {