我創建了一個簡單的Node.js快速服務,基本上使用child_process.exec來運行一個命令(在這種情況下,在命令提示符下運行「守夜」命令來運行Nightwatch .js端到端測試),然後一旦Nightwatch測試運行完畢,然後從Nightwatch輸出結果的特定html文件中讀取測試的報告結果。快速服務然後將HTML文本返回給提出請求的.NET Web應用程序。快速Node.js服務返回響應太早
我遇到了一個錯誤,在每次向.net c#webapp發出請求時,Express服務會在120000毫秒之後提前返回200響應。
任何想法爲什麼快遞服務提前返回200響應?我以爲這是一個超時問題,所以我在.net Web應用程序中設置了Web請求超時,但仍然遇到同樣的問題。快遞服務本身可能需要設置超時時間,這是否有問題?
快速服務代碼:
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;
}
.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." });
}
諾言實際上是解決了嗎?它可能是一個不同的代碼路徑返回200響應?正在返回什麼樣的結果?空白? –
承諾永遠無法解決,因爲每次節點服務器響應200時,大約120秒後都會有空白響應。 – jre247
更新:我創建了另一個節點服務,只有代碼爲setTimeout 121秒,然後返回響應。正如所料,節點服務在120秒後返回空響應。任何想法? – jre247