Yoji SHIDARA
null+****@clear*****
Wed Nov 14 12:58:18 JST 2012
Yoji SHIDARA 2012-11-14 12:58:18 +0900 (Wed, 14 Nov 2012) New Revision: 28a8e19390c0822b8266d2638d630801cf1f5428 https://github.com/groonga/gcs/commit/28a8e19390c0822b8266d2638d630801cf1f5428 Log: Make runner to be an EventEmitter Modified files: tools/scenario-runner.js Modified: tools/scenario-runner.js (+89 -85) =================================================================== --- tools/scenario-runner.js 2012-11-13 16:11:31 +0900 (0d512ed) +++ tools/scenario-runner.js 2012-11-14 12:58:18 +0900 (d0bedb5) @@ -1,4 +1,5 @@ var Client = require(__dirname + '/../lib/client').Client; +var EventEmitter = require('events').EventEmitter; var statusCodeTable = { 500: 'Inetnal Server Error', @@ -14,110 +15,113 @@ function Runner(options) { if (options.documentEndpoint) this.client.docEndpoint = options.documentEndpoint; } -Runner.prototype = { - run: function(scenario, callback) { - if (!Array.isArray(scenario)) - this._processScenario(scenario, callback); - else - this._processScenarios({ scenarios: scenario }, callback); - }, - - _processScenarios: function(params, globalCallback) { - if (!params.start) params.start = Date.now(); - var scenario = params.scenarios.shift(); - var self = this; - if (globalCallback) - this.globalCallback = globalCallback; - this._processScenario( - scenario, - function(error, event) { - if (params.scenarios.length) { - self._processScenarios(params); - } else { - var elapsedTime = Date.now() - params.start; - if (self.globalCallback) - self.globalCallback(null, { type: 'all:finish', - elapsedTime: elapsedTime }); - } - } - ); - }, - - _processScenario: function(scenario, callback) { - if (!scenario.toBeProcessedRequests) { - scenario.toBeProcessedRequests = scenario.requests.slice(0); - scenario.start = Date.now(); - scenario.processed = {}; - if (this.globalCallback) - this.globalCallback(null, { type: 'scenario:start', - scenario: scenario }); - } - var request = scenario.toBeProcessedRequests.shift(); - var self = this; - function processNext() { - if (scenario.toBeProcessedRequests.length) { - self._processScenario(scenario, callback); +Runner.prototype = new EventEmitter(); + +Runner.prototype.run = function(scenario, callback) { + if (!Array.isArray(scenario)) + this._processScenario(scenario, callback); + else + this._processScenarios({ scenarios: scenario }, callback); +}; + +Runner.prototype._processScenarios = function(params, globalCallback) { + if (!params.start) params.start = Date.now(); + var scenario = params.scenarios.shift(); + var self = this; + if (globalCallback) + this.globalCallback = globalCallback; + this._processScenario( + scenario, + function(error, event) { + if (params.scenarios.length) { + self._processScenarios(params); } else { - var elapsedTime = Date.now() - scenario.start; - var event = { type: 'scenario:finish', - elapsedTime: elapsedTime, - scenario: scenario }; + var elapsedTime = Date.now() - params.start; if (self.globalCallback) - self.globalCallback(null, event); - if (callback) - callback(null, event); + self.globalCallback(null, { type: 'all:finish', + elapsedTime: elapsedTime }); } } + ); +}; - if (request.onlyGCS && this.options.acs) - return processNext(); +Runner.prototype._processScenario = function(scenario, callback) { + if (!scenario.toBeProcessedRequests) { + scenario.toBeProcessedRequests = scenario.requests.slice(0); + scenario.start = Date.now(); + scenario.processed = {}; + if (this.globalCallback) + this.globalCallback(null, { type: 'scenario:start', + scenario: scenario }); + } - var name = request.name; - var count = 1; - while (name in scenario.processed) { - name = request.name + count++; + var request = scenario.toBeProcessedRequests.shift(); + var self = this; + function processNext() { + if (scenario.toBeProcessedRequests.length) { + self._processScenario(scenario, callback); + } else { + var elapsedTime = Date.now() - scenario.start; + var event = { type: 'scenario:finish', + elapsedTime: elapsedTime, + scenario: scenario }; + if (self.globalCallback) + self.globalCallback(null, event); + if (callback) + callback(null, event); } + } - if (this.globalCallback) - this.globalCallback(null, { type: 'request:start', - scenario: scenario, - request: request }); + if (request.onlyGCS && this.options.acs) + return processNext(); - this.client.rawConfigurationRequest(request.params.Action, request.params, function(error, result) { - var response = error || result; + var name = request.name; + var count = 1; + while (name in scenario.processed) { + name = request.name + count++; + } - var statusCode = response.StatusCode; - if (!statusCodeTable[statusCode]) { - if (self.globalCallback) - self.globalCallback(null, { type: 'error', - statusCode: statusCode }); - if (callback) - callback(statusCode, null); - return; - } + if (this.globalCallback) + this.globalCallback(null, { type: 'request:start', + scenario: scenario, + request: request }); - 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(); + this.client.rawConfigurationRequest(request.params.Action, request.params, function(error, result) { + var response = error || result; - request.result = output; + var statusCode = response.StatusCode; + if (!statusCodeTable[statusCode]) { if (self.globalCallback) - self.globalCallback(null, { type: 'request:finish', - scenario: scenario, - request: request }); + self.globalCallback(null, { type: 'error', + statusCode: statusCode }); + if (callback) + callback(statusCode, null); + return; + } - processNext(); - }); - } + 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(); + + request.result = output; + if (self.globalCallback) + self.globalCallback(null, { type: 'request:finish', + scenario: scenario, + request: request }); + + processNext(); + }); }; + Runner.toSafeName = function(name) { return name .replace(/[^a-zA-Z0-9]+/g, '-') .replace(/-$/, ''); }; + exports.Runner = Runner; -------------- next part -------------- HTML����������������������������...ダウンロード