Merge pull request #12 from overleaf/spd-force-race

Handle duplicate-notification race condition when using forceCreate
This commit is contained in:
Simon Detheridge 2019-11-29 10:35:48 +00:00 committed by GitHub
commit 20dbbbeb58
2 changed files with 61 additions and 67 deletions

View file

@ -15,25 +15,18 @@ module.exports = Notifications =
callback err, notifications
_checkExistingNotifcationAndOverride : (user_id, notification, callback)->
self = @
_countExistingNotifications : (user_id, notification, callback = (err, count)->)->
query =
user_id: ObjectId(user_id)
key: notification.key
db.notifications.count query, (err, number)->
if number > 0 and !notification.forceCreate
logger.log number:number, user_id:user_id, key:notification.key, "alredy has notification key for user"
return callback(number)
else if number > 0 and notification.forceCreate
self.deleteNotificationByKeyOnly notification.key, callback
else
callback()
db.notifications.count query, (err, count)->
return callback(err) if err?
callback(null, count)
addNotification: (user_id, notification, callback)->
@_checkExistingNotifcationAndOverride user_id, notification, (err)->
if err?
callback err
else
@_countExistingNotifications user_id, notification, (err, count)->
return callback(err) if err?
return callback() unless count == 0 || notification.forceCreate
doc =
user_id: ObjectId(user_id)
key: notification.key
@ -50,7 +43,7 @@ module.exports = Notifications =
catch err
logger.error {user_id, expires: notification.expires}, "error converting `expires` field to Date"
return callback(err)
db.notifications.insert(doc, callback)
db.notifications.update({user_id: doc.user_id, key: notification.key}, doc, {upsert: true}, callback)
removeNotificationId: (user_id, notification_id, callback)->
searchOps =

View file

@ -29,7 +29,8 @@ describe 'Notifications Tests', ->
remove: @removeStub
@mongojs.ObjectId = ObjectId
@notifications = SandboxedModule.require modulePath, requires:
@notifications = SandboxedModule.require modulePath,
requires:
'logger-sharelatex': {
log:()->
error:()->
@ -37,6 +38,8 @@ describe 'Notifications Tests', ->
'settings-sharelatex': {}
'mongojs':@mongojs
'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
globals:
console: console
@stubbedNotification = {user_id: ObjectId(user_id), key:"notification-key", messageOpts:"some info", templateKey:"template-key"}
@stubbedNotificationArray = [@stubbedNotification]
@ -63,33 +66,34 @@ describe 'Notifications Tests', ->
messageOpts:"some info",
templateKey:"template-key"
}
@notifications.deleteNotificationByKeyOnly = sinon.stub()
@notifications.deleteNotificationByKeyOnly.callsArgWith(1, null)
@expectedQuery = {
user_id: @stubbedNotification.user_id,
key:"notification-key",
}
@updateStub.yields()
@countStub.yields(null, 0)
it 'should insert the notification into the collection', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 0)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
expect(err).not.exists
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true })
done()
it 'should fail insert of existing notification key', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 1)
describe 'when there is an existing notification', (done) ->
beforeEach ->
@countStub.yields(null, 1)
it 'should fail to insert', (done)->
@notifications.addNotification user_id, @stubbedNotification, (err)=>
@insertStub.calledWith(@expectedDocument).should.equal false
expect(err).not.exists
sinon.assert.notCalled(@updateStub)
done()
describe "when key already exists but forceCreate is passed", (done)->
it "should delete the old key and insert the new one", (done) ->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 1)
it "should update the key if forceCreate is true", (done) ->
@stubbedNotification.forceCreate = true
@notifications.addNotification user_id, @stubbedNotification, (err)=>
assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
@notifications.deleteNotificationByKeyOnly.calledWith(@stubbedNotification.key).should.equal true
expect(err).not.exists
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true })
done()
describe 'when the notification is set to expire', () ->
@ -108,14 +112,15 @@ describe 'Notifications Tests', ->
templateKey:"template-key",
expires: new Date(@stubbedNotification.expires),
}
@expectedQuery = {
user_id: @stubbedNotification.user_id,
key:"notification-key",
}
it 'should add an `expires` Date field to the document', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 0)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
@insertStub.callCount.should.equal 1
assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
expect(err).not.exists
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true })
done()
describe 'when the notification has a nonsensical expires field', () ->
@ -136,13 +141,9 @@ describe 'Notifications Tests', ->
}
it 'should produce an error', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 0)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
(err instanceof Error).should.equal true
@insertStub.callCount.should.equal 0
@insertStub.calledWith(@expectedDocument).should.equal false
sinon.assert.notCalled(@updateStub)
done()
describe 'removeNotificationId', ->