tasks.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict'
  2. const { ObjectId } = require('mongodb')
  3. const { expect } = require('chai')
  4. const config = require('config')
  5. const tasks = require('../../../../storage/tasks')
  6. const {
  7. persistor,
  8. historyStore,
  9. knex,
  10. mongodb,
  11. } = require('../../../../storage')
  12. const cleanup = require('./support/cleanup')
  13. const CHUNK_STORE_BUCKET = config.get('chunkStore.bucket')
  14. const postgresProjectId = 1
  15. const mongoProjectId = ObjectId('abcdefabcdefabcdefabcdef')
  16. describe('tasks', function () {
  17. beforeEach(cleanup.everything)
  18. const options = {
  19. batchSize: 3,
  20. timeout: 3000,
  21. minAgeSecs: 3600,
  22. maxBatches: 1000,
  23. }
  24. it('deletes old chunks', async function () {
  25. const postgresChunks = []
  26. const mongoChunks = []
  27. for (let i = 1; i <= 25; i++) {
  28. const deletedAt = new Date(Date.now() - 86400000)
  29. const startVersion = (i - 1) * 10
  30. const endVersion = i * 10
  31. postgresChunks.push({
  32. chunk_id: i,
  33. doc_id: postgresProjectId,
  34. start_version: startVersion,
  35. end_version: endVersion,
  36. deleted_at: deletedAt,
  37. })
  38. mongoChunks.push({
  39. _id: ObjectId(i.toString().padStart(24, '0')),
  40. projectId: mongoProjectId,
  41. startVersion,
  42. endVersion,
  43. state: 'deleted',
  44. updatedAt: deletedAt,
  45. })
  46. }
  47. for (let i = 26; i <= 30; i++) {
  48. const deletedAt = new Date()
  49. const startVersion = (i - 1) * 10
  50. const endVersion = i * 10
  51. postgresChunks.push({
  52. chunk_id: i,
  53. doc_id: postgresProjectId,
  54. start_version: startVersion,
  55. end_version: endVersion,
  56. deleted_at: deletedAt,
  57. })
  58. mongoChunks.push({
  59. _id: ObjectId(i.toString().padStart(24, '0')),
  60. projectId: mongoProjectId,
  61. startVersion,
  62. endVersion,
  63. state: 'deleted',
  64. updatedAt: deletedAt,
  65. })
  66. }
  67. await knex('old_chunks').insert(postgresChunks)
  68. await mongodb.chunks.insertMany(mongoChunks)
  69. await Promise.all([
  70. ...postgresChunks.map(chunk =>
  71. historyStore.storeRaw(postgresProjectId.toString(), chunk.chunk_id, {
  72. history: 'raw history',
  73. })
  74. ),
  75. ...mongoChunks.map(chunk =>
  76. historyStore.storeRaw(mongoProjectId.toString(), chunk._id.toString(), {
  77. history: 'raw history',
  78. })
  79. ),
  80. ])
  81. await expectChunksExist(1, 30, true)
  82. await tasks.deleteOldChunks(options)
  83. await expectChunksExist(1, 25, false)
  84. await expectChunksExist(26, 30, true)
  85. })
  86. })
  87. async function expectChunksExist(minChunkId, maxChunkId, expected) {
  88. const keys = []
  89. for (let i = minChunkId; i <= maxChunkId; i++) {
  90. keys.push(`100/000/000/${i.toString().padStart(9, '0')}`)
  91. keys.push(`fed/cba/fedcbafedcbafedcba/${i.toString().padStart(24, '0')}`)
  92. }
  93. return Promise.all(
  94. keys.map(async key => {
  95. const exists = await persistor.checkIfObjectExists(
  96. CHUNK_STORE_BUCKET,
  97. key
  98. )
  99. expect(exists).to.equal(expected)
  100. })
  101. )
  102. }