YUKI Hiroshi
null+****@clear*****
Mon Nov 12 17:38:28 JST 2012
YUKI Hiroshi 2012-11-12 17:38:28 +0900 (Mon, 12 Nov 2012) New Revision: 916634225ed8f7866b439e2c08d1dc016b5f8bdd https://github.com/groonga/gcs/commit/916634225ed8f7866b439e2c08d1dc016b5f8bdd Log: Extract core implementation of scenario runner to a library Added files: tools/scenario-runner.js Modified files: tools/run-scenarios Modified: tools/run-scenarios (+40 -105) =================================================================== --- tools/run-scenarios 2012-11-12 16:56:35 +0900 (52c0288) +++ tools/run-scenarios 2012-11-12 17:38:28 +0900 (fd7be3c) @@ -8,9 +8,9 @@ var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface; var Client = require(__dirname + '/../lib/client').Client; +var Runner = require(__dirname + 'scenario-runner').Runner; var fs = require('fs'); var path = require('path'); -var mkdirp = require('mkdirp'); var commandLine = new CLI(); commandLine @@ -34,10 +34,6 @@ commandLine String) .parseClient(); -var client = new Client(commandLine); -if (commandLine.options.documentEndpoint) - client.docEndpoint = commandLine.options.documentEndpoint; - var scenariosDir; var scenarios; @@ -62,6 +58,8 @@ if (commandLine.options.scenario) { scenarios = fs.readdirSync(scenariosDir); scenarios = scenarios.filter(function(file) { return /\.json$/i.test(file) + }).map(function(file) { + return path.resolve(scenariosDir, file); }); if (!scenarios.length) client.raiseFatalError('No scenario file.'); @@ -70,104 +68,41 @@ if (commandLine.options.scenario) { '"--scenarios" option.'); } -var statusCodeTable = { - 404: 'Not Found', - 409: 'Conflict', - 400: 'Bad Request', - 200: 'OK' -}; - -function processScenarios(params) { - if (!params.start) params.start = Date.now(); - var scenarioFile = params.scenarios.shift(); - - console.log(''); - console.log('Scenario file: ' + scenarioFile); - var scenario = fs.readFileSync(path.resolve(scenariosDir, scenarioFile)); - scenario = JSON.parse(scenario); - - var scenarioName = path.basename(scenarioFile, '.json'); - var resultsDir = path.resolve(commandLine.options.outputDirectory, - scenarioName); - mkdirp.sync(resultsDir); - - processScenario( - { name: scenarioName, - requests: scenario, - resultsDir: resultsDir }, - function(error) { - if (scenarios.length) { - processScenarios(params); - } else { - var elapsedTime = Date.now() - params.start; - console.log(''); - console.log('All scenarios are finished. ' + - '(total elapsed time: ' + elapsedTime + 'msec)'); - process.exit(0); +var options = {}; +Object.keys(commandLine.options).forEach(function(key) { + if (typeof commandLine.options[key] != 'function') + options[key] = commandLine.options[key]; +}); +Object.keys(commandLine).forEach(function(key) { + if (typeof commandLine[key] != 'function') + options[key] = commandLine[key]; +}); + +var runner = new Runner(options, function(error, event) { + switch (event.type) { + case 'scenario:start': + console.log(''); + console.log('Scenario file: ' + event.path); + break; + + case 'scenario:finish': + console.log(' Done. (elapsed time: ' + event.elapsedTime + 'msec)'); + break; + + case 'request:start': + console.log(' Processing request: ' + event.name); + break; + + case 'request:write': + console.log(' Wrote ' + event.path); + break; + + case 'all:finish': + console.log(''); + console.log('All scenarios are finished. ' + + '(total elapsed time: ' + event.elapsedTime + 'msec)'); + process.exit(0); + break; } - } - ); -} - -function scenarioNameToFileName(scenarioName) { - return scenarioName - .replace(/[^a-zA-Z0-9]+/g, '-') - .replace(/-$/, '') + '.txt'; -} - -function processScenario(params, callback) { - if (!params.start) params.start = Date.now(); - if (!params.processed) params.processed = {}; - - var request = params.requests.shift(); - - function processNext() { - if (params.requests.length) { - processScenario(params, callback); - } else { - var elapsedTime = Date.now() - params.start; - console.log(' Done. (elapsed time: ' + elapsedTime + 'msec)'); - if (callback) - callback(null); - else - process.exit(0); - } - } - - if (request.onlyGCS && commandLine.options.acs) - return processNext(); - - var name = request.name; - var count = 1; - while (name in params.processed) { - name = request.name + count++; - } - - console.log(' Processing request: ' + name); - - var filename = scenarioNameToFileName(name); - client.rawConfigurationRequest(request.params.Action, request.params, function(error, result) { - var response = error || result; - - var statusCode = response.StatusCode; - if (!statusCodeTable[statusCode]) { - client.raiseFatalError('Unknown status code ' + statusCode); - } - - var output = ''; - output += 'HTTP/1.1 ' + statusCode + ' ' + statusCodeTable[statusCode] + '\r\n'; - for (var key in response.Headers) { - output += key + ': ' + response.Headers[key] + '\r\n'; - }; - output += '\r\n'; - output += response.Body.toString(); - - var resultPath = path.resolve(params.resultsDir, filename); - fs.writeFile(resultPath, output); - console.log(' Wrote ' + resultPath); - - processNext(); - }); -} - -processScenarios({ scenarios: scenarios }); + }); +runner.run(scenarios); Added: tools/scenario-runner.js (+124 -0) 100644 =================================================================== --- /dev/null +++ tools/scenario-runner.js 2012-11-12 17:38:28 +0900 (f410709) @@ -0,0 +1,124 @@ +var Client = require(__dirname + '/../lib/client').Client; +var fs = require('fs'); +var path = require('path'); +var mkdirp = require('mkdirp'); + +var statusCodeTable = { + 404: 'Not Found', + 409: 'Conflict', + 400: 'Bad Request', + 200: 'OK' +}; + +function Runner(options, callback) { + this.client = new Client(options); + this.options = options; + if (options.documentEndpoint) + this.client.docEndpoint = options.documentEndpoint; + this.callback = callback; +} +Runner.prototype = { + run: function(scenarios) { + this.processScenarios({ scenarios: scenarios }); + }, + + processScenarios: function(params) { + if (!params.start) params.start = Date.now(); + var scenarioFile = params.scenarios.shift(); + + this.callback(null, { type: 'scenario:start', path: scenarioFile }); + + var scenario = fs.readFileSync(scenarioFile); + scenario = JSON.parse(scenario); + + var scenarioName = path.basename(scenarioFile, '.json'); + var resultsDir; + if (this.options.outputDirectory) { + resultsDir = path.resolve(this.options.outputDirectory, + scenarioName); + mkdirp.sync(resultsDir); + } + + var self = this; + this.processScenario( + { name: scenarioName, + requests: scenario, + resultsDir: resultsDir }, + function(error) { + if (scenarios.length) { + self.processScenarios(params); + } else { + var elapsedTime = Date.now() - params.start; + self.callback(null, { type: 'all:finish', elapsedTime: elapsedTime }); + } + } + ); + }, + + scenarioNameToFileName: function(scenarioName) { + return scenarioName + .replace(/[^a-zA-Z0-9]+/g, '-') + .replace(/-$/, '') + '.txt'; + }, + + processScenario: function(params, callback) { + if (!params.start) params.start = Date.now(); + if (!params.processed) params.processed = {}; + + var request = params.requests.shift(); + var results = {}; + + var self = this; + function processNext() { + if (params.requests.length) { + self.processScenario(params, callback); + } else { + var elapsedTime = Date.now() - params.start; + self.callback(null, { type: 'scenario:finish', + elapsedTime: elapsedTime, + results: results }); + callback(null); + } + } + + if (request.onlyGCS && this.options.acs) + return processNext(); + + var name = request.name; + var count = 1; + while (name in params.processed) { + name = request.name + count++; + } + + this.callback(null, { type: 'request:start', name: name }); + + var filename = this.scenarioNameToFileName(name); + this.client.rawConfigurationRequest(request.params.Action, request.params, function(error, result) { + var response = error || result; + + var statusCode = response.StatusCode; + if (!statusCodeTable[statusCode]) { + self.callback(null, { type: 'error', statusCode: statusCode }); + return; + } + + var output = ''; + output += 'HTTP/1.1 ' + statusCode + ' ' + statusCodeTable[statusCode] + '\r\n'; + for (var key in response.Headers) { + output += key + ': ' + response.Headers[key] + '\r\n'; + }; + output += '\r\n'; + output += response.Body.toString(); + + results[name] = output; + if (params.resultsDir) { + var resultPath = path.resolve(params.resultsDir, filename); + fs.writeFile(resultPath, output); + self.callback(null, { type: 'request:write', path: resultPath }); + } + + processNext(); + }); + } +}; +exports.Runner = Runner; -------------- next part -------------- HTML����������������������������... Descargar