Пример #1
0
        public void WriterTest()
        {
            var timeOut = TimeSpan.FromSeconds(4);

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            using (var problem = new ReadersWritersProblem(readersCount: 4))
            {
                problem.WriterEventRaised += OnWriterEventRaised;

                while (Interlocked.Read(ref _writerFinishedCount) < 0 || stopWatch.Elapsed < timeOut)
                {
                    ;
                }

                problem.WriterEventRaised -= OnWriterEventRaised;
                stopWatch.Stop();
            }

            Assert.IsTrue(_writerFinishedCount > 0, $"The writer did not finish writing for {timeOut}");
        }
Пример #2
0
        public void ReadersTest()
        {
            var       timeOut      = TimeSpan.FromSeconds(2);
            const int readersCount = 4;

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            using (var problem = new ReadersWritersProblem(readersCount))
            {
                problem.ReaderEventRaised += OnReaderEventRaised;

                while (Interlocked.Read(ref _readerFinishedCount) < readersCount || stopWatch.Elapsed < timeOut)
                {
                    ;
                }

                problem.ReaderEventRaised -= OnReaderEventRaised;
                stopWatch.Stop();
            }

            Assert.IsTrue(_readerFinishedCount >= readersCount, $"The readers are not finished reading for {timeOut}");
        }