rollout.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const config = require('config')
  2. const sinon = require('sinon')
  3. const { expect } = require('chai')
  4. const cleanup = require('../storage/support/cleanup')
  5. const Rollout = require('../../../../api/app/rollout')
  6. describe('rollout', function () {
  7. beforeEach(cleanup.everything)
  8. beforeEach('Set up stubs', function () {
  9. sinon.stub(config, 'has').callThrough()
  10. sinon.stub(config, 'get').callThrough()
  11. })
  12. afterEach(sinon.restore)
  13. it('should return a valid history buffer level', function () {
  14. setMockConfig('historyBufferLevel', '2')
  15. setMockConfig('forcePersistBuffer', 'false')
  16. const rollout = new Rollout(config)
  17. const { historyBufferLevel, forcePersistBuffer } =
  18. rollout.getHistoryBufferLevelOptions('test-project-id')
  19. expect(historyBufferLevel).to.equal(2)
  20. expect(forcePersistBuffer).to.be.false
  21. })
  22. it('should return a valid history buffer level and force persist buffer options', function () {
  23. setMockConfig('historyBufferLevel', '1')
  24. setMockConfig('forcePersistBuffer', 'true')
  25. const rollout = new Rollout(config)
  26. const { historyBufferLevel, forcePersistBuffer } =
  27. rollout.getHistoryBufferLevelOptions('test-project-id')
  28. expect(historyBufferLevel).to.equal(1)
  29. expect(forcePersistBuffer).to.be.true
  30. })
  31. describe('with a higher next history buffer level rollout', function () {
  32. beforeEach(function () {
  33. setMockConfig('historyBufferLevel', '2')
  34. setMockConfig('forcePersistBuffer', 'false')
  35. setMockConfig('nextHistoryBufferLevel', '3')
  36. })
  37. it('should return the expected history buffer level when the rollout percentage is zero', function () {
  38. setMockConfig('nextHistoryBufferLevelRolloutPercentage', '0')
  39. const rollout = new Rollout(config)
  40. for (let i = 0; i < 1000; i++) {
  41. const { historyBufferLevel, forcePersistBuffer } =
  42. rollout.getHistoryBufferLevelOptions(`test-project-id-${i}`)
  43. expect(historyBufferLevel).to.equal(2)
  44. expect(forcePersistBuffer).to.be.false
  45. }
  46. })
  47. it('should return the expected distribution of levels when the rollout percentage is 10%', function () {
  48. setMockConfig('nextHistoryBufferLevelRolloutPercentage', '10')
  49. const rollout = new Rollout(config)
  50. let currentLevel = 0
  51. let nextLevel = 0
  52. for (let i = 0; i < 1000; i++) {
  53. const { historyBufferLevel } = rollout.getHistoryBufferLevelOptions(
  54. `test-project-id-${i}`
  55. )
  56. switch (historyBufferLevel) {
  57. case 2:
  58. currentLevel++
  59. break
  60. case 3:
  61. nextLevel++
  62. break
  63. default:
  64. expect.fail(
  65. `Unexpected history buffer level: ${historyBufferLevel}`
  66. )
  67. }
  68. }
  69. const twoPercentage = (currentLevel / 1000) * 100
  70. const threePercentage = (nextLevel / 1000) * 100
  71. expect(twoPercentage).to.be.closeTo(90, 5) // 90% for level 2
  72. expect(threePercentage).to.be.closeTo(10, 5) // 10% for level 3
  73. })
  74. })
  75. describe('with a next history buffer level lower than the current level', function () {
  76. beforeEach(function () {
  77. setMockConfig('historyBufferLevel', '3')
  78. setMockConfig('forcePersistBuffer', 'false')
  79. setMockConfig('nextHistoryBufferLevel', '2')
  80. })
  81. it('should always return the current level when the rollout percentage is zero', function () {
  82. setMockConfig('nextHistoryBufferLevelRolloutPercentage', '0')
  83. const rollout = new Rollout(config)
  84. for (let i = 0; i < 1000; i++) {
  85. const { historyBufferLevel, forcePersistBuffer } =
  86. rollout.getHistoryBufferLevelOptions(`test-project-id-${i}`)
  87. expect(historyBufferLevel).to.equal(3)
  88. expect(forcePersistBuffer).to.be.false
  89. }
  90. })
  91. it('should always return the current level regardless of the rollout percentage', function () {
  92. setMockConfig('nextHistoryBufferLevelRolloutPercentage', '10')
  93. const rollout = new Rollout(config)
  94. for (let i = 0; i < 1000; i++) {
  95. const { historyBufferLevel } = rollout.getHistoryBufferLevelOptions(
  96. `test-project-id-${i}`
  97. )
  98. expect(historyBufferLevel).to.equal(3)
  99. }
  100. })
  101. })
  102. })
  103. function setMockConfig(path, value) {
  104. config.has.withArgs(path).returns(true)
  105. config.get.withArgs(path).returns(value)
  106. }