private static void ValidateProcessOutput(Process process)
        {
            var logReader     = new Log4NetReader(process.StandardOutput);
            var errorMessages = new StringBuilder();

            foreach (var errorMessage in logReader.GetErrors())
            {
                errorMessages.AppendLine(errorMessage);
            }

            if (errorMessages.Length > 0)
            {
                throw new Exception(errorMessages.ToString());
            }
        }
Esempio n. 2
0
        public void ShouldWriteCsvReports()
        {
            try
            {
                ReportOptions.WriteToConfig(_reportOptions, _hostFileName);
                if (Directory.Exists(_outputDirectory))
                {
                    Directory.Delete(_outputDirectory, true);
                }
                if (File.Exists(_logFileName))
                {
                    File.Delete(_logFileName);
                }

                _serviceRunner.InstallService();

                var services = ServiceController.GetServices();
                var nextTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(_timeZone));

                var service = services.FirstOrDefault(x => x.ServiceName == _serviceName);
                service.Start();
                Thread.Sleep(TimeSpan.FromSeconds(120));
                service.Stop();

                foreach (var filePath in Directory.Exists(_outputDirectory) ? Directory.EnumerateFiles(_outputDirectory) : new string[0])
                {
                    var fileName      = Path.GetFileName(filePath);
                    var fileNameMatch = _csvFileNameRegex.Match(fileName);
                    Assert.IsTrue(fileNameMatch.Success, $"File name {fileName} is incorrect");

                    var reportDate = DateTime.ParseExact(fileNameMatch.Groups[1].Value, "yyyyMMdd_HHmmss", CultureInfo.InvariantCulture);

                    while ((reportDate - nextTime).Duration() > _reportTimeDeviation)
                    {
                        nextTime += _reportInterval;

                        if (nextTime > reportDate + _reportInterval + _reportTimeDeviation)
                        {
                            Assert.Fail($"Report {fileName} has incorrect time.");
                        }
                    }

                    var regex       = new Regex(string.Format(_csvContentRegex, reportDate));
                    var fileContent = File.ReadAllText(filePath);
                    Assert.IsTrue(regex.IsMatch(fileContent), $"File content {fileName} is incorrect");
                }

                Assert.IsTrue(File.Exists(_logFileName), "Log file was not written");

                using (var logReader = new Log4NetReader(_logFileName))
                {
                    var errorMessages = new StringBuilder();
                    foreach (var error in logReader.GetErrors())
                    {
                        if (!error.EndsWith("TradingPlatform.TradingServiceException."))
                        {
                            errorMessages.AppendLine(error);
                        }
                    }
                    if (errorMessages.Length > 0)
                    {
                        Assert.Fail(errorMessages.ToString());
                    }
                }
            }
            finally
            {
                _serviceRunner.UninstallService();
            }
        }