-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Refactor [github] token persistence, again #1906
Changes from 5 commits
0602996
3f8daa1
238cdcb
42ee5fe
e471b43
95c1d7e
aae9b67
42cfcb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,22 +3,11 @@ | |
const fs = require('fs') | ||
const tmp = require('tmp') | ||
const readFile = require('fs-readfile-promise') | ||
const sinon = require('sinon') | ||
const { sleep } = require('wait-promise') | ||
const { expect } = require('chai') | ||
const TokenPersistence = require('./token-persistence') | ||
const githubAuth = require('./github-auth') | ||
|
||
describe('Token persistence', function() { | ||
// Fake timers must be set up before any timers are scheduled. | ||
let clock | ||
beforeEach(function() { | ||
clock = sinon.useFakeTimers() | ||
}) | ||
afterEach(function() { | ||
clock.restore() | ||
}) | ||
|
||
beforeEach(githubAuth.removeAllTokens) | ||
afterEach(githubAuth.removeAllTokens) | ||
|
||
|
@@ -27,67 +16,63 @@ describe('Token persistence', function() { | |
path = tmp.tmpNameSync() | ||
persistence = new TokenPersistence({ path }) | ||
}) | ||
afterEach(function() { | ||
if (persistence) { | ||
persistence.stop() | ||
persistence = null | ||
} | ||
}) | ||
|
||
context('when the file does not exist', function() { | ||
it('creates it with an empty array', async function() { | ||
it('does nothing', async function() { | ||
await persistence.initialize() | ||
const json = JSON.parse(await readFile(path)) | ||
expect(githubAuth.getAllTokenIds()).to.deep.equal([]) | ||
}) | ||
|
||
expect(json).to.deep.equal([]) | ||
it('saving creates an empty file', async function() { | ||
await persistence.initialize() | ||
|
||
await persistence.save() | ||
|
||
const json = JSON.parse(await readFile(path)) | ||
expect(json).to.deep.deep.equal([]) | ||
}) | ||
}) | ||
|
||
context('when the file exists', function() { | ||
it('loads the contents', async function() { | ||
const initialTokens = ['a', 'b', 'c'].map(char => char.repeat(40)) | ||
const initialTokens = ['a', 'b', 'c'].map(char => char.repeat(40)) | ||
|
||
beforeEach(async function() { | ||
fs.writeFileSync(path, JSON.stringify(initialTokens)) | ||
}) | ||
|
||
it('loads the contents', async function() { | ||
await persistence.initialize() | ||
|
||
expect(githubAuth.getAllTokenIds()).to.deep.equal(initialTokens) | ||
}) | ||
}) | ||
|
||
context('when shutting down', function() { | ||
it('writes added tokens to the file', async function() { | ||
const initialTokens = ['a', 'b', 'c'].map(char => char.repeat(40)) | ||
fs.writeFileSync(path, JSON.stringify(initialTokens)) | ||
|
||
const newToken = 'e'.repeat(40) | ||
const expected = initialTokens.slice() | ||
expected.push(newToken) | ||
context('when tokens are added', function() { | ||
it('writes them to the file', async function() { | ||
const newToken = 'e'.repeat(40) | ||
const expected = initialTokens.slice() | ||
expected.push(newToken) | ||
|
||
await persistence.initialize() | ||
githubAuth.addGithubToken(newToken) | ||
await persistence.stop() | ||
await persistence.initialize() | ||
githubAuth.addGithubToken(newToken) | ||
await persistence.noteTokenAdded(newToken) | ||
|
||
const json = JSON.parse(await readFile(path)) | ||
expect(json).to.deep.equal(expected) | ||
const json = JSON.parse(await readFile(path)) | ||
expect(json).to.deep.equal(expected) | ||
}) | ||
}) | ||
}) | ||
|
||
context('time has elapsed', function() { | ||
it('writes added tokens to the file', async function() { | ||
const addedTokens = ['d', 'e'].map(char => char.repeat(40)) | ||
context('when tokens are removed', function() { | ||
it('writes them to the file', async function() { | ||
const expected = Array.from(initialTokens) | ||
const toRemove = expected.pop() | ||
|
||
await persistence.initialize() | ||
|
||
addedTokens.forEach(githubAuth.addGithubToken) | ||
await persistence.initialize() | ||
|
||
// Fake time passing to trigger autosaving, then give the save a brief | ||
// moment of real time to complete before reading. | ||
clock.tick(5000) | ||
clock.restore() | ||
await sleep(200) | ||
githubAuth.rmGithubToken(toRemove) | ||
await persistence.noteTokenAdded(toRemove) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah thanks, I caught that in #1939 but forgot to fix it here. |
||
|
||
const json = JSON.parse(await readFile(path)) | ||
expect(json).to.deep.equal(addedTokens) | ||
const json = JSON.parse(await readFile(path)) | ||
expect(json).to.deep.equal(expected) | ||
}) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The listeners aren't actually using the token that is being passed around (as they call
getAllTokenIds
to get the whole list), but it probably doesn't hurt either to include it in the emitted event.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea… and the next iteration of this, with a redis backend, will use that. I believe Redis can do inserts and deletes instead of rewriting the whole thing.