Exemplo n.º 1
0
            public async Task ConcurrentRequests()
            {
                var responds = new ConcurrentQueue <int>();
                var input    = new ConcurrentQueue <string>();
                var output   = new ConcurrentQueue <string>();

                void OutputHandler(object o, ROutputEventArgs e) => output.Enqueue(e.Message);

                Task <string> InputHandler(string prompt, int maximumLength, CancellationToken ct)
                {
                    input.Enqueue(prompt);
                    return(Task.FromResult($"{prompt}\n"));
                }

                _callback.ReadUserInputHandler = InputHandler;
                _session.Output += OutputHandler;
                await ParallelTools.InvokeAsync(10, async i => {
                    using (var interaction = await _session.BeginInteractionAsync()) {
                        responds.Enqueue(i);
                        await interaction.RespondAsync($"readline('{i}')");
                    }
                }, 20000);

                _session.Output -= OutputHandler;

                responds.Should().BeEquivalentTo(Enumerable.Range(0, 10));
                input.Should().Equal(responds.Select(i => i.ToString()));
                output.Should().Contain(responds.Select(i => $" \"{i}\""));
            }
Exemplo n.º 2
0
        public async Task StartStopMultipleSessions()
        {
            Func <int, Task <RSession> > start = async i => {
                var session = new RSession(i, _brokerClient, () => { });
                await session.StartHostAsync(new RHostStartupInfo { Name = _testMethod.Name + i }, null, 50000);

                return(session);
            };

            var sessionsTasks = await ParallelTools.InvokeAsync(4, start);

            if (sessionsTasks.Any(t => t.Status != TaskStatus.RanToCompletion))
            {
                Debugger.Launch();
            }

            sessionsTasks.Should().OnlyContain(t => t.Status == TaskStatus.RanToCompletion);
            var sessions = sessionsTasks.Select(t => t.Result).ToList();

            sessions.Should().OnlyContain(t => t.IsHostRunning);

            var sessionStopTasks = await ParallelTools.InvokeAsync(4, i => sessionsTasks[i].Result.StopHostAsync());

            sessionStopTasks.Should().OnlyContain(t => t.Status == TaskStatus.RanToCompletion);
            sessions.Should().OnlyContain(t => !t.IsHostRunning);
        }
Exemplo n.º 3
0
        public async Task Concurrent_Write_ReadCanceled()
        {
            var writersCount = 0;

            await ParallelTools.InvokeAsync(48, async i => {
                // Every 4th lock is writer lock
                var isWriter = i % 4 == 0;
                if (isWriter)
                {
                    var task = _arwl.WriterLockAsync(CancellationToken.None);
                    using (await task) {
                        var count = writersCount;
                        await Task.Delay(20, CancellationToken.None);
                        // CompareExchange won't work if lock wasn't exclusive
                        Interlocked.CompareExchange(ref writersCount, count + 1, count);
                    }
                    return(task);
                }
                else
                {
                    var cts  = new CancellationTokenSource();
                    var task = _arwl.ReaderLockAsync(cts.Token);
                    cts.Cancel();
                    if (!task.IsCanceled)
                    {
                        (await task).Dispose();
                    }
                    return(task);
                }
            });

            writersCount.Should().Be(12);
        }
Exemplo n.º 4
0
            public async Task CancelAllInParallel()
            {
                Task responceTask;

                using (var interaction = await _session.BeginInteractionAsync()) {
                    responceTask = interaction.RespondAsync("while(TRUE){}\n");
                }

                await ParallelTools.InvokeAsync(4, i => _session.CancelAllAsync());

                _session.IsHostRunning.Should().BeTrue();
                responceTask.Status.Should().Be(TaskStatus.Canceled);
            }
Exemplo n.º 5
0
        public async Task Concurrent_Write()
        {
            var writersCount = 0;
            await ParallelTools.InvokeAsync(12, async i => {
                using (await _arwl.WriterLockAsync()) {
                    var count = writersCount;
                    await Task.Delay(20);
                    // CompareExchange won't work it lock wasn't exclusive
                    Interlocked.CompareExchange(ref writersCount, count + 1, count);
                }
            });

            writersCount.Should().Be(12);
        }
Exemplo n.º 6
0
        public async Task Release()
        {
            var bal   = new BinaryAsyncLock();
            var count = 0;
            await ParallelTools.InvokeAsync(4, async i => {
                var isSet = await bal.WaitAsync();
                if (!isSet)
                {
                    Interlocked.Increment(ref count);
                    bal.Release();
                }
            });

            count.Should().Be(1);
        }
Exemplo n.º 7
0
        public async Task WaitAsync_SetInCtor()
        {
            var bal   = new BinaryAsyncLock(true);
            var count = 0;
            await ParallelTools.InvokeAsync(4, async i => {
                await bal.WaitAsync();
                if (!bal.IsSet)
                {
                    await Task.Delay(50);
                    Interlocked.Increment(ref count);
                }
            });

            count.Should().Be(0);
        }
Exemplo n.º 8
0
        public async Task WaitAsync_Reset()
        {
            var bal   = new BinaryAsyncLock();
            var count = 0;
            await ParallelTools.InvokeAsync(4, async i => {
                var token = await bal.WaitAsync();
                if (!bal.IsSet)
                {
                    await Task.Delay(50);
                    Interlocked.Increment(ref count);
                    token.Reset();
                }
            });

            count.Should().Be(4);
        }
            public async Task ExclusiveEvaluation()
            {
                var interactionTasks = await ParallelTools.InvokeAsync(4, i => Task.Factory.StartNew(() => _session.BeginEvaluationAsync()));

                IList <Task <IRSessionEvaluation> > runningTasks = interactionTasks.ToList();

                while (runningTasks.Count > 0)
                {
                    await Task.WhenAny(runningTasks);

                    IList <Task <IRSessionEvaluation> > completedTasks;
                    runningTasks.Split(t => t.Status == TaskStatus.RanToCompletion, out completedTasks, out runningTasks);
                    completedTasks.Should().ContainSingle();
                    completedTasks.Single().Result.Dispose();
                }
            }
Exemplo n.º 10
0
            public async Task ConcurrentRequests()
            {
                var output = new List <string>();
                EventHandler <ROutputEventArgs> outputHandler = (o, e) => output.Add(e.Message);

                _callback.ReadUserInputHandler = (m, l, c) => Task.FromResult($"{m}\n");

                await ParallelTools.InvokeAsync(10, async i => {
                    using (var interaction = await _session.BeginInteractionAsync()) {
                        _session.Output += outputHandler;
                        await interaction.RespondAsync($"readline('{i}')");
                        _session.Output -= outputHandler;
                    }
                });

                output.Should().Contain(Enumerable.Range(0, 10).Select(i => $" \"{i}\""));
            }
Exemplo n.º 11
0
        public async Task DoubleStart()
        {
            var         session = new RSession(0, _testMethod.FileSystemSafeName, _brokerClient, new AsyncReaderWriterLock().CreateExclusiveReaderLock(), () => { });
            Func <Task> start   = () => session.StartHostAsync(new RHostStartupInfo(), null, 50000);

            var tasks = await ParallelTools.InvokeAsync(4, i => start(), 50000);

            tasks.Should().ContainSingle(t => t.Status == TaskStatus.RanToCompletion);

            await session.HostStarted;

            session.IsHostRunning.Should().BeTrue();

            await session.StopHostAsync();

            session.IsHostRunning.Should().BeFalse();
        }
Exemplo n.º 12
0
        public async Task StartStopMultipleSessions()
        {
            Func <int, Task <RSession> > start = async i => {
                var session = new RSession(i, () => { });
                await session.StartHostAsync(new RHostStartupInfo { Name = _testMethod.Name, RBasePath = RUtilities.FindExistingRBasePath() }, null, 50000);

                return(session);
            };

            var sessions = await ParallelTools.InvokeAsync(4, start);

            sessions.Should().OnlyContain(s => s.IsHostRunning);

            await ParallelTools.InvokeAsync(4, i => sessions[i].StopHostAsync());

            sessions.Should().OnlyContain(s => !s.IsHostRunning);
        }
Exemplo n.º 13
0
        public async Task Concurrent_ReadWriteRead_CancelSecond()
        {
            var task  = _arwl.ReaderLockAsync(CancellationToken.None);
            var tasks = await ParallelTools.InvokeAsync(12, i => {
                var cts   = new CancellationTokenSource();
                var task1 = _arwl.WriterLockAsync(cts.Token);
                var task2 = _arwl.ReaderLockAsync(CancellationToken.None);
                cts.Cancel();
                return(new[] { task1, task2 });
            }, Task.WhenAll);

            task.Should().BeRanToCompletion();
            foreach (var taskPair in tasks)
            {
                taskPair[0].Should().BeCanceled();
                taskPair[1].Should().BeRanToCompletion();
            }
        }
Exemplo n.º 14
0
        public async Task DoubleStart()
        {
            var         session = new RSession(0, _brokerConnector, () => { });
            Func <Task> start   = () => session.StartHostAsync(new RHostStartupInfo {
                Name = _testMethod.Name
            }, null, 50000);

            Func <Task> f = () => ParallelTools.InvokeAsync(4, i => start());

            f.ShouldThrow <InvalidOperationException>();

            await session.HostStarted;

            session.IsHostRunning.Should().BeTrue();

            await session.StopHostAsync();

            session.IsHostRunning.Should().BeFalse();
        }
Exemplo n.º 15
0
        public async Task DoubleStart()
        {
            var         session = new RSession(0, _brokerClient, () => { });
            Func <Task> start   = () => session.StartHostAsync(new RHostStartupInfo {
                Name = _testMethod.Name
            }, null, 50000);

            var tasks = await ParallelTools.InvokeAsync(4, i => start(), 50000);

            tasks.Should().ContainSingle(t => t.Status == TaskStatus.RanToCompletion);

            await session.HostStarted;

            session.IsHostRunning.Should().BeTrue();

            await session.StopHostAsync();

            session.IsHostRunning.Should().BeFalse();
        }
Exemplo n.º 16
0
        public async Task Reset()
        {
            var bal   = new BinaryAsyncLock();
            var count = 0;
            await ParallelTools.InvokeAsync(4, async i => {
                try {
                    var isSet = await bal.WaitAsync();
                    if (!isSet)
                    {
                        await Task.Delay(50);
                        bal.Reset();
                    }
                } catch (OperationCanceledException) {
                    Interlocked.Increment(ref count);
                }
            });

            count.Should().Be(3);
        }
Exemplo n.º 17
0
        public async Task DoubleStart()
        {
            var         session = new RSession(0, null, () => { });
            Func <Task> start   = () => session.StartHostAsync(new RHostStartupInfo {
                Name      = _testMethod.Name,
                RBasePath = RUtilities.FindExistingRBasePath()
            }, 50000);

            Func <Task> f = () => ParallelTools.InvokeAsync(4, i => start());

            f.ShouldThrow <InvalidOperationException>();

            await session.HostStarted;

            session.IsHostRunning.Should().BeTrue();

            await session.StopHostAsync();

            session.IsHostRunning.Should().BeFalse();
        }
Exemplo n.º 18
0
        public async Task StartStopMultipleSessions()
        {
            Func <int, Task <RSession> > start = async i => {
                var session = new RSession(i, _testMethod.FileSystemSafeName + i, _brokerClient, new AsyncReaderWriterLock().CreateExclusiveReaderLock(), () => { });
                await session.StartHostAsync(new RHostStartupInfo(), null, 50000);

                return(session);
            };

            var sessionsTasks = await ParallelTools.InvokeAsync(4, start, 50000);

            sessionsTasks.Should().OnlyContain(t => t.Status == TaskStatus.RanToCompletion);
            var sessions = sessionsTasks.Select(t => t.Result).ToList();

            sessions.Should().OnlyContain(t => t.IsHostRunning);

            var sessionStopTasks = await ParallelTools.InvokeAsync(4, i => sessionsTasks[i].Result.StopHostAsync());

            sessionStopTasks.Should().OnlyContain(t => t.Status == TaskStatus.RanToCompletion);
            sessions.Should().OnlyContain(t => !t.IsHostRunning);
        }
Exemplo n.º 19
0
        public async Task Concurrent_ReadWrite()
        {
            var writersCount = 0;
            await ParallelTools.InvokeAsync(48, async i => {
                // Every 4th lock is writer lock
                var isWriter = i % 4 == 3;
                if (isWriter)
                {
                    using (await _arwl.WriterLockAsync()) {
                        var count = writersCount;
                        await Task.Delay(20);
                        // CompareExchange won't work it lock wasn't exclusive
                        Interlocked.CompareExchange(ref writersCount, count + 1, count);
                    }
                }
                else
                {
                    using (await _arwl.ReaderLockAsync()) { }
                }
            }, 50000);

            writersCount.Should().Be(12);
        }