Esempio n. 1
0
        public void NewFilesCreatedAfterMidnight()
        {
            var timeMock = new Mock <IDateTimeProvider>();

            timeMock.SetupGet(x => x.DateTimeNow).Returns(new DateTime(2020, 3, 12, 11, 59, 00));
            DateTimeProvider.Current = timeMock.Object;

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

            log.Write(_demoText);
            Thread.Sleep(50);

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

            int numberOfFilesBeforeMidnight = Directory.GetFiles(LogDir).Count();

            timeMock.SetupGet(x => x.DateTimeNow).Returns(new DateTime(2020, 3, 13, 00, 01, 00));

            log.Write(_demoText);
            Thread.Sleep(50);

            int numberOfFilesAfterMidnight = Directory.GetFiles(LogDir).Count();

            Assert.True(numberOfFilesBeforeMidnight < numberOfFilesAfterMidnight);
        }
Esempio n. 2
0
        public void Write1milAndFlush_SingleThread()
        {
            for (int i = 0; i < 1_000_000; i++)
            {
                _log.Write("Number with Flush: " + i.ToString());
            }

            _log.Flush();
            _log.Stop();
        }
Esempio 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.Flush();
            logger.Stop();

            ILog logger2 = new AsyncLog();

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

            logger2.Stop();

            Console.ReadLine();
        }
Esempio n. 4
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);
        }
Esempio n. 5
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());
        }
Esempio n. 6
0
        public void testNewLogFileNextDay()
        {
            fileName = "test2";
            AsyncLog test2 = new AsyncLog(fileName, filePath, threadSleep);
            DateTime today = new DateTime();
            Random   rnd   = new Random();

            int startFiles;
            int finishFiles;
            int logSize  = 100;
            int randomNr = rnd.Next(26, 28);

            startFiles = Directory.GetFiles(filePath).Length;
            today      = DateTime.Now;

            for (int i = 0; i < logSize; i++)
            {
                if (i == randomNr)
                {
                    test2.setDate(today.AddDays(randomNr));
                }
                test2.Write("Number with Flush: " + i.ToString());
                Thread.Sleep(50);
            }
            finishFiles = Directory.GetFiles(filePath).Length;
            startFiles++;

            Assert.AreEqual(finishFiles, startFiles);
        }
Esempio n. 7
0
        public void testStopWithOutflush()
        {
            fileName = "test4";
            AsyncLog test4 = new AsyncLog(fileName, filePath, threadSleep);
            Random   rnd   = new Random();

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

            for (int i = 0; i < randomNr; i++)
            {
                if (i == randomNr)
                {
                    test4.StopWithoutFlush();
                }
                test4.Write("Number with out Flush: " + i.ToString());

                Thread.Sleep(20);
            }
            randomNr++;


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

            Assert.IsTrue(randomNr > lines);
        }
Esempio n. 8
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));
                    }
                }
            }
        }
Esempio n. 9
0
        public void TestNewDayNewFile()
        {
            AsyncLog logger = new AsyncLog();

            logger.Write("Number with Flush: ");
            Thread.Sleep(1000);
            DateTime plusOne = DateTime.Now.AddDays(1);

            logger.logFile._curDate = plusOne;
            logger.logFile.isTest   = true;
            logger.Write("Number with Flush: ");
            Thread.Sleep(1000);
            logger.StopWithoutFlush();

            Assert.IsTrue(File.Exists(logger.logFile.filename));
            Assert.IsTrue(logger.logFile.filename.Contains(plusOne.ToString("yyyyMMdd HHmmss fff")));
        }
Esempio n. 10
0
        public async Task StopWritingWithoutFlush()
        {
            var dateTimeProviderMock = new Mock <IDateTimeProvider>();
            var dateTime             = new DateTime(2020, 3, 14, 12, 30, 00);

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

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

            log.Write(_demoText);
            var loopCounter = 5;

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

            await Task.Delay(1);

            log.StopWithoutFlush();

            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 content = await streamReader.ReadToEndAsync();

                        Assert.False(content.Contains($"{_demoText}{loopCounter}"));
                    }
                    break;
                }
            }
        }
Esempio n. 11
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();
        }
Esempio n. 12
0
        static void Do2()
        {
            ILog logger2 = new AsyncLog();

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

            logger2.StopWithoutFlush();
        }
Esempio n. 13
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();
        }
Esempio n. 14
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();
            }
        }
Esempio n. 15
0
        public void StopWithoutFlush_StopAtPoint10_AssertThatItPointIs11AndNotEqual10()
        {
            ILog writerAsync = new AsyncLog(new FakeLogOutput());

            for (int i = 50; i >= 0; i--)
            {
                writerAsync.Write(i.ToString());
                if (i == 10)
                {
                    writerAsync.StopWithoutFlush();
                }
                System.Threading.Thread.Sleep(50);
            }
        }
Esempio n. 16
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);
        }
Esempio n. 17
0
        /// <summary>
        /// If the works are done completely add "finish" to the last line
        /// </summary>
        static void DoWork()
        {
            ILog logger = new AsyncLog();

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

            ILog logger2 = new AsyncLog();

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

            Thread.Sleep(5000);
        }
Esempio n. 18
0
 public void Write_ExceptionThrown_NoExceptionExpected()
 {
     try
     {
         ILog writerAsync = new AsyncLog(new FakeLogOutputForUnitTestThrowException());
         for (int i = 10; i >= 0; i--)
         {
             writerAsync.Write(i.ToString());
             System.Threading.Thread.Sleep(50);
         }
     }
     catch (Exception ex)
     {
         Assert.Fail("Expected no exception but got one : " + ex.Message);
     }
 }
Esempio n. 19
0
        /// <summary>
        /// Run the task and change the date dynamicly
        /// </summary>
        void DoWork()
        {
            ILog logger2 = new AsyncLog();

            for (int i = 1000; i > 0; i--)
            {
                logger2.Write("Number with No flush: " + i.ToString());
                if (i == 500)
                {
                    ChangeCurrentDate.ChangeDatetoTommorrow();
                }
                Thread.Sleep(20);
            }
            logger2.CloseStreamWriter();

            Thread.Sleep(5000);
        }
Esempio n. 20
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);
        }
Esempio n. 21
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();
        }
Esempio n. 22
0
        public void testLogWriten()
        {
            fileName = "test1";
            AsyncLog test = new AsyncLog(fileName, filePath, threadSleep);
            {
                int logSize = 100;
                int lines;

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

                logSize++;

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

                Assert.AreEqual(logSize, lines);
            }
        }
Esempio n. 23
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();
        }
Esempio n. 24
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();
            }
        }