Ejemplo n.º 1
0
        public void testStopWithFlush()
        {
            fileName = "test3";
            AsyncLog test3 = new AsyncLog(fileName, filePath, threadSleep);
            Random   rnd   = new Random();

            int logSize = 100;
            int lines;
            int randomNr = rnd.Next(1, 99);

            for (int i = 0; i < logSize; i++)
            {
                if (i == randomNr)
                {
                    test3.StopWithFlush();
                }
                test3.Write("Number with Flush: " + i.ToString());
                Thread.Sleep(50);
            }
            Thread.Sleep(500);

            randomNr++;


            lines = File.ReadLines(test3.fullFilePath).Count();

            Assert.AreEqual(randomNr, lines);
        }
Ejemplo n.º 2
0
        public async Task WriteToLogFile()
        {
            var dateProviderMock = new Mock <IDateTimeProvider>();
            var dateTime         = new DateTime(2020, 3, 11, 18, 55, 00);

            dateProviderMock.SetupGet(x => x.DateTimeNow).Returns(dateTime);
            DateTimeProvider.Current = dateProviderMock.Object;

            var log = new AsyncLog(new LogsStorage());

            log.Write(_demoText);
            log.StopWithFlush();

            Assert.True(Directory.Exists(LogDir));
            var files = Directory.GetFiles(LogDir);

            var dateTimeInLogfileName = dateTime.ToString("yyyyMMdd HHmmss fff");

            foreach (var file in files)
            {
                var fileInfo = new FileInfo(file);

                if (fileInfo.Name.Contains(dateTimeInLogfileName))
                {
                    using (var streamReader = File.OpenText(file))
                    {
                        var fileContent = await streamReader.ReadToEndAsync();

                        Assert.True(fileContent.Contains(_demoText));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            ILog logger = new AsyncLog();

            for (int i = 0; i < 15; i++)
            {
                logger.Write("Number with Flush: " + i.ToString());
                Thread.Sleep(50);
            }

            logger.StopWithFlush();

            ILog logger2 = new AsyncLog();

            for (int i = 5000; i > 0; i--)
            {
                logger2.Write("Number with No flush: " + i.ToString());
            }

            logger2.StopWithoutFlush();

            Console.ReadLine();

            Console.WriteLine("Day Change: " + (new Tests()).TestDayChange());
            Console.WriteLine("Line Logged: " + (new Tests()).TestCallToWrite());
            Console.WriteLine("Stop With Flush: " + (new Tests()).TestStopWithFlush());
            Console.WriteLine("Stop Without Flush (test not deterministic!): " + (new Tests()).TestStopWithoutFlush());
        }
Ejemplo n.º 4
0
        public void StopWithFlush_StopAtPoint3_AssertThat0IsWritten()
        {
            ILog writerAsync = new AsyncLog(new FakeLogOutputForUnitTestWithFlush());

            for (int i = 30; i >= 0; i--)
            {
                writerAsync.Write(i.ToString());
                System.Threading.Thread.Sleep(50);
            }
            writerAsync.StopWithFlush();
        }
Ejemplo n.º 5
0
        static void Do1()
        {
            ILog logger = new AsyncLog();

            for (int i = 0; i < 15; i++)
            {
                logger.Write("Number with Flush: " + i.ToString());
                Thread.Sleep(50);
            }

            logger.StopWithFlush();
        }
Ejemplo n.º 6
0
        /// <summary>
        ///  Run and  Stop Without Flush
        /// </summary>
        static void StopWithFlushTest()
        {
            ILog logger2 = new AsyncLog();

            for (int i = 0; i < 1000; i++)
            {
                logger2.Write("Number with Flush: " + i.ToString());
                if (i == 500)
                {
                    logger2.StopWithFlush();
                }
            }
            logger2.CloseStreamWriter();
            Thread.Sleep(2000);
        }
Ejemplo n.º 7
0
        public void TestStopWithFlush()
        {
            // First logger
            ILog   firstFlushLog = new AsyncLog(nameof(firstFlushLog), Location);
            string pathFirst     = TestUtils.BuildFilePath(Location, nameof(firstFlushLog));

            for (int i = 0; i < 15; i++)
            {
                firstFlushLog.AddLogToQueue("Number with Flush: " + i);
                if (i == 10)
                {
                    firstFlushLog.StopWithFlush();
                }
            }

            Thread.Sleep(100);

            var firstFile       = File.OpenRead(pathFirst);
            var firstFileLength = firstFile.Length;

            firstFile.Dispose();

            // Second logger
            ILog   secondFlushLog = new AsyncLog(nameof(secondFlushLog), Location);
            string pathSecond     = TestUtils.BuildFilePath(Location, nameof(secondFlushLog));

            for (int i = 0; i < 15; i++)
            {
                if (i == 10)
                {
                    secondFlushLog.StopWithFlush();
                }
                secondFlushLog.AddLogToQueue("Number with Flush: " + i);
            }

            Thread.Sleep(100);

            var secondFile       = File.OpenRead(pathSecond);
            var secondFileLength = secondFile.Length;

            secondFile.Dispose();

            File.Delete(pathFirst);
            File.Delete(pathSecond);
            Assert.IsTrue(firstFileLength > secondFileLength);
        }
Ejemplo n.º 8
0
        public void TestWriteToLogWithFlush()
        {
            AsyncLog logger        = new AsyncLog();
            int      numberOfLines = 15;

            for (int i = 1; i < numberOfLines; i++)
            {
                logger.Write("Number with Flush: " + i.ToString());
                Thread.Sleep(50);
            }
            logger.StopWithFlush();

            Thread.Sleep(5000);
            logger.logFile._writer.Close();
            string[] readText = File.ReadAllLines(logger.logFile.filename);
            Assert.IsTrue(File.Exists(logger.logFile.filename));
            Assert.IsTrue(readText.Length == numberOfLines);
        }
Ejemplo n.º 9
0
        public void TestOnException()
        {
            ILog   testLogger = new AsyncLog(nameof(testLogger), Location);
            string file       = TestUtils.BuildFilePath(Location, nameof(testLogger));

            try
            {
                for (int i = 0; i < 10; i++)
                {
                    testLogger.AddLogToQueue("Test log no: " + i);
                    if (i == 5)
                    {
                        throw new Exception();
                    }
                }
            }
            catch
            {
                // ignored
            }

            testLogger.StopWithFlush();

            ILog   testLogger2 = new AsyncLog(nameof(testLogger2), Location);
            string file2       = TestUtils.BuildFilePath(Location, nameof(testLogger2));

            for (int i = 0; i < 10; i++)
            {
                testLogger2.AddLogToQueue("Test log after exception no: " + i);
            }

            testLogger2.StopWithFlush();

            Thread.Sleep(100);

            Assert.IsTrue(File.Exists(file));
            Assert.IsTrue(File.Exists(file2));

            Thread.Sleep(1000);

            File.Delete(file);
            File.Delete(file2);
        }
Ejemplo n.º 10
0
        public void TestFileDate()
        {
            var date = DateTime.Today.ToString("yyyyMMdd");

            ILog   logDateTester = new AsyncLog(nameof(logDateTester), Location);
            string path          = TestUtils.BuildFilePath(Location, nameof(logDateTester));

            logDateTester.AddLogToQueue("Some random test log"); // file created on first log
            logDateTester.StopWithFlush();

            Thread.Sleep(1);

            var file = File.OpenRead(path);

            file.Dispose();
            var fileDate = file.Name.Substring(24, 8);

            File.Delete(path);
            Assert.IsTrue(fileDate == date);
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            string fileName    = "Log";
            string filePath    = @"C:\LogTest";
            int    threadSleep = 50;

            ILog logger = new AsyncLog(fileName, filePath, threadSleep);

            while (true)
            {
                for (int i = 0; i < 15; i++)
                {
                    logger.Write("Number with Flush: " + i.ToString());
                    Thread.Sleep(50);
                }

                logger.StopWithFlush();
                Thread.Sleep(50);

                for (int i = 15; i < 50; i++)
                {
                    logger.Write("Number with Flush: " + i.ToString());
                    Thread.Sleep(50);
                }
                fileName = "Log";
                filePath = @"C:\LogTest";
                ILog logger2 = new AsyncLog(fileName, filePath, threadSleep);

                for (int i = 100; i > 0; i--)
                {
                    logger2.Write("Number with No flush: " + i.ToString());
                    Thread.Sleep(20);
                }

                logger2.StopWithoutFlush();
                Console.WriteLine("finish");
                Console.ReadLine();
            }
        }
Ejemplo n.º 12
0
        public static void Main(string[] args)
        {
            var flushLogger = new AsyncLog(new LogsStorage());

            for (var i = 0; i < 15; i++)
            {
                flushLogger.Write($"Number with Flush: {i}");
                Task.Delay(10).Wait();
            }
            flushLogger.StopWithFlush();

            var noFlushLogger = new AsyncLog(new LogsStorage());

            for (var i = 50; i > 0; i--)
            {
                noFlushLogger.Write($"Number with No flush: {i}");
                Task.Delay(5).Wait();
            }
            noFlushLogger.StopWithoutFlush();

            Console.ReadKey();
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            ILog logger = new AsyncLog(new FileWriter());

            for (int i = 0; i < 15; i++)
            {
                logger.Write("Number with Flush: " + i);
            }
            Thread.Sleep(5);
            logger.StopWithFlush();

            ILog logger2 = new AsyncLog(new FileWriter());

            for (int i = 50; i > 0; i--)
            {
                logger2.Write("Number with No flush: " + i);
            }
            Thread.Sleep(5);
            logger2.StopWithoutFlush();

            Console.ReadLine();
        }
Ejemplo n.º 14
0
        public async Task StopWritingWithFlush()
        {
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();
            var dateTime             = new DateTime(2020, 3, 14, 12, 45, 00);

            dateTimeProviderMock.SetupGet(x => x.DateTimeNow).Returns(dateTime);
            DateTimeProvider.Current = dateTimeProviderMock.Object;

            var log         = new AsyncLog(new LogsStorage());
            var loopCounter = 5;

            for (var i = 0; i <= loopCounter; i++)
            {
                log.Write($"{_demoText}{i}");
            }
            log.StopWithFlush();

            Assert.True(Directory.Exists(LogDir));

            var files = Directory.GetFiles(LogDir);
            var dateTimeInLogfileName = dateTime.ToString("yyyyMMdd HHmmss fff");

            foreach (var file in files)
            {
                var fileInfo = new FileInfo(file);
                if (fileInfo.Name.Contains(dateTimeInLogfileName))
                {
                    using (StreamReader fileReader = File.OpenText(file))
                    {
                        var fileContent = await fileReader.ReadToEndAsync();

                        Assert.True(fileContent.Contains($"{_demoText}{loopCounter}"));
                    }
                    break;
                }
            }
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            // the stopAt variable is initiated to 50 to marka breakinmg point where a new date is detected
            int stopAt = 50;

            Console.WriteLine("Which test do you want to run  ?");
            Console.WriteLine("0 - No Stops / No Date Change");
            Console.WriteLine("1 - No Stops / With Date Change");
            Console.WriteLine("2 - Exit With Flush");
            Console.WriteLine("3 - Exit With No Flush");
            Console.WriteLine("4 - Exit Program");
            Char keyChar = 'A';

            while (keyChar != '4')
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey(true);
                keyChar = keyInfo.KeyChar;
                switch (keyChar)
                {
                // Test that checks that the program is running/writing something
                case ('0'):
                {
                    ILog logger0 = new AsyncLog();
                    for (int i = 0; i < 200; i++)
                    {
                        Console.WriteLine("Test line with No Stop # " + i);
                        logger0.Write("Number with No Stop: " + i.ToString() + "\n");
                        Thread.Sleep(20);
                    }

                    Console.WriteLine("Test line NO STOPS Completed");
                    break;
                }

                //Test to check if a new log file is created upon passing midnight
                case ('1'):
                {
                    ILog logger0 = new AsyncLog();
                    for (int i = 0; i < 200; i++)
                    {
                        Console.WriteLine("Test line with No Stop # " + i);
                        logger0.Write("Number with No Stop: " + i.ToString() + "\n");
                        Thread.Sleep(100);
                        if (i == stopAt)
                        {
                            Console.WriteLine("Increasing day by one");
                            increaseDayByOne();
                        }
                    }

                    Console.WriteLine("Test line NO STOPS Completed");
                    break;
                }

                //Test to check if the system exits with flush
                case ('2'):
                {
                    Console.WriteLine("Testing With Flush");
                    ILog logger = new AsyncLog();
                    logger.ExitFlushTestingMode();
                    for (int i = 0; i < 150; i++)
                    {
                        Console.WriteLine("Test line with flush # " + i);
                        logger.Write("Number with Flush: " + i.ToString() + "\n");
                        Thread.Sleep(100);
                        if (i == stopAt)
                        {
                            Console.WriteLine("Increasing day by one");
                            increaseDayByOne();
                        }
                    }


                    logger.StopWithFlush();

                    Console.WriteLine("Testing Without Flush");
                    break;
                }

                //Test to check if the system exits without flush
                case ('3'):
                {
                    ILog logger2 = new AsyncLog();
                    logger2.ExitNoFlushTestingMode();
                    for (int i = 100; i > 0; i--)
                    {
                        Console.WriteLine("Test line (no flush) # " + i);
                        logger2.Write("Number with No flush: " + i.ToString());
                        Thread.Sleep(35);
                        if (i == stopAt)
                        {
                            Console.WriteLine("Increasing day by one");
                            increaseDayByOne();
                        }
                    }

                    logger2.StopWithoutFlush();
                    break;
                }

                case ('4'):
                {
                    Console.Write("Program Completed");
                    Console.ReadLine();
                    break;
                }
                }
                Console.Write("Program Completed");
                Console.ReadLine();
            }
        }