Merge pull request #19162 from overleaf/jpa-flaky-admin-test

[web] refresh system message cache after creating/deleting message

GitOrigin-RevId: f7155e94a8bf08b527e7ae69d9d6a41e54c01282
This commit is contained in:
Jimmy Domagala-Tang 2024-06-26 09:37:22 -04:00 committed by Copybot
parent 2e1dcee22b
commit c04fea0fd3
2 changed files with 14 additions and 12 deletions

View file

@ -52,8 +52,6 @@ describe('admin panel', function () {
cy.get('button').contains('Post Message').click()
cy.findByText(message)
cy.log('wait for system message propagation')
cy.wait(13000)
const resumeUser1Session = login(user1)
cy.visit('/project')
cy.findByText(message)

View file

@ -6,6 +6,8 @@ const { callbackifyAll } = require('@overleaf/promise-utils')
const logger = require('@overleaf/logger')
const SystemMessageManager = {
_cachedMessages: [],
getMessages() {
return this._cachedMessages
},
@ -16,28 +18,30 @@ const SystemMessageManager = {
async clearMessages() {
await SystemMessage.deleteMany({}).exec()
await this.refreshCache()
},
async createMessage(content) {
const message = new SystemMessage({ content })
await message.save()
await this.refreshCache()
},
refreshCache() {
this.getMessagesFromDB()
.then(messages => {
this._cachedMessages = messages
})
.catch(err => {
logger.warn({ err }, 'failed to refresh system messages cache')
})
async refreshCache() {
this._cachedMessages = await this.getMessagesFromDB()
},
refreshCacheInBackground() {
this.refreshCache().catch(err => {
logger.warn({ err }, 'failed to refresh system messages cache')
})
},
}
const CACHE_TIMEOUT = 10 * 1000 * (Math.random() + 2) // 20-30 seconds
SystemMessageManager.refreshCache()
SystemMessageManager.refreshCacheInBackground()
const intervalHandle = setInterval(
() => SystemMessageManager.refreshCache(),
() => SystemMessageManager.refreshCacheInBackground(),
CACHE_TIMEOUT
)