-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If subscribe listener not be called before subscribeTimeout, ApiClient will emit timeout error.
- Loading branch information
Showing
17 changed files
with
180 additions
and
51 deletions.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
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,48 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const sleep = require('mz-modules/sleep'); | ||
const ApiClient = require('./supports/sub_timeout/api_client'); | ||
|
||
describe('test/subscrib.test.js', () => { | ||
describe('timeout case', () => { | ||
it('should timeout', async () => { | ||
const leader = new ApiClient({ | ||
singleMode: false, | ||
isLeader: true, | ||
}); | ||
const follower = new ApiClient({ | ||
clusterOptions: { | ||
subscribeTimeout: 1000, | ||
}, | ||
singleMode: false, | ||
isLeader: false, | ||
}); | ||
await follower.ready(); | ||
const errors = []; | ||
const values = []; | ||
follower.on('error', err => { | ||
errors.push(err); | ||
}); | ||
follower.subscribe({ | ||
key: 'timeout:500', | ||
}, value => { | ||
values.push(value); | ||
}); | ||
follower.subscribe({ | ||
key: 'timeout:1500', | ||
}, value => { | ||
values.push(value); | ||
}); | ||
await sleep(2000); | ||
assert.deepStrictEqual(values, [ | ||
'hello:500', | ||
'hello:1500', | ||
]); | ||
assert(errors.length === 1); | ||
assert(/subscribe timeout for/.test(errors[0])); | ||
await follower.close(); | ||
await leader.close(); | ||
}); | ||
}); | ||
}); |
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,40 @@ | ||
'use strict'; | ||
|
||
const DataClient = require('./data_client'); | ||
const APIClientBase = require('../../../lib/api_client'); | ||
|
||
class ApiClient extends APIClientBase { | ||
constructor(options = {}) { | ||
super(Object.assign({}, options, { initMethod: '_init' })); | ||
} | ||
|
||
get DataClient() { | ||
return DataClient; | ||
} | ||
|
||
get clusterOptions() { | ||
return { | ||
port: parseInt(process.env.NODE_CLUSTER_CLIENT_PORT || 7777), | ||
singleMode: false, | ||
subscribeTimeout: 1000, | ||
}; | ||
} | ||
|
||
async _init() { | ||
await this._client.ready(); | ||
} | ||
|
||
subscribe(config, listener) { | ||
this._client.subscribe(config, listener); | ||
} | ||
|
||
publish(reg) { | ||
this._client.publish(reg); | ||
} | ||
|
||
close() { | ||
return this._client.close(); | ||
} | ||
} | ||
|
||
module.exports = ApiClient; |
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,29 @@ | ||
'use strict'; | ||
|
||
const Base = require('sdk-base'); | ||
|
||
class DataClient extends Base { | ||
constructor(options = {}) { | ||
super(options); | ||
} | ||
|
||
async _init() { | ||
// ... | ||
} | ||
|
||
subscribe(reg, listener) { | ||
const { key } = reg; | ||
const match = key.match(/timeout:(\d+)/); | ||
if (!match) { | ||
throw new Error('not a timeout key'); | ||
} | ||
setTimeout(() => { | ||
listener('hello:' + match[1]); | ||
}, +match[1]); | ||
} | ||
|
||
close() { | ||
} | ||
} | ||
|
||
module.exports = DataClient; |