overleaf/services/document-updater/test/cluster_failover/coffee/test_pubsub_failover.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

/* eslint-disable
no-return-assign,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2021-05-25 09:27:45 -04:00
let sendPings
const redis = require('@overleaf/redis-wrapper')
const rclient1 = redis.createClient({
cluster: [
{
port: '7000',
host: 'localhost'
}
]
})
2021-05-25 09:27:45 -04:00
const rclient2 = redis.createClient({
cluster: [
{
port: '7000',
host: 'localhost'
}
]
})
2021-05-25 09:27:45 -04:00
let counter = 0
const sendPing = function (cb) {
if (cb == null) {
cb = function () {}
}
return rclient1.publish('test-pubsub', counter, (error) => {
if (error != null) {
console.error('[SENDING ERROR]', error.message)
}
if (error == null) {
counter += 1
}
return cb()
})
}
2021-05-25 09:27:45 -04:00
let previous = null
rclient2.subscribe('test-pubsub')
rclient2.on('message', (channel, value) => {
value = parseInt(value, 10)
if (value % 10 === 0) {
console.log('.')
}
if (previous != null && value !== previous + 1) {
console.error(
'[RECEIVING ERROR]',
`Counter not in order. Got ${value}, expected ${previous + 1}`
)
}
return (previous = value)
})
2021-05-25 09:27:45 -04:00
const PING_DELAY = 100
;(sendPings = () => sendPing(() => setTimeout(sendPings, PING_DELAY)))()