Ejemplo n.º 1
0
        public void OnNext_EnqueuesItems_WhenCalled()
        {
            var observerToQueue = new ObserverToQueue <string>();
            var counter         = new ObservableCounter();

            counter.Subscribe(observerToQueue);
            var task = new Task(() => counter.Count());

            Assert.Empty(observerToQueue.Queue);
            task.Start();
            Thread.Sleep(30);
            Assert.NotEmpty(observerToQueue.Queue);
            Assert.True(observerToQueue.Queue.Count < 10);
            task.Wait();
            Assert.Equal(10, observerToQueue.Queue.Count);
            Assert.Equal("1,2,3,4,5,6,7,8,9,10".Split(","),
                         observerToQueue.Queue.ToArray());
        }
        public void OnNext_LogsItemsToFile_WhenCalled()
        {
            string tmpFile = Path.GetTempFileName();

            int GetFileLineCount(string file)
            {
                using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Write))
                    using (var reader = new StreamReader(fs))
                    {
                        return(reader.ReadToEnd()
                               .Split(Environment.NewLine)
                               .Count(l => l.Length > 0));
                    }
            }

            var loggers = new ILogger[] { new ListLogger(), new FileLogger(tmpFile), new ConsoleLogger() };
            var counter = new ObservableCounter();

            foreach (var logger in loggers)
            {
                counter.Subscribe(logger);
            }

            var task = new Task(() => counter.Count());

            task.Start();
            Thread.Sleep(30);
            Assert.True(GetFileLineCount(tmpFile) > 0);
            Assert.True(GetFileLineCount(tmpFile) < 10);
            task.Wait();
            Assert.Equal(10, GetFileLineCount(tmpFile));
            var lines = File.ReadAllLines(tmpFile);

            Assert.Equal("1,2,3,4,5,6,7,8,9,10".Split(","), lines);
            File.Delete(tmpFile);
        }