Пример #1
0
        public void ServiceTest_Watcher()
        {
            FileQueue fileQueue = new FileQueue();
            using(FileWatcher fileWatcher = new FileWatcher(fileQueue))
            {
                int errCount = 0;
                fileWatcher.Log = (msg, ex) => 
                {
                    if (ex != null)
                        errCount++;
                };

                if (!System.IO.Directory.Exists(@"D:\test"))
                    System.IO.Directory.CreateDirectory(@"D:\test");

                fileWatcher.Start(new FileWatcherParam[] 
                { 
                    new FileWatcherParam(@"D:\test", "*.txt", "cmd", "utf-8"),
                    new FileWatcherParam(@"D:\test", "*.txt", "cmd", "utf-8"),
                    new FileWatcherParam(@"D:\err" + Guid.NewGuid().ToString("N"), "*.txt", "cmd", "utf-8")
                });

                for (int i = 0; i < 5; i++)
                { 
                    System.IO.File.WriteAllText(string.Format(@"D:\test\{0}.txt", i), "test");
                    System.IO.File.WriteAllText(string.Format(@"D:\test\{0}.xtx", i), "test");
                }
                Thread.Sleep(1000);
                for (int i = 0; i < 5; i++)
                {
                    System.IO.File.Delete(string.Format(@"D:\test\{0}.txt", i));
                    System.IO.File.Delete(string.Format(@"D:\test\{0}.xtx", i));
                }

                Assert.AreEqual(1, errCount, "Error count must equals 1 (load bad folder)");
                Assert.AreEqual(5, fileQueue.Count);

                System.IO.Directory.Delete(@"D:\test", true);
            }
        }
Пример #2
0
        public void ServiceTest_WatcherAndCommand()
        {
            string folderName = @"D:\test2";
            FileQueue fileQueue = new FileQueue();
            using (FileWatcher fileWatcher = new FileWatcher(fileQueue))
            {
                if (!System.IO.Directory.Exists(folderName))
                    System.IO.Directory.CreateDirectory(folderName);

                fileWatcher.Start(new FileWatcherParam[] { new FileWatcherParam(folderName, "*.d??", "cmd ??", "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);
                for (int i = 0; i < 5; i++)
                    System.IO.File.Delete(string.Format(@"{0}\test.d{1}", folderName, i.ToString("00")));

                Assert.AreEqual(5, fileQueue.Count);

                FileQueueElement item = null;
                while ((item = fileQueue.Pop()) != null)
                {
                    Debug.WriteLine("file: '{0}' and command for it: '{1}'", item.File, item.Command);
                }

                System.IO.Directory.Delete(folderName, true);
            }
        }
Пример #3
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);
            }
        }
Пример #4
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);
            }
        }