Skip to content

Commit

Permalink
feat: without registry & serverHost in unittest env (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored Nov 6, 2018
1 parent b128369 commit 2499080
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/client/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RpcConsumer extends Base {
constructor(options = {}) {
assert(options.interfaceName, '[RpcConsumer] options.interfaceName is required');
assert(options.logger, '[RpcConsumer] options.logger is required');
assert(options.serverHost || options.registry, '[RpcConsumer] options.registry or options.serverHost at least set one');
assert(options.allowMock || options.serverHost || options.registry, '[RpcConsumer] options.registry or options.serverHost at least set one');
assert(options.connectionManager, '[RpcConsumer] options.connectionManager is required');
super({ initMethod: '_init' });

Expand Down Expand Up @@ -73,13 +73,15 @@ class RpcConsumer extends Base {
if (this.options.serverHost) {
const addressList = this.options.serverHost.split(',').map(url => this.parseUrl(url));
setImmediate(() => { this._addressGroup.addressList = addressList; });
} else {
} else if (this.registry) {
await this.registry.ready();
this._addressGroup = this.createAddressGroup(this.id + '@' + this.group);
this._addressListener = addressList => {
this._addressGroup.addressList = addressList.map(url => this.parseUrl(url));
};
this.registry.subscribe(this.registryConfig, this._addressListener);
} else {
setImmediate(() => { this._addressGroup.addressList = []; });
}
await this._addressGroup.ready();
}
Expand Down
25 changes: 25 additions & 0 deletions test/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,29 @@ describe('test/client/client.test.js', () => {

await client.close();
});

it('should support without registry or serverHost in unittest env', async function() {
const client = new RpcClient({
protocol,
logger,
allowMock: true, // 标识当前支持 mock
});
const consumer = client.createConsumer({
interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
targetAppName: 'pb',
});
await consumer.ready();

const args = [{
name: 'Peter',
group: 'A',
}];
try {
await consumer.invoke('echoObj', args);
assert(false);
} catch (err) {
assert(err.name === 'RpcNoProviderError');
assert(err.message === 'No provider of com.alipay.sofa.rpc.test.ProtoService:1.0@SOFA:echoObj() found!');
}
});
});
1 change: 1 addition & 0 deletions test/client/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ describe('test/client/connection.test.js', () => {
assert(res.error.message.includes('this request is block by circuit breaker, HealthCounts'));

await sleep(connection._circuitBreaker.config.sleepWindowInMilliseconds);
await sleep(10);

res = await connection.invoke(req);
assert.deepEqual(res, { error: null, appResponse: 3, responseProps: null });
Expand Down

0 comments on commit 2499080

Please sign in to comment.