FlushManagerTests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import async from 'async'
  2. import nock from 'nock'
  3. import { expect } from 'chai'
  4. import request from 'request'
  5. import assert from 'assert'
  6. import { ObjectId } from 'mongodb'
  7. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  8. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  9. const MockHistoryStore = () => nock('http://localhost:3100')
  10. const MockWeb = () => nock('http://localhost:3000')
  11. describe('Flushing old queues', function () {
  12. const historyId = ObjectId().toString()
  13. beforeEach(function (done) {
  14. this.timestamp = new Date()
  15. ProjectHistoryApp.ensureRunning(error => {
  16. if (error) {
  17. throw error
  18. }
  19. this.projectId = ObjectId().toString()
  20. this.docId = ObjectId().toString()
  21. this.fileId = ObjectId().toString()
  22. MockHistoryStore().post('/api/projects').reply(200, {
  23. projectId: historyId,
  24. })
  25. MockWeb()
  26. .get(`/project/${this.projectId}/details`)
  27. .reply(200, {
  28. name: 'Test Project',
  29. overleaf: {
  30. history: {
  31. id: historyId,
  32. },
  33. },
  34. })
  35. MockHistoryStore()
  36. .get(`/api/projects/${historyId}/latest/history`)
  37. .reply(200, {
  38. chunk: {
  39. startVersion: 0,
  40. history: {
  41. changes: [],
  42. },
  43. },
  44. })
  45. ProjectHistoryClient.initializeProject(historyId, done)
  46. })
  47. })
  48. afterEach(function () {
  49. nock.cleanAll()
  50. })
  51. describe('retrying an unflushed project', function () {
  52. describe('when the update is older than the cutoff', function () {
  53. beforeEach(function (done) {
  54. this.flushCall = MockHistoryStore()
  55. .put(
  56. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  57. )
  58. .reply(201)
  59. .post(`/api/projects/${historyId}/legacy_changes?end_version=0`)
  60. .reply(200)
  61. const update = {
  62. pathname: '/main.tex',
  63. docLines: 'a\nb',
  64. doc: this.docId,
  65. meta: { user_id: this.user_id, ts: new Date() },
  66. }
  67. async.series(
  68. [
  69. cb =>
  70. ProjectHistoryClient.pushRawUpdate(this.projectId, update, cb),
  71. cb =>
  72. ProjectHistoryClient.setFirstOpTimestamp(
  73. this.projectId,
  74. Date.now() - 24 * 3600 * 1000,
  75. cb
  76. ),
  77. ],
  78. done
  79. )
  80. })
  81. it('flushes the project history queue', function (done) {
  82. request.post(
  83. {
  84. url: 'http://localhost:3054/flush/old?maxAge=10800',
  85. },
  86. (error, res, body) => {
  87. if (error) {
  88. return done(error)
  89. }
  90. expect(res.statusCode).to.equal(200)
  91. assert(
  92. this.flushCall.isDone(),
  93. 'made calls to history service to store updates'
  94. )
  95. done()
  96. }
  97. )
  98. })
  99. it('flushes the project history queue in the background when requested', function (done) {
  100. request.post(
  101. {
  102. url: 'http://localhost:3054/flush/old?maxAge=10800&background=1',
  103. },
  104. (error, res, body) => {
  105. if (error) {
  106. return done(error)
  107. }
  108. expect(res.statusCode).to.equal(200)
  109. expect(body).to.equal('{"message":"running flush in background"}')
  110. assert(
  111. !this.flushCall.isDone(),
  112. 'did not make calls to history service to store updates in the foreground'
  113. )
  114. setTimeout(() => {
  115. assert(
  116. this.flushCall.isDone(),
  117. 'made calls to history service to store updates in the background'
  118. )
  119. done()
  120. }, 100)
  121. }
  122. )
  123. })
  124. })
  125. describe('when the update is newer than the cutoff', function () {
  126. beforeEach(function (done) {
  127. this.flushCall = MockHistoryStore()
  128. .put(
  129. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  130. )
  131. .reply(201)
  132. .post(`/api/projects/${historyId}/legacy_changes?end_version=0`)
  133. .reply(200)
  134. const update = {
  135. pathname: '/main.tex',
  136. docLines: 'a\nb',
  137. doc: this.docId,
  138. meta: { user_id: this.user_id, ts: new Date() },
  139. }
  140. async.series(
  141. [
  142. cb =>
  143. ProjectHistoryClient.pushRawUpdate(this.projectId, update, cb),
  144. cb =>
  145. ProjectHistoryClient.setFirstOpTimestamp(
  146. this.projectId,
  147. Date.now() - 60 * 1000,
  148. cb
  149. ),
  150. ],
  151. done
  152. )
  153. })
  154. it('does not flush the project history queue', function (done) {
  155. request.post(
  156. {
  157. url: `http://localhost:3054/flush/old?maxAge=${3 * 3600}`,
  158. },
  159. (error, res, body) => {
  160. if (error) {
  161. return done(error)
  162. }
  163. expect(res.statusCode).to.equal(200)
  164. assert(
  165. !this.flushCall.isDone(),
  166. 'did not make calls to history service to store updates'
  167. )
  168. done()
  169. }
  170. )
  171. })
  172. })
  173. describe('when the update does not have a timestamp', function () {
  174. beforeEach(function (done) {
  175. this.flushCall = MockHistoryStore()
  176. .put(
  177. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  178. )
  179. .reply(201)
  180. .post(`/api/projects/${historyId}/legacy_changes?end_version=0`)
  181. .reply(200)
  182. const update = {
  183. pathname: '/main.tex',
  184. docLines: 'a\nb',
  185. doc: this.docId,
  186. meta: { user_id: this.user_id, ts: new Date() },
  187. }
  188. this.startDate = Date.now()
  189. async.series(
  190. [
  191. cb =>
  192. ProjectHistoryClient.pushRawUpdate(this.projectId, update, cb),
  193. cb =>
  194. ProjectHistoryClient.clearFirstOpTimestamp(this.projectId, cb),
  195. ],
  196. done
  197. )
  198. })
  199. it('flushes the project history queue anyway', function (done) {
  200. request.post(
  201. {
  202. url: `http://localhost:3054/flush/old?maxAge=${3 * 3600}`,
  203. },
  204. (error, res, body) => {
  205. if (error) {
  206. return done(error)
  207. }
  208. expect(res.statusCode).to.equal(200)
  209. assert(
  210. this.flushCall.isDone(),
  211. 'made calls to history service to store updates'
  212. )
  213. ProjectHistoryClient.getFirstOpTimestamp(
  214. this.projectId,
  215. (err, result) => {
  216. if (err) {
  217. return done(err)
  218. }
  219. expect(result).to.be.null
  220. done()
  221. }
  222. )
  223. }
  224. )
  225. })
  226. })
  227. })
  228. })