From 0ae585f3f07e91967c38b0410ad2db11c6aaebdf Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Wed, 29 May 2024 08:20:52 +0100 Subject: [PATCH] wip --- lib/submit-request.js | 10 +++++++++- test/akg-debug.js | 9 +++++++++ test/client/submit.js | 33 ++++++++++++++++++++++++++++----- 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 test/akg-debug.js diff --git a/lib/submit-request.js b/lib/submit-request.js index 5c33fb9b1..5488e526d 100644 --- a/lib/submit-request.js +++ b/lib/submit-request.js @@ -2,6 +2,7 @@ var ot = require('./ot'); var projections = require('./projections'); var ShareDBError = require('./error'); var types = require('./types'); +var akg = require('../test/akg-debug'); var ERROR_CODE = ShareDBError.CODES; @@ -91,17 +92,21 @@ SubmitRequest.prototype.submit = function(callback) { // Send a special projection so that getSnapshot knows to return all fields. // With a null projection, it strips document metadata var fields = {$submit: true}; + akg.debug('submit', collection, id, op); var snapshotOptions = {}; snapshotOptions.agentCustom = request.agent.custom; backend.db.getSnapshot(collection, id, fields, snapshotOptions, function(err, snapshot) { if (err) return callback(err); + akg.debug('get snapshot', collection, id, snapshot); request.snapshot = snapshot; request._addSnapshotMeta(); if (op.v == null) { + akg.debug('null version'); if (op.create && snapshot.type && op.src) { + akg.debug('check create collision'); // If the document was already created by another op, we will return a // 'Document already exists' error in response and fail to submit this // op. However, this could also happen in the case that the op was @@ -110,7 +115,8 @@ SubmitRequest.prototype.submit = function(callback) { // must get the past ops and check their src and seq values to // differentiate. request._fetchCreateOpVersion(function(error, version) { - if (err) return callback(err); + akg.debug('fetch create op version returned', error, version); + if (error) return callback(error); if (version == null) { callback(request.alreadyCreatedError()); } else { @@ -301,6 +307,7 @@ SubmitRequest.prototype._shouldSaveMilestoneSnapshot = function(snapshot) { SubmitRequest.prototype._fetchCreateOpVersion = function(callback) { var create = this.snapshot.m._create; + akg.debug('got create meta', create); if (create) { var version = (create.src === this.op.src && create.seq === this.op.seq) ? create.v : null; return callback(null, version); @@ -310,6 +317,7 @@ SubmitRequest.prototype._fetchCreateOpVersion = function(callback) { // This can happen if a client tries to re-create or resubmit a create op to // a "legacy" snapshot that existed before we started adding the meta (should // be uncommon) or when using a driver that doesn't support metadata (eg Postgres). + akg.debug('get committed op version...'); this.backend.db.getCommittedOpVersion(this.collection, this.id, this.snapshot, this.op, null, callback); }; diff --git a/test/akg-debug.js b/test/akg-debug.js new file mode 100644 index 000000000..bbb7c18f8 --- /dev/null +++ b/test/akg-debug.js @@ -0,0 +1,9 @@ +function debug() { + if (!module.exports.enabled) return; + console.log.apply(console, arguments); +} + +module.exports = { + enabled: false, + debug: debug +}; diff --git a/test/client/submit.js b/test/client/submit.js index c74058499..4a4519936 100644 --- a/test/client/submit.js +++ b/test/client/submit.js @@ -6,11 +6,11 @@ var deserializedType = require('./deserialized-type'); var numberType = require('./number-type'); var errorHandler = require('../util').errorHandler; var richText = require('rich-text'); -var MemoryDB = require('../../lib/db/memory'); types.register(deserializedType.type); types.register(deserializedType.type2); types.register(numberType.type); types.register(richText.type); +var akg = require('../akg-debug'); module.exports = function() { describe('client submit', function() { @@ -210,13 +210,17 @@ module.exports = function() { describe('create', function() { describe('metadata enabled', function() { + afterEach(function() { + akg.enabled = false; + }); + runCreateTests(); }); describe('no snapshot metadata available', function() { beforeEach(function() { - var getSnapshot = MemoryDB.prototype.getSnapshot; - sinon.stub(MemoryDB.prototype, 'getSnapshot') + var getSnapshot = this.backend.db.getSnapshot; + sinon.stub(this.backend.db, 'getSnapshot') .callsFake(function() { var args = Array.from(arguments); var callback = args.pop(); @@ -229,6 +233,7 @@ module.exports = function() { }); afterEach(function() { + akg.enabled = false; sinon.restore(); }); @@ -297,6 +302,8 @@ module.exports = function() { }); it('does not fail when resubmitting a create op on a doc that was deleted', function(done) { + akg.enabled = true; + console.log('> START TEST'); var backend = this.backend; var connection1 = backend.connect(); var connection2 = backend.connect(); @@ -304,20 +311,36 @@ module.exports = function() { var doc2 = connection2.get('dogs', 'fido'); async.series([ - doc1.create.bind(doc1, {age: 3}), - doc1.del.bind(doc1), + function(next) { + console.log('doc1 create...'); + doc1.create({age: 3}, function(error) { + console.log('doc1 created', error); + next(error); + }); + }, + function(next) { + console.log('doc1 del...'); + doc1.del(function(error) { + console.log('doc1 deleted', error); + next(error); + }); + }, function(next) { var submitted = false; backend.use('submit', function(request, next) { if (!submitted) { submitted = true; + console.log('reconnecting connection2'); connection2.close(); backend.connect(connection2); } next(); }); + console.log('doc2 create...'); doc2.create({name: 'Fido'}, function(error) { + console.log('doc2 created', error); + console.log('doc2 version', doc2.version); expect(doc2.version).to.equal(3); next(error); });