2015-09-30 41 views
0

我創建了一個簡單的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." }); 
     } 
+0

諾言實際上是解決了嗎?它可能是一個不同的代碼路徑返回200響應?正在返回什麼樣的結果?空白? –

+0

承諾永遠無法解決,因爲每次節點服務器響應200時,大約120秒後都會有空白響應。 – jre247

+0

更新:我創建了另一個節點服務,只有代碼爲setTimeout 121秒,然後返回響應。正如所料,節點服務在120秒後返回空響應。任何想法? – jre247

回答

1

從這篇文章:如果您使用快遞

,你可以使用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 

你也應該考慮不要做長時間的請求,而是做一個開始/狀態模式。由於網絡問題,這個失敗的可能性要小得多。

+0

你可以把我鏈接到一篇關於開始/狀態模式的文章嗎?謝謝! – jre247

+0

它被稱爲構建異步API。對於我的生活,我無法找到關於Node的博客。如果你只是建立一個內部應用程序,也許你不在乎打擾。基本上,你只需調用/ start api,它就會啓動Nightwatch命令。/status api將返回承諾的當前狀態(未完成/已完成+結果)。您的C#代碼然後再次輪詢。 –