-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[testUtils/esTestCluster] use more standard api style (#13197)
(cherry picked from commit d36080b)
- Loading branch information
Showing
5 changed files
with
80 additions
and
84 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
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 |
---|---|---|
@@ -1,103 +1,99 @@ | ||
import { resolve } from 'path'; | ||
import { esTestConfig } from './es_test_config'; | ||
|
||
import libesvm from 'libesvm'; | ||
|
||
import { esTestConfig } from './es_test_config'; | ||
|
||
const ESVM_DIR = resolve(__dirname, '../../../esvm/test_utils/es_test_cluster'); | ||
const BRANCHES_DOWNLOADED = []; | ||
|
||
export class EsTestCluster { | ||
_branchesDownloaded = []; | ||
function isDownloadNeeded(branch) { | ||
if (process.env.ESVM_NO_FRESH || process.argv.includes('--esvm-no-fresh')) { | ||
return false; | ||
} | ||
|
||
use(options = {}) { | ||
const { | ||
name, | ||
log = console.log, | ||
port = esTestConfig.getPort(), | ||
branch = esTestConfig.getBranch(), | ||
} = options; | ||
if (BRANCHES_DOWNLOADED.includes(branch)) { | ||
return false; | ||
} | ||
|
||
if (!name) { | ||
throw new Error('esTestCluster.use() requires { name }'); | ||
} | ||
return true; | ||
} | ||
|
||
// assigned in use.start(), reassigned in use.stop() | ||
let cluster; | ||
export function createEsTestCluster(options = {}) { | ||
const { | ||
name, | ||
log = console.log, | ||
port = esTestConfig.getPort(), | ||
branch = esTestConfig.getBranch(), | ||
} = options; | ||
|
||
return { | ||
getStartTimeout: () => { | ||
return esTestConfig.getLibesvmStartTimeout(); | ||
}, | ||
if (!name) { | ||
throw new Error('createEsTestCluster() requires { name }'); | ||
} | ||
|
||
start: async () => { | ||
const download = this._isDownloadNeeded(branch); | ||
// assigned in use.start(), reassigned in use.stop() | ||
let cluster; | ||
|
||
if (cluster) { | ||
throw new Error(` | ||
EsTestCluster[${name}] is already started, call and await es.stop() | ||
before calling es.start() again. | ||
`); | ||
} | ||
return new class EsTestCluster { | ||
getStartTimeout() { | ||
return esTestConfig.getLibesvmStartTimeout(); | ||
} | ||
|
||
async start() { | ||
const download = isDownloadNeeded(branch); | ||
|
||
cluster = libesvm.createCluster({ | ||
fresh: download, | ||
purge: !download, | ||
directory: ESVM_DIR, | ||
branch, | ||
config: { | ||
http: { | ||
port, | ||
}, | ||
cluster: { | ||
name, | ||
}, | ||
discovery: { | ||
zen: { | ||
ping: { | ||
unicast: { | ||
hosts: [ `localhost:${port}` ] | ||
} | ||
if (cluster) { | ||
throw new Error(` | ||
EsTestCluster[${name}] is already started, call and await es.stop() | ||
before calling es.start() again. | ||
`); | ||
} | ||
|
||
cluster = libesvm.createCluster({ | ||
fresh: download, | ||
purge: !download, | ||
directory: ESVM_DIR, | ||
branch, | ||
config: { | ||
http: { | ||
port, | ||
}, | ||
cluster: { | ||
name, | ||
}, | ||
discovery: { | ||
zen: { | ||
ping: { | ||
unicast: { | ||
hosts: [ `localhost:${port}` ] | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
cluster.on('log', (event) => { | ||
log(`EsTestCluster[${name}]: ${event.type} - ${event.message}`); | ||
}); | ||
|
||
await cluster.install(); | ||
|
||
if (download) { | ||
// track the branches that have successfully downloaded | ||
// after cluster.install() resolves | ||
this._branchesDownloaded.push(branch); | ||
} | ||
}); | ||
|
||
await cluster.start(); | ||
}, | ||
cluster.on('log', (event) => { | ||
log(`EsTestCluster[${name}]: ${event.type} - ${event.message}`); | ||
}); | ||
|
||
stop: async () => { | ||
if (cluster) { | ||
const c = cluster; | ||
cluster = null; | ||
await c.shutdown(); | ||
} | ||
await cluster.install(); | ||
|
||
if (download) { | ||
// track the branches that have successfully downloaded | ||
// after cluster.install() resolves | ||
BRANCHES_DOWNLOADED.push(branch); | ||
} | ||
}; | ||
} | ||
|
||
_isDownloadNeeded(branch) { | ||
if (process.env.ESVM_NO_FRESH || process.argv.includes('--esvm-no-fresh')) { | ||
return false; | ||
await cluster.start(); | ||
} | ||
|
||
if (this._branchesDownloaded.includes(branch)) { | ||
return false; | ||
async stop() { | ||
if (cluster) { | ||
const c = cluster; | ||
cluster = null; | ||
await c.shutdown(); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
} | ||
|
||
export const esTestCluster = new EsTestCluster(); |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { esTestCluster } from './es_test_cluster'; | ||
export { esTestConfig } from './es_test_config'; | ||
export { createEsTestCluster } from './es_test_cluster'; |