Ejemplo n.º 1
0
        public async void ServiceTest_WCFAndCalculationAndWatcherAndCommand(int timeOut)
        {
            string folderName = @"D:\test3";
            FileQueue fileQueue = new FileQueue();
            using (FileWatcher fileWatcher = new FileWatcher(fileQueue))
            {
                if (System.IO.Directory.Exists(folderName))
                    System.IO.Directory.Delete(folderName, true);

                System.IO.Directory.CreateDirectory(folderName);
                if (!System.IO.Directory.Exists(folderName + @"\logs"))
                    System.IO.Directory.CreateDirectory(folderName + @"\logs");

                System.IO.File.WriteAllText(string.Format(@"{0}\start.bat", folderName), "@echo [+] test %1 completed");

                fileWatcher.Start(new FileWatcherParam[] { new FileWatcherParam(folderName, "*.d??", string.Format("\"{0}\\start.bat\" ??", folderName), "utf-8") });

                for (int i = 0; i < 3; i++)
                    System.IO.File.WriteAllText(string.Format(@"{0}\test.d{1}", folderName, i.ToString("00")), "test");

                Thread.Sleep(1000);

                Assert.AreEqual(3, fileQueue.Count);

                var virtualParameters = new VirtualParameters();
                virtualParameters.SetParameter("CalculationLogs", string.Format(@"{0}\logs", folderName));
                virtualParameters.SetParameter("ProcessTimeout", "10");
                virtualParameters.SetParameter("AutoStart", "0");
                int exceptionsCount = 0;
                using (var fileCalculation = new FileCalculation(fileQueue, virtualParameters))
                {
                    var logFiles = new List<string>();

                    fileCalculation.Log += (s, e) =>
                    {
                        if (e != null)
                        {
                            exceptionsCount++;
                            Assert.Fail("[ServiceTest] exception: " + e.Message);
                        }
                    };

                    fileCalculation.InfoChanged += (s, e) =>
                    {
                        if (e == null)
                        {
                            Debug.WriteLine("[ServiceTest] file calculation end");
                        }
                        else
                        {
                            logFiles.Add(System.IO.Path.Combine(virtualParameters.GetParameter("CalculationLogs") ?? string.Empty, e.Id + ".log"));
                            Debug.WriteLine(string.Format("[ServiceTest] file calculation: '{0}'|status:'{1}'", e.File, e.Status));
                        }
                    };
                    //fileCalculation.Start();

                    var serviceData = new ServiceData() 
                    {
                        Queue = fileQueue,
                        Watcher = fileWatcher,
                        Calculation = fileCalculation,
                        Parameters = virtualParameters 
                    };
                    CalculationManagerService.ServiceInstance = serviceData;

                    //http://WS27SYCHSS.OFFICE.GTT.GAZPROM.RU:9929/CalculationManager/ControlService
                    //net.tcp://WS27SYCHSS.OFFICE.GTT.GAZPROM.RU:9929/CalculationManager/ControlService
                    var hostName = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).HostName;
                    using (ServiceHost srvHost = CreateServiceHost(typeof(ControlService), typeof(IControlService), new Uri(string.Format("net.tcp://{0}:9929/CalculationManager/ControlService", hostName), UriKind.Absolute)))
                    {
                        srvHost.Open();
                        //Thread.Sleep(10 * 60 * 1000);
                        await Task.Delay(timeOut);
                    }

                    Assert.AreEqual(false, fileCalculation.IsActive, "Поток подсчёта уже должен быть завершен");
                }

                if (System.IO.Directory.Exists(folderName + @"\logs"))
                    foreach (string fl in System.IO.Directory.GetFiles(folderName + @"\logs"))
                        Debug.WriteLine(string.Format("[ServiceTest] Log '{0}': {1}", fl, System.IO.File.ReadAllText(fl).Trim()));

                if (System.IO.Directory.Exists(folderName))
                    System.IO.Directory.Delete(folderName, true);

                Assert.AreEqual(0, fileQueue.Count);
            }
        }
Ejemplo n.º 2
0
        public void ServiceTest_CalculationAndWatcherAndCommand()
        {
            string folderName = @"D:\test3";
            FileQueue fileQueue = new FileQueue();
            using (FileWatcher fileWatcher = new FileWatcher(fileQueue))
            {
                if (!System.IO.Directory.Exists(folderName))
                    System.IO.Directory.CreateDirectory(folderName);
                if (!System.IO.Directory.Exists(folderName + @"\logs"))
                    System.IO.Directory.CreateDirectory(folderName + @"\logs");

                System.IO.File.WriteAllText(string.Format(@"{0}\start.bat", folderName), "@echo [+] test %1 completed");

                fileWatcher.Start(new FileWatcherParam[] { new FileWatcherParam(folderName, "*.d??", string.Format("\"{0}\\start.bat\" ??", folderName), "utf-8") });

                for (int i = 0; i < 5; i++)
                    System.IO.File.WriteAllText(string.Format(@"{0}\test.d{1}", folderName, i.ToString("00")), "test");

                Thread.Sleep(1000);

                Assert.AreEqual(5, fileQueue.Count);

                var virtualParameters = new VirtualParameters();
                virtualParameters.SetParameter("CalculationLogs", string.Format(@"{0}\logs", folderName));
                virtualParameters.SetParameter("ProcessTimeout", "10");
                virtualParameters.SetParameter("CalculationLogs", folderName + @"\logs");
                using (var fileCalculation = new FileCalculation(fileQueue, virtualParameters))
                {
                    var logFiles = new List<string>();

                    fileCalculation.Log += (s, e) =>
                        {
                            if (e != null)
                                Assert.Fail("exception: " + e.Message);
                        };

                    fileCalculation.InfoChanged += (s,e) => 
                        {
                            if (e == null)
                            {
                                Debug.WriteLine("end");
                            }
                            else
                            { 
                                logFiles.Add(System.IO.Path.Combine(virtualParameters.GetParameter("CalculationLogs") ?? string.Empty,e.Id + ".log"));
                                Debug.WriteLine(string.Format("File: '{0}'|status:'{1}'|", e.File, e.Status));
                            }
                        };
                    fileCalculation.Start();

                    while (fileQueue.Count > 0 && fileCalculation.IsActive)
                        Thread.Sleep(1000);
                }

                if (System.IO.Directory.Exists(folderName + @"\logs"))
                    foreach (string fl in System.IO.Directory.GetFiles(folderName + @"\logs"))
                        Debug.WriteLine(string.Format("Log '{0}': {1}", fl, System.IO.File.ReadAllText(fl)));

                for (int i = 0; i < 5; i++)
                    System.IO.File.Delete(string.Format(@"{0}\test.d{1}", folderName, i.ToString("00")));

                if (System.IO.Directory.Exists(folderName))
                    System.IO.Directory.Delete(folderName, true);

                Assert.AreEqual(0, fileQueue.Count);
            }
        }