我创建了一个简单的Node.js Express服务,它基本上使用child_process.exec来运行一个命令(在这种情况下,在命令提示符下运行“nightwatch”命令来运行Nightwatch.js端到端测试),以及然后,一旦Nightwatch测试运行完毕,然后从Nightwatch输出结果的特定html文件中读取测试的报告结果。然后,Express服务将html文本返回给发出请求的.NET Web应用程序。
我遇到一个错误,每当.net c#webapp向它发出请求时,Express服务在120000 ms之后过早地返回200响应。
知道为什么快递服务过早地返回200响应?我以为这是一个超时问题,所以我在.net网络应用程序中设置了一个Web请求超时,但仍然遇到了同样的问题。是否可能需要在Express服务中设置超时?
快递服务代码:
var exec = require('child_process').exec;
var fs = require('fs');
var Q = require('Q');
exports.runTests = function(req, res){
var environment = req.param('environment');
console.log('current environment to run nightwatch tests: ' + environment);
executeNightwatchCommand(environment)
return getReportResults(res).then(function(reportHtmlFormatted){
console.log('About to send back response of report html.');
res.send(reportHtmlFormatted);
});
};
var executeNightwatchCommand = function(environment){
//var nightwatchDirectory = 'C:/Nightwatch/Brightline.AcceptanceTesting';
var nightwatchDirectory = 'C:/code/project/brightline/tests/Brightline.AcceptanceTesting';
if(environment.toLowerCase() == 'local'){
environment = 'develop';
}
var cmd = 'cmd /c "cd /d ' + nightwatchDirectory + ' && nightwatch --env ' + environment + '"';
console.log("About to run Nightwatch tests.");
exec(cmd, function(error, stdout, stderr) {
if(error){
console.log("error: " + error);
}
// if(stdout){
// console.log("stdout: " + stdout);
// }
if(stderr){
console.log("stderr: " + stderr);
}
console.log("Finished running Nightwatch tests.");
});
}
var getReportResults = function(res, deferred){
var deferred = Q.defer();
//var reportPath = 'C:/Nightwatch/Brightline.AcceptanceTesting/reports_html/report.html';
var reportPath = 'C:/code/project/brightline/tests/Brightline.AcceptanceTesting/reports_html/report.html';
console.log('Setting up filesystem watch for report file: ' + reportPath);
fs.watch(reportPath, function(){
fs.readFile(reportPath, 'utf8', function (err,data) {
console.log('currently reading report file.');
if (err) {
console.log("error: " + err);
deferred.reject(new Error(err));
}
//remove style from report html so it doesn't override website styles
var reportHtml = data;
var reportHtmlFormatted = reportHtml.split('<style type="text/css">')[0] + reportHtml.split('</style>')[1];
console.log('About to resolve promise for reading report html.')
deferred.resolve(reportHtmlFormatted);
});
});
return deferred.promise;
}
向Express服务发出请求的.NET C#代码:
string environment = null;
try
{
if (IoC.Settings.CurrentEnvironment == EnvironmentType.PRO)
environment = "production";
else if (IoC.Settings.CurrentEnvironment == EnvironmentType.UAT)
environment = "uat";
else if (IoC.Settings.CurrentEnvironment == EnvironmentType.DEV)
environment = "develop";
else
environment = "local";
var buildServerIp = ConfigurationManager.AppSettings["buildServerIp"];
var nightwatchServicePort = ConfigurationManager.AppSettings["nightwatchServicePort"];
//var requestUrl = string.Format("http://{0}:{1}/nightwatchTests?environment={2}", buildServerIp, nightwatchServicePort, environment);
var requestUrl = string.Format("http://{0}:{1}/nightwatchTests?environment={2}", "localhost", nightwatchServicePort, environment);
var request = WebRequest.Create(requestUrl);
request.Method = "GET";
request.Timeout = 1000000000;
string text;
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
var vm = new NightwatchTestsViewModel();
vm.html = text;
return JObject.FromObject(vm);
}
catch (Exception ex)
{
IoC.Log.Error("Could not retrieve Nightwatch test results.", ex);
FlashMessageExtensions.Debug(ex);
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "Error processing request." });
}
答案 0 :(得分:1)
从这篇文章:
如果您正在使用express,则可以使用server.timeout功能。
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
server.timeout = 1000; //Timeout requests after 1 second
您还应该考虑不执行长请求,而是执行启动/状态模式。由于网络问题,这不太可能失败。