Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce device id v2 #362

Merged
merged 5 commits into from Dec 23, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

Add device id v2 test

  • Loading branch information
darkdh committed Dec 10, 2019
commit f6a25c1fb62ef5c16e7cf7e2ff41e909a2123f5a
@@ -31,6 +31,26 @@ const isExpiredCredentialError = (error) => {
})
}

const createAndSubscribeSQSforCategory = function (deviceId, category, thisRef) {
let newQueueParams = {
QueueName: thisRef.sqsName(deviceId, category),
Attributes: {
'MessageRetentionPeriod': s3Helper.SQS_RETENTION
}
}
return new Promise((resolve, reject) => {
thisRef.sqs.createQueue(newQueueParams, (error, data) => {
if (error) {
console.log('SQS creation failed with error: ' + error)
reject(error)
} else if (data) {
thisRef.SQSUrlByCat[category] = data.QueueUrl
resolve([])
}
})
})
}

/**
* @param {{
* apiVersion: <string>,
@@ -378,6 +398,22 @@ RequestUtil.prototype.sqsName = function (deviceId, category) {
return queueName
}

/**
* Main purpose of this function is to create old SQS queue to test device id v2
* migration
* @param {string} deviceId
* @returns {Promise}
*/
RequestUtil.prototype.createAndSubscribeSQSForTest = function (deviceId) {
var createSQSPromises = []
// Simple for loop instead foreach to capture 'this'
for (var i = 0; i < CATEGORIES_FOR_SQS.length; ++i) {
createSQSPromises.push(createAndSubscribeSQSforCategory(deviceId, CATEGORIES_FOR_SQS[i], this))
}

return Promise.all(createSQSPromises)
}

/**
* Creates SQS for the current device.
* @param {string} deviceId
@@ -389,27 +425,7 @@ RequestUtil.prototype.createAndSubscribeSQS = function (deviceId, deviceIdV2) {
if (!deviceIdV2) {
throw new Error('createSQS failed. deviceIdV2 is null!')
}
this.deviceId = deviceId
this.deviceIdV2 = deviceIdV2
const createAndSubscribeSQSforCategory = function (deviceId, category, thisRef) {
let newQueueParams = {
QueueName: thisRef.sqsName(deviceId, category),
Attributes: {
'MessageRetentionPeriod': s3Helper.SQS_RETENTION
}
}
return new Promise((resolve, reject) => {
thisRef.sqs.createQueue(newQueueParams, (error, data) => {
if (error) {
console.log('SQS creation failed with error: ' + error)
reject(error)
} else if (data) {
thisRef.SQSUrlByCat[category] = data.QueueUrl
resolve([])
}
})
})
}

const subscribeOldSQSforCategory = function (deviceId, category, thisRef) {
return new Promise((resolve, reject) => {
@@ -0,0 +1,148 @@
const test = require('tape')
const testHelper = require('../testHelper')
const clientTestHelper = require('./testHelper')
const Serializer = require('../../lib/serializer')
const RequestUtil = require('../../client/requestUtil')
const proto = require('../../client/constants/proto')

test('deviceId V2 migration', (t) => {
t.test('constructor', (t) => {
Serializer.init().then((serializer) => {
clientTestHelper.getSerializedCredentials(serializer).then((data) => {
const args = {
apiVersion: clientTestHelper.CONFIG.apiVersion,
credentialsBytes: data.serializedCredentials,
keys: data.keys,
serializer,
serverUrl: clientTestHelper.CONFIG.serverUrl
}

t.throws(() => { return new RequestUtil() }, 'requires arguments')
const requiredArgs = ['apiVersion', 'keys', 'serializer', 'serverUrl']
for (let arg of requiredArgs) {
let lessArgs = Object.assign({}, args)
lessArgs[arg] = undefined
t.throws(() => { return new RequestUtil(lessArgs) }, `requires ${arg}`)
}

const requestUtil = new RequestUtil(args)
requestUtil.createAndSubscribeSQSForTest('1')
.then(() => {
t.pass('can instantiate requestUtil')
t.test('prototype', (t) => {
setTimeout(() => {
testPrototype(t, requestUtil, data.keys)
}, 5000)
})
}).catch((error) => { t.end(error) })
}).catch((error) => { t.end(error) })
})
})

const testPrototype = (t, requestUtil, keys) => {
test.onFinish(() => {
requestUtil.deleteUser()
.then(() => {
requestUtil.purgeUserQueue()
})
.catch((error) => { console.log(`Cleanup failed: ${error}`) })
})

const record = {
action: 'CREATE',
deviceId: new Uint8Array([0]),
objectId: testHelper.newUuid(),
bookmark: {
site: {
location: `https://brave.com?q=${'x'.repeat(4)}`,
title: 'Brave',
lastAccessedTime: 1480000000 * 1000,
creationTime: 1480000000 * 1000
},
isFolder: false,
hideInToolbar: false,
order: '1.0.0.1'
}
}
const record2 = {
action: 'CREATE',
deviceId: new Uint8Array([0]),
objectId: testHelper.newUuid(),
bookmark: {
site: {
location: `https://brave.com?q=${'y'.repeat(4)}`,
title: 'Brave2',
lastAccessedTime: 1480000000 * 1000,
creationTime: 1480000000 * 1000
},
isFolder: false,
hideInToolbar: false,
order: '1.0.0.2'
}
}
t.test('#put record before migration', (t) => {
requestUtil.put(proto.categories.BOOKMARKS, record)
.then((response) => {
t.pass(`${t.name} resolves`)
requestUtil.createAndSubscribeSQS('1', 'beef12')
.then(() => {
setTimeout(() => {
t.pass('subscribe to new SQS queue')
testCanListFromOldQueue(t)
}, 5000)
}).catch((error) => { t.end(error) })
})
.catch((error) => { t.fail(error) })
})
const testCanListFromOldQueue = (t) => {
t.test('can list notification from old SQS queue', (t) => {
let currentTime = new Date().getTime()
requestUtil.list(proto.categories.BOOKMARKS, currentTime)
.then(s3Objects => requestUtil.s3ObjectsToRecords(s3Objects.contents))
.then((response) => {
t.equals(response.length, 1)
const s3Record = response[0].record
// FIXME: Should this deserialize to 'CREATE' ?
t.equals(s3Record.action, 0, `${t.name}: action`)
t.deepEquals(s3Record.deviceId, record.deviceId, `${t.name}: deviceId`)
t.deepEquals(s3Record.objectId, record.objectId, `${t.name}: objectId`)
t.deepEquals(s3Record.bookmark.site, record.bookmark.site, `${t.name}: bookmark.site`)
t.deepEquals(s3Record.bookmark.isFolder, record.bookmark.isFolder, `${t.name}: bookmark.isFolder`)
t.deepEquals(s3Record.bookmark.hideInToolbar, record.bookmark.hideInToolbar, `${t.name}: bookmark.hideInToolbar`)
t.deepEquals(s3Record.bookmark.order, record.bookmark.order, `${t.name}: bookmark.order`)
putRecordAfterMigration(t)
})
.catch((error) => { t.fail(error) })
})
}
const putRecordAfterMigration = (t) => {
requestUtil.put(proto.categories.BOOKMARKS, record2)
.then((response) => {
setTimeout(() => {
testCanListFromBothQueues(t)
}, 5000)
})
.catch((error) => { t.fail(error) })
}
const testCanListFromBothQueues = (t) => {
t.test('can list notifications from new and old SQS queues', (t) => {
let currentTime = new Date().getTime()
requestUtil.list(proto.categories.BOOKMARKS, currentTime)
.then(s3Objects => requestUtil.s3ObjectsToRecords(s3Objects.contents))
.then((response) => {
t.equals(response.length, 1)
const s3Record = response[0].record
// FIXME: Should this deserialize to 'CREATE' ?
t.equals(s3Record.action, 0, `${t.name}: action`)
t.deepEquals(s3Record.deviceId, record2.deviceId, `${t.name}: deviceId`)
t.deepEquals(s3Record.objectId, record2.objectId, `${t.name}: objectId`)
t.deepEquals(s3Record.bookmark.site, record2.bookmark.site, `${t.name}: bookmark.site`)
t.deepEquals(s3Record.bookmark.isFolder, record2.bookmark.isFolder, `${t.name}: bookmark.isFolder`)
t.deepEquals(s3Record.bookmark.hideInToolbar, record2.bookmark.hideInToolbar, `${t.name}: bookmark.hideInToolbar`)
t.deepEquals(s3Record.bookmark.order, record2.bookmark.order, `${t.name}: bookmark.order`)
t.end()
})
})
}
}
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.