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); }
public async Task StopReentrantLoop() { var callback = new RSessionCallbackStub(); var session = new RSession(0, _testMethod.FileSystemSafeName, _brokerClient, new AsyncReaderWriterLock().CreateExclusiveReaderLock(), () => { }); await session.StartHostAsync(new RHostStartupInfo(), callback, 50000); var testMrs = new AsyncManualResetEvent(); callback.PlotHandler = (message, ct) => { testMrs.Set(); return(session.EvaluateAsync("x <- 1\n")); }; Task responceTask; using (var interaction = await session.BeginInteractionAsync()) { responceTask = interaction.RespondAsync("plot(1)\n"); } await testMrs.WaitAsync().Should().BeCompletedAsync(); await session.StopHostAsync().Should().BeCompletedAsync(20000); session.IsHostRunning.Should().BeFalse(); await responceTask.Should().BeCanceledAsync(); }
public async Task UnicodeOutput() { using (var inter = await _session.BeginInteractionAsync()) { await inter.RespondAsync("Sys.setlocale('LC_CTYPE', 'Japanese_Japan.932')\n"); } var output = new StringBuilder(); _session.Output += (sender, e) => output.Append(e.Message); using (var inter = await _session.BeginInteractionAsync()) { await inter.RespondAsync("'日本語'\n"); } output.ToString().Should().Be("[1] \"日本語\"\n"); }
public async Task Paste() { var input = @" h <- 'Hello' name <- readline('Name:') paste(h, name) "; var output = new List <string>(); _callback.ReadUserInputHandler = (m, l, c) => Task.FromResult("Goo\n"); using (var interaction = await _session.BeginInteractionAsync()) { _session.Output += (o, e) => output.Add(e.Message); await interaction.RespondAsync(input); } string.Join("", output).Should().Be("[1] \"Hello Goo\"\n"); }
public async Task BreakAsync(CancellationToken ct = default(CancellationToken)) { await TaskUtilities.SwitchToBackgroundThread(); using (var inter = await RSession.BeginInteractionAsync(true, ct)) { await inter.RespondAsync("browser()\n"); } }
public async Task BeginInteractionAsync_DisconnectedFromTheStart() { using (var session = new RSession(0, _brokerConnector, () => { })) { // ReSharper disable once AccessToDisposedClosure Func <Task> f = () => session.BeginInteractionAsync(); await f.ShouldThrowAsync <RHostDisconnectedException>(); } }
public async Task BeginInteractionAsync_DisconnectedFromTheStart() { using (var session = new RSession(0, _testMethod.FileSystemSafeName, _fileSystem, _brokerClient, new AsyncReaderWriterLock().CreateExclusiveReaderLock(), () => { })) { // ReSharper disable once AccessToDisposedClosure Func <Task> f = () => session.BeginInteractionAsync(); await f.ShouldThrowAsync <RHostDisconnectedException>(); } }
public async Task BreakAsync(CancellationToken ct = default(CancellationToken)) { await TaskUtilities.SwitchToBackgroundThread(); // Evaluation will not end until after Browse> is responded to, but this method must indicate completion // as soon as the prompt appears. So don't wait for this, but wait for the prompt instead. RSession.EvaluateAsync("browser()", REvaluationKind.Reentrant, ct) .SilenceException <MessageTransportException>().DoNotWait(); // Wait until prompt appears, but don't actually respond to it. using (var inter = await RSession.BeginInteractionAsync(true, ct)) { } }
public async Task ExclusiveInteraction() { var interactionTasks = await ParallelTools.InvokeAsync(4, i => Task.Factory.StartNew(() => _session.BeginInteractionAsync())); IList <Task <IRSessionInteraction> > runningTasks = interactionTasks.ToList(); while (runningTasks.Count > 0) { await Task.WhenAny(runningTasks); IList <Task <IRSessionInteraction> > completedTasks; runningTasks.Split(t => t.Status == TaskStatus.RanToCompletion, out completedTasks, out runningTasks); completedTasks.Should().ContainSingle(); completedTasks.Single().Result.Dispose(); } }
private async Task InitializeWorkerAsync(CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); await TaskUtilities.SwitchToBackgroundThread(); try { using (var eval = await RSession.BeginEvaluationAsync(cancellationToken: cancellationToken)) { // Re-initialize the breakpoint table. foreach (var bp in _breakpoints.Values) { await eval.EvaluateAsync(bp.GetAddBreakpointExpression(false)); // TODO: mark breakpoint as invalid if this fails. } await eval.EvaluateAsync("rtvs:::reapply_breakpoints()"); // TODO: mark all breakpoints as invalid if this fails. } // Attach might happen when session is already at the Browse prompt, in which case we have // missed the corresponding BeginRequest event, but we want to raise Browse anyway. So // grab an interaction and check the prompt. RSession.BeginInteractionAsync(cancellationToken: cancellationToken).ContinueWith(async t => { using (var inter = await t) { // If we got AfterRequest before we got here, then that has already taken care of // the prompt; or if it's not a Browse prompt, will do so in a future event. Bail out.' if (_initialPromptCts.IsCancellationRequested) { return; } // Otherwise, treat it the same as if AfterRequest just happened. ProcessBrowsePrompt(inter.Contexts); } }, cancellationToken).DoNotWait(); } catch (Exception ex) when(!ex.IsCriticalException()) { Dispose(); throw; } }
private void ProcessBrowsePrompt(IReadOnlyList <IRContext> contexts) { if (!contexts.IsBrowser()) { InterruptBreakpointHitProcessing(); return; } RSession.BeginInteractionAsync().ContinueWith(async t => { using (var inter = await t) { if (inter.Contexts != contexts) { // Someone else has already responded to this interaction. InterruptBreakpointHitProcessing(); return; } else { await ProcessBrowsePromptWorker(inter); } } }).DoNotWait(); }
public async Task <bool> ExecuteBrowserCommandAsync(string command, Func <IRSessionInteraction, Task <bool> > prepare = null, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); await TaskUtilities.SwitchToBackgroundThread(); using (var inter = await RSession.BeginInteractionAsync(isVisible: true, cancellationToken: cancellationToken)) { if (prepare != null) { if (!await prepare(inter)) { return(false); } } if (inter.Contexts.IsBrowser()) { await inter.RespondAsync(command + "\n"); return(true); } } return(false); }