test_pubsub_failover.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. let sendPings
  2. const redis = require('@overleaf/redis-wrapper')
  3. const rclient1 = redis.createClient({
  4. cluster: [
  5. {
  6. port: '7000',
  7. host: '127.0.0.1',
  8. },
  9. ],
  10. })
  11. const rclient2 = redis.createClient({
  12. cluster: [
  13. {
  14. port: '7000',
  15. host: '127.0.0.1',
  16. },
  17. ],
  18. })
  19. let counter = 0
  20. const sendPing = function (cb) {
  21. if (cb == null) {
  22. cb = function () {}
  23. }
  24. return rclient1.publish('test-pubsub', counter, error => {
  25. if (error) {
  26. console.error('[SENDING ERROR]', error.message)
  27. }
  28. if (error == null) {
  29. counter += 1
  30. }
  31. return cb()
  32. })
  33. }
  34. let previous = null
  35. rclient2.subscribe('test-pubsub')
  36. rclient2.on('message', (channel, value) => {
  37. value = parseInt(value, 10)
  38. if (value % 10 === 0) {
  39. console.log('.')
  40. }
  41. if (previous != null && value !== previous + 1) {
  42. console.error(
  43. '[RECEIVING ERROR]',
  44. `Counter not in order. Got ${value}, expected ${previous + 1}`
  45. )
  46. }
  47. return (previous = value)
  48. })
  49. const PING_DELAY = 100
  50. ;(sendPings = () => sendPing(() => setTimeout(sendPings, PING_DELAY)))()