overleaf/services/document-updater/test/cluster_failover/js/test_blpop_failover.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-05-25 13:27:45 +00:00
let listenInBackground, sendPings
const redis = require('@overleaf/redis-wrapper')
const rclient1 = redis.createClient({
cluster: [
{
port: '7000',
2021-07-13 11:04:42 +00:00
host: 'localhost',
},
],
2021-05-25 13:27:45 +00:00
})
2021-05-25 13:27:45 +00:00
const rclient2 = redis.createClient({
cluster: [
{
port: '7000',
2021-07-13 11:04:42 +00:00
host: 'localhost',
},
],
2021-05-25 13:27:45 +00:00
})
2021-05-25 13:27:45 +00:00
let counter = 0
const sendPing = function (cb) {
if (cb == null) {
cb = function () {}
}
2021-07-13 11:04:42 +00:00
return rclient1.rpush('test-blpop', counter, error => {
2021-05-25 13:27:45 +00:00
if (error != null) {
console.error('[SENDING ERROR]', error.message)
}
if (error == null) {
counter += 1
}
return cb()
})
}
2021-05-25 13:27:45 +00:00
let previous = null
2021-07-13 11:04:42 +00:00
const listenForPing = cb =>
2021-05-25 13:27:45 +00:00
rclient2.blpop('test-blpop', 200, (error, result) => {
if (error != null) {
return cb(error)
}
2021-05-25 13:32:03 +00:00
let [, value] = Array.from(result)
2021-05-25 13:27:45 +00:00
value = parseInt(value, 10)
if (value % 10 === 0) {
console.log('.')
}
if (previous != null && value !== previous + 1) {
error = new Error(
`Counter not in order. Got ${value}, expected ${previous + 1}`
)
}
previous = value
return cb(error, value)
})
2021-05-25 13:27:45 +00:00
const PING_DELAY = 100
;(sendPings = () => sendPing(() => setTimeout(sendPings, PING_DELAY)))()
;(listenInBackground = () =>
2021-07-13 11:04:42 +00:00
listenForPing(error => {
2021-05-25 13:27:45 +00:00
if (error) {
console.error('[RECEIVING ERROR]', error.message)
}
return setTimeout(listenInBackground)
}))()