-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: enhance elastic control of connections (#34)
- Loading branch information
Showing
7 changed files
with
113 additions
and
12 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 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 |
---|---|---|
|
@@ -37,6 +37,7 @@ class RpcRequest { | |
responseDecodeRT: 0, | ||
resSize: 0, | ||
rt: null, | ||
error: null, | ||
}; | ||
} | ||
} | ||
|
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,64 @@ | ||
'use strict'; | ||
|
||
const mm = require('mm'); | ||
const assert = require('assert'); | ||
const urlparse = require('url').parse; | ||
const utils = require('../../lib/client/utils'); | ||
const MockConnection = require('../fixtures/mock_connection'); | ||
const AddressGroup = require('../../lib/client/address_group'); | ||
const ConnectionManager = require('../../lib/client/connection_mgr'); | ||
|
||
const logger = console; | ||
|
||
describe('test/client/elastic_control.test.js', () => { | ||
let connectionManager; | ||
let addressGroup; | ||
const count = 10; | ||
|
||
const addressList = []; | ||
|
||
before(async () => { | ||
connectionManager = new ConnectionManager({ logger }); | ||
await connectionManager.ready(); | ||
|
||
for (let i = 0; i < count; i++) { | ||
const address = urlparse(`tr://127.0.0.${i}:12200`); | ||
addressList.push(address); | ||
MockConnection.addAvailableAddress(address); | ||
} | ||
|
||
addressGroup = new AddressGroup({ | ||
key: 'xxx', | ||
logger, | ||
connectionManager, | ||
connectionClass: MockConnection, | ||
retryFaultInterval: 5000, | ||
}); | ||
addressGroup.connectionPoolSize = 2; | ||
addressGroup.addressList = addressList; | ||
|
||
await addressGroup.ready(); | ||
}); | ||
|
||
afterEach(mm.restore); | ||
|
||
after(async () => { | ||
MockConnection.clearAvailableAddress(); | ||
addressGroup.close(); | ||
await connectionManager.closeAllConnections(); | ||
}); | ||
|
||
it('should use new address', () => { | ||
assert(addressGroup.addressList.length === 2); | ||
mm(utils, 'shuffle', arr => arr); | ||
|
||
const newAddress = urlparse('rpc://127.0.0.11:12200'); | ||
MockConnection.addAvailableAddress(newAddress); | ||
|
||
const newAddressList = [ newAddress ].concat(addressList); | ||
addressGroup.addressList = newAddressList; | ||
|
||
assert(addressGroup.addressList.length === 2); | ||
assert(addressGroup.addressList[0].href === 'rpc://127.0.0.11:12200'); | ||
}); | ||
}); |