update archiving acceptance tests

This commit is contained in:
Brian Gough 2016-03-10 15:15:57 +00:00
parent 98738d1344
commit 5ea7a31ad9
2 changed files with 35 additions and 16 deletions

View file

@ -42,15 +42,15 @@ describe "Archiving updates", ->
sinon.spy MockDocStoreApi, "getAllDoc"
@updates = []
for i in [0..1024+9]
for i in [0..512+10]
@updates.push {
op: [{ i: "a", p: 0 }]
meta: { ts: @now - i * @hours, user_id: @user_id }
meta: { ts: @now + (i-2048) * @hours, user_id: @user_id }
v: 2 * i + 1
}
@updates.push {
op: [{ i: "b", p: 0 }]
meta: { ts: @now - i * @hours + 10*@minutes, user_id: @user_id }
meta: { ts: @now + (i-2048) * @hours + 10*@minutes, user_id: @user_id }
v: 2 * i + 2
}
@ -62,8 +62,9 @@ describe "Archiving updates", ->
after (done) ->
MockWebApi.getUserInfo.restore()
db.docHistory.remove {project_id: ObjectId(@project_id)}, () ->
TrackChangesClient.removeS3Doc @project_id, @doc_id, done
db.docHistory.remove {project_id: ObjectId(@project_id)}, () =>
db.docHistoryIndex.remove {project_id: ObjectId(@project_id)}, () =>
TrackChangesClient.removeS3Doc @project_id, @doc_id, done
describe "archiving a doc's updates", ->
before (done) ->
@ -97,14 +98,19 @@ describe "Archiving updates", ->
it "should have a docHistoryIndex entry with the last version", (done) ->
db.docHistoryIndex.findOne { _id: ObjectId(@doc_id) }, (error, index) ->
throw error if error?
index.packs[0].v_end.should.equal 100
index.packs[0].v_end.should.equal 1024
done()
# it "should store twenty doc changes in S3 in one pack", (done) ->
# TrackChangesClient.getS3Doc @project_id, @doc_id, (error, res, doc) =>
# doc.length.should.equal 1
# doc[0].pack.length.should.equal 20
# done()
it "should store 1024 doc changes in S3 in one pack", (done) ->
db.docHistoryIndex.findOne { _id: ObjectId(@doc_id) }, (error, index) =>
throw error if error?
console.log "index", index, JSON.stringify(index)
pack_id = index.packs[0]._id
TrackChangesClient.getS3Doc @project_id, @doc_id, pack_id, (error, doc) =>
console.log error, "DOC", doc
doc.n.should.equal 1024
doc.pack.length.should.equal 1024
done()
describe "unarchiving a doc's updates", ->
before (done) ->

View file

@ -1,3 +1,5 @@
async = require 'async'
zlib = require 'zlib'
request = require "request"
rclient = require("redis").createClient() # Only works locally for now
{db, ObjectId} = require "../../../../app/js/mongojs"
@ -99,10 +101,21 @@ module.exports = TrackChangesClient =
uri:"https://#{Settings.trackchanges.stores.doc_history}.s3.amazonaws.com/#{key}"
}
getS3Doc: (project_id, doc_id, callback = (error, res, body) ->) ->
options = TrackChangesClient.buildS3Options(true, project_id+"/changes-"+doc_id)
request.get options, callback
getS3Doc: (project_id, doc_id, pack_id, callback = (error, body) ->) ->
options = TrackChangesClient.buildS3Options(true, project_id+"/changes-"+doc_id+"/pack-"+pack_id)
options.encoding = null
request.get options, (err, res, body) ->
console.log "body", typeof body
return callback(error) if error?
zlib.gunzip body, (err, result) ->
return callback(err) if err?
callback(null, JSON.parse(result.toString()))
removeS3Doc: (project_id, doc_id, callback = (error, res, body) ->) ->
options = TrackChangesClient.buildS3Options(true, project_id+"/changes-"+doc_id)
request.del options, callback
options = TrackChangesClient.buildS3Options(true, "?prefix=" + project_id + "/changes-" +doc_id)
request.get options, (error, res, body) ->
keys = body.match /[0-9a-f]{24}\/changes-[0-9a-f]{24}\/pack-[0-9a-f]{24}/g
async.eachSeries keys, (key, cb) ->
options = TrackChangesClient.buildS3Options(true, key)
request.del options, cb
, callback