tasks.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = new 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: new 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: new 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(
  72. postgresProjectId.toString(),
  73. chunk.chunk_id.toString(),
  74. {
  75. history: 'raw history',
  76. }
  77. )
  78. ),
  79. ...mongoChunks.map(chunk =>
  80. historyStore.storeRaw(mongoProjectId.toString(), chunk._id.toString(), {
  81. history: 'raw history',
  82. })
  83. ),
  84. ])
  85. await expectChunksExist(1, 30, true)
  86. await tasks.deleteOldChunks(options)
  87. await expectChunksExist(1, 25, false)
  88. await expectChunksExist(26, 30, true)
  89. })
  90. })
  91. async function expectChunksExist(minChunkId, maxChunkId, expected) {
  92. const keys = []
  93. for (let i = minChunkId; i <= maxChunkId; i++) {
  94. keys.push(`100/000/000/${i.toString().padStart(9, '0')}`)
  95. keys.push(`fed/cba/fedcbafedcbafedcba/${i.toString().padStart(24, '0')}`)
  96. }
  97. return await Promise.all(
  98. keys.map(async key => {
  99. const exists = await persistor.checkIfObjectExists(
  100. CHUNK_STORE_BUCKET,
  101. key
  102. )
  103. expect(exists).to.equal(expected)
  104. })
  105. )
  106. }