-
Notifications
You must be signed in to change notification settings - Fork 3
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
locking/unlocking #3
Open
stelcheck
wants to merge
1
commit into
mage:master
Choose a base branch
from
stelcheck:features/locking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
import { TestTopic } from './' | ||
import * as assert from 'assert' | ||
import * as mage from 'mage' | ||
|
||
let state: mage.core.IState | ||
|
||
describe('lock, unlock and isLocked', function () { | ||
let RealState: mage.core.IState | ||
|
||
beforeEach(() => { | ||
state = new mage.core.State() | ||
RealState = <any> mage.core.State | ||
}) | ||
|
||
afterEach(() => { | ||
mage.core.State = <any> RealState | ||
}) | ||
|
||
it('lock will throw if state.archivist.get.fails', async () => { | ||
const id = '12132341233' | ||
const message = 'fails' | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
get(_topic: string, _index: any, _cfg: any, cb: any) { | ||
cb(new Error(message)) | ||
} | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
try { | ||
await topic.isLocked() | ||
} catch (error) { | ||
return assert.equal(error.message, message) | ||
} | ||
|
||
|
||
throw new Error('did not fail') | ||
}) | ||
|
||
it('isLocked checks if a key is locked', async () => { | ||
const id = '12132341233' | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
get(topic: string, index: any, cfg: any, cb: any) { | ||
assert.equal(topic, 'TestTopic') | ||
assert.equal(index.id, id) | ||
assert.equal(index.mageValidatorLock, 'locked') | ||
assert.equal(cfg.optional, true) | ||
|
||
cb(null, 'locked') | ||
} | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
const isLocked = await topic.isLocked() | ||
|
||
assert.equal(isLocked, true) | ||
}) | ||
|
||
it('isLocked can take a state as a parameter', async () => { | ||
const id = '12132341233' | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
get(topic: string, index: any, cfg: any, cb: any) { | ||
assert.equal(topic, 'TestTopic') | ||
assert.equal(index.id, id) | ||
assert.equal(index.mageValidatorLock, 'locked') | ||
assert.equal(cfg.optional, true) | ||
|
||
cb(null, 'locked') | ||
} | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
const isLocked = await topic.isLocked(new mage.core.State()) | ||
|
||
assert.equal(isLocked, true) | ||
}) | ||
|
||
it('lock will throw if it fails to get the lock', async () => { | ||
const id = '12132341233' | ||
const message = 'fails' | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
get(_topic: string, _index: any, _cfg: any, cb: any) { | ||
cb(new Error(message)) | ||
} | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
try { | ||
await topic.lock() | ||
} catch (error) { | ||
return assert.equal(error.message, message) | ||
} | ||
|
||
|
||
throw new Error('did not fail') | ||
}) | ||
|
||
it('lock will throw if the topic is locked', async () => { | ||
const id = '12132341233' | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
get(topic: string, index: any, cfg: any, cb: any) { | ||
assert.equal(topic, 'TestTopic') | ||
assert.equal(index.id, id) | ||
assert.equal(index.mageValidatorLock, 'locked') | ||
assert.equal(cfg.optional, true) | ||
|
||
return cb(null, 'locked') | ||
} | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
try { | ||
await topic.lock() | ||
} catch (error) { | ||
return assert.equal(error.message, ('Topic is locked')) | ||
} | ||
|
||
|
||
throw new Error('did not fail') | ||
}) | ||
|
||
it('lock will lock, and set autoUnlock operation on the topic state', async () => { | ||
const id = '1' | ||
let wasDistributeCalled = false | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
get(_topic: string, _index: any, _cfg: any, cb: any) { | ||
return cb() | ||
}, | ||
set(topic: string, index: any, data: any) { | ||
assert.equal(topic, 'TestTopic') | ||
assert.equal(index.id, id) | ||
assert.equal(data, 'locked') | ||
} | ||
} | ||
|
||
/* tslint:disable:prefer-function-over-method */ | ||
public distribute(cb: (error?: Error) => void) { | ||
wasDistributeCalled = true | ||
cb() | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
await topic.lock() | ||
|
||
assert(wasDistributeCalled) | ||
|
||
const loaded = (<any> state.archivist).loaded.TestTopicoids1omageValidatorLockslocked | ||
assert(loaded) | ||
assert.equal(loaded.operation, 'del') | ||
assert.equal(loaded.topic, 'TestTopic') | ||
assert.deepEqual(loaded.index, { | ||
id, | ||
mageValidatorLock: 'locked' | ||
}) | ||
}) | ||
|
||
it('lock will not autoUnlock if autoUnlock is set to false', async () => { | ||
const id = '1' | ||
let wasDistributeCalled = false | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
get(_topic: string, _index: any, _cfg: any, cb: any) { | ||
return cb() | ||
}, | ||
set(topic: string, index: any, data: any) { | ||
assert.equal(topic, 'TestTopic') | ||
assert.equal(index.id, id) | ||
assert.equal(data, 'locked') | ||
} | ||
} | ||
|
||
/* tslint:disable:prefer-function-over-method */ | ||
public distribute(cb: (error?: Error) => void) { | ||
wasDistributeCalled = true | ||
cb() | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
await topic.lock(false) | ||
|
||
assert(wasDistributeCalled) | ||
|
||
const loaded = (<any> state.archivist).loaded.TestTopicoids1omageValidatorLockslocked | ||
assert.equal(loaded, undefined) | ||
}) | ||
|
||
it('lock will fail if it cannot distribute the lock', async () => { | ||
const id = '1' | ||
const message = 'oh maimai' | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
get(_topic: string, _index: any, _cfg: any, cb: any) { | ||
return cb() | ||
}, | ||
set(topic: string, index: any, data: any) { | ||
assert.equal(topic, 'TestTopic') | ||
assert.equal(index.id, id) | ||
assert.equal(data, 'locked') | ||
} | ||
} | ||
|
||
/* tslint:disable:prefer-function-over-method */ | ||
public distribute(cb: (error?: Error) => void) { | ||
cb(new Error(message)) | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
try { | ||
await topic.lock() | ||
} catch (error) { | ||
return assert.equal(error.message, message) | ||
} | ||
|
||
throw new Error('did not fail') | ||
}) | ||
|
||
it('unlock deletes the lock immediately', async () => { | ||
const id = '1' | ||
let wasDelCalled = false | ||
let wasDistributeCalled = false | ||
|
||
mage.core.State = <any> class { | ||
public archivist = { | ||
del(topic: string, index: any) { | ||
wasDelCalled = true | ||
assert.equal(topic, 'TestTopic') | ||
assert.equal(index.id, id) | ||
} | ||
} | ||
|
||
/* tslint:disable:prefer-function-over-method */ | ||
public distribute(cb: (error?: Error) => void) { | ||
wasDistributeCalled = true | ||
cb() | ||
} | ||
} | ||
|
||
const topic = await TestTopic.create(state, { id }) | ||
await topic.unlock() | ||
|
||
assert(wasDelCalled) | ||
assert(wasDistributeCalled) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure about TypeScript, but in ES (afaik) if you return a Promise, you should not mark your function
async
.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.
It doesn't matter: if you return a promise on an async call, then that promise will be the promise to be returned.
tslint
is currently configured to request that functions returning promises or otherwise usingawait
require theasync
keyword. I believe this is for readability purposes (e.g. when checking the function signature), but also to make sure that whatever you return is a promise.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.
Do you know if that is true for ES as well?
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.
Yes.