예제 #1
0
        public void ResetCancelsWriteTasks()
        {
            var sut        = new BufferedChannel <int>(0);
            var writeTask1 = sut.TryWriteSafeAsync(1);
            var writeTask2 = sut.TryWriteSafeAsync(2);

            sut.Reset();
            writeTask1.Result.ShouldBeFalse();
            writeTask2.Result.ShouldBeFalse();
        }
예제 #2
0
        public void ResetCancelsReadTasks()
        {
            var sut       = new BufferedChannel <int>(0);
            var readTask1 = sut.TryReadSafeAsync();
            var readTask2 = sut.TryReadSafeAsync();

            sut.Reset();
            readTask1.Result.HasValue.ShouldBeFalse();
            readTask2.Result.HasValue.ShouldBeFalse();
        }
예제 #3
0
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public void CanWriteAndReadFromSeveralThreadsConcurrently(int writeThreads, int readThreads, int bufferSize)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            var writeCount = readThreads * 100;
            var readCount  = writeThreads * 100;
            var sut        = new BufferedChannel <int>(bufferSize);

            for (var reuse = 0; reuse < 3; reuse++)
            {
                async Task WriteWorker()
                {
                    for (var i = 0; i < writeCount; i++)
                    {
                        await sut.WriteAsync(i).ConfigureAwait(false);
                    }
                }

                async Task ReadWorker()
                {
                    for (var i = 0; i < readCount; i++)
                    {
                        await sut.ReadAsync().ConfigureAwait(false);
                    }
                }

                var writeTasks = Enumerable.Range(0, writeThreads).Select(_ => Task.Run(WriteWorker));
                var readTasks  = Enumerable.Range(0, readThreads).Select(_ => Task.Run(ReadWorker));
                var allTasks   = writeTasks.Concat(readTasks).ToArray();

                Task.WhenAll(allTasks).ShouldCompleteIn(Timeout10Sec);

                sut.TryReadSafe(out int _).ShouldBe(false);
                sut.TryComplete().ShouldBe(true);

                sut.Reset();
            }
        }