public void ParallelAccess() { using (var sessionProvider = new RSessionProvider()) { var guids = new[] { new Guid(), new Guid() }; ParallelTools.Invoke(100, i => { var session = sessionProvider.GetOrCreate(guids[i % 2], _brokerConnector); session.Dispose(); }); } }
public async Task SwitchToTheSameBroker_NoSessions() { using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) { var switch1Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker)); var switch2Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker)); await Task.WhenAll(switch1Task, switch2Task); switch1Task.Status.Should().Be(TaskStatus.RanToCompletion); switch2Task.Status.Should().Be(TaskStatus.RanToCompletion); } }
public void Lifecycle() { var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal()); // ReSharper disable once AccessToDisposedClosure Action a = () => sessionProvider.GetOrCreate(new Guid()); a.ShouldNotThrow(); sessionProvider.Dispose(); a.ShouldThrow <InvalidOperationException>(); }
public async Task SwitchToTheSameBroker_NoSessions() { using (var sessionProvider = new RSessionProvider(_services)) { var switch1Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker_NoSessions)); var switch2Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker_NoSessions)); await ParallelTools.WhenAll(30000, switch1Task, switch2Task); switch1Task.Status.Should().Be(TaskStatus.RanToCompletion); switch2Task.Status.Should().Be(TaskStatus.RanToCompletion); } }
public void Lifecycle() { var sessionProvider = new RSessionProvider(_services); // ReSharper disable once AccessToDisposedClosure Action a = () => sessionProvider.GetOrCreate(nameof(Lifecycle)); a.ShouldNotThrow(); sessionProvider.Dispose(); a.ShouldThrow <InvalidOperationException>(); }
public void Lifecycle() { var sessionProvider = new RSessionProvider(); // ReSharper disable once AccessToDisposedClosure Action a = () => sessionProvider.GetOrCreate(new Guid(), _brokerConnector); a.ShouldNotThrow(); sessionProvider.Dispose(); a.ShouldThrow <InvalidOperationException>(); }
private IRInteractiveWorkflow CreateRInteractiveWorkflow() { var sessionProvider = new RSessionProvider(_services, new InteractiveWindowConsole(_shell, _instanceLazy)); var workflow = new RInteractiveWorkflow(sessionProvider, _connectionsProvider, _historyProvider, _packagesProvider, _plotsProvider, _activeTextViewTracker, _debuggerModeTracker, _shell, _settings, _wss, () => DisposeInstance(sessionProvider)); _disposableBag.Add(workflow); sessionProvider.BrokerChanging += OnBrokerChanging; return(workflow); }
public async Task ConnectToRemote() { using (var sessionProvider = new RSessionProvider(_services)) { await _remoteBroker.ConnectAsync(sessionProvider); var session = sessionProvider.GetOrCreate(nameof(ConnectToRemote)); await session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 5000); var evaluationResult = await session.EvaluateAsync <int>("1+2", REvaluationKind.Normal); evaluationResult.Should().Be(3); } }
public async Task SwitchBrokerWithCancellation(int timeout) { using (var sessionProvider = new RSessionProvider(_coreServices)) { var session = sessionProvider.GetOrCreate(nameof(SwitchBrokerWithCancellation)); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation)); await session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000); var result = await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation) + "1", cancellationToken : new CancellationTokenSource(timeout).Token); result.Should().BeFalse(); } }
public async Task SwitchWhileEnsureHostStartedAsyncFailed() { using (var sessionProvider = new RSessionProvider(_services)) { var session = sessionProvider.GetOrCreate(nameof(SwitchWhileEnsureHostStartedAsyncFailed)); session.RestartOnBrokerSwitch = true; var startTask = session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000); await Task.Yield(); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchWhileEnsureHostStartedAsyncFailed)); startTask.Should().BeCanceled(); session.IsHostRunning.Should().BeTrue(); } }
public void GetOrCreate() { using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) { var guid = new Guid(); var session1 = sessionProvider.GetOrCreate(guid); session1.Should().NotBeNull(); var session2 = sessionProvider.GetOrCreate(guid); session2.Should().BeSameAs(session1); session1.Dispose(); var session3 = sessionProvider.GetOrCreate(guid); session3.Should().NotBeSameAs(session1); session3.Id.Should().NotBe(session1.Id); } }
public async Task SwitchWhileEnsureHostStartedAsyncFailed() { using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) { var guid = new Guid(); var session = sessionProvider.GetOrCreate(guid); var startTask = session.EnsureHostStartedAsync(new RHostStartupInfo { Name = nameof(session) }, null, 1000); await Task.Yield(); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchWhileEnsureHostStartedAsyncFailed)); startTask.Should().BeCanceled(); session.IsHostRunning.Should().BeTrue(); } }
public void GetOrCreate() { using (var sessionProvider = new RSessionProvider(_services)) { var guid = Guid.NewGuid().ToString(); var session1 = sessionProvider.GetOrCreate(guid); session1.Should().NotBeNull(); var session2 = sessionProvider.GetOrCreate(guid); session2.Should().BeSameAs(session1); session1.Dispose(); var session3 = sessionProvider.GetOrCreate(guid); session3.Should().NotBeSameAs(session1); session3.Id.Should().NotBe(session1.Id); } }
private async Task ExecuteInSession(string[] inputs, IRSessionCallback app) { using (var sessionProvider = new RSessionProvider()) { var session = sessionProvider.GetOrCreate(Guid.NewGuid(), _brokerConnector); await session.StartHostAsync(new RHostStartupInfo { Name = _testMethod.Name }, app, 50000); foreach (var input in inputs) { using (var interaction = await session.BeginInteractionAsync()) { await interaction.RespondAsync(input + "\n"); } } await session.StopHostAsync(); } }
public async Task ConnectWhenSwitching() { using (var sessionProvider = new RSessionProvider(_services)) { var session1 = sessionProvider.GetOrCreate(nameof(ConnectWhenSwitching) + 1); var session2 = sessionProvider.GetOrCreate(nameof(ConnectWhenSwitching) + 2); var switchTask = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(ConnectWhenSwitching)); var startHost1Task = Task.Delay(50).ContinueWith(t => session1.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000)).Unwrap(); var startHost2Task = Task.Delay(100).ContinueWith(t => session2.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000)).Unwrap(); await ParallelTools.WhenAll(30000, switchTask, startHost1Task, startHost2Task); startHost1Task.Should().BeRanToCompletion(); startHost2Task.Should().BeRanToCompletion(); await ParallelTools.WhenAll(30000, session1.HostStarted, session2.HostStarted); } }
public async Task SwitchBroker_DisposeSession(int timeout, bool restartOnBrokerSwitch) { using (var sessionProvider = new RSessionProvider(_services)) { var session = sessionProvider.GetOrCreate(nameof(SwitchBroker_DisposeSession)); session.RestartOnBrokerSwitch = restartOnBrokerSwitch; await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBroker_DisposeSession)); await session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 5000); var sessionDisposeTask = Task.Delay(timeout).ContinueWith(t => session.Dispose()); var result = await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBroker_DisposeSession) + "1"); await sessionDisposeTask; result.Should().BeTrue(); } }
public void GetOrCreate() { var sessionProvider = new RSessionProvider(); var guid = new Guid(); var session1 = sessionProvider.GetOrCreate(guid, null); session1.Should().NotBeNull(); var session2 = sessionProvider.GetOrCreate(guid, null); session2.Should().BeSameAs(session1); session1.Dispose(); var session3 = sessionProvider.GetOrCreate(guid, null); session3.Should().NotBeSameAs(session1); session3.Id.Should().NotBe(session1.Id); }
public async Task ConnectWhenSwitching_SwitchFailed() { using (var sessionProvider = new RSessionProvider(_services)) { await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(ConnectWhenSwitching_SwitchFailed)); var switchTask = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(ConnectWhenSwitching_SwitchFailed) + "1", BrokerConnectionInfo.Create(null, "JHFFR", @"\\JHF\F\R")); await Task.Yield(); var session = sessionProvider.GetOrCreate(nameof(ConnectWhenSwitching_SwitchFailed)); var startHostTask = session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000); await ParallelTools.WhenAll(30000, switchTask, startHostTask); startHostTask.Should().BeRanToCompletion(); switchTask.Should().BeRanToCompletion(); switchTask.Result.Should().BeFalse(); } }
public async Task SwitchBrokerWithCancellation(int timeout) { using (var sessionProvider = new RSessionProvider(_services)) { var session = sessionProvider.GetOrCreate(nameof(SwitchBrokerWithCancellation)); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation)); await session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000); var stopwatch = new Stopwatch(); stopwatch.Start(); var result = await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation) + "1", cancellationToken : new CancellationTokenSource(timeout).Token); stopwatch.Stop(); result.Should().Be(stopwatch.ElapsedMilliseconds < timeout); } }
public async Task SwitchToTheSameBroker() { using (var sessionProvider = new RSessionProvider(_services)) { var session = sessionProvider.GetOrCreate(nameof(SwitchToTheSameBroker)); session.RestartOnBrokerSwitch = true; await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker)); await session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000); var switch1Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker) + "1"); var switch2Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker) + "1"); await ParallelTools.WhenAll(30000, switch1Task, switch2Task); switch1Task.Status.Should().Be(TaskStatus.RanToCompletion); switch2Task.Status.Should().Be(TaskStatus.RanToCompletion); } }
private async Task ExecuteInSession(string[] inputs, IRSessionCallback app) { using (var sessionProvider = new RSessionProvider(_services)) { await sessionProvider.TrySwitchBrokerAsync(nameof(IdeGraphicsDeviceTest)); var session = sessionProvider.GetOrCreate(_testMethod.FileSystemSafeName); await session.StartHostAsync(new RHostStartupInfo(), app, 50000); foreach (var input in inputs) { using (var interaction = await session.BeginInteractionAsync()) { await interaction.RespondAsync(input.EnsureLineBreak()); } } await session.StopHostAsync(); } }
public async Task SwitchBroker_DisposeSessionProvider(int timeout) { var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal()); var guid = new Guid(); var session = sessionProvider.GetOrCreate(guid); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation)); await session.EnsureHostStartedAsync(new RHostStartupInfo { Name = nameof(session) }, null, 5000); var sessionDisposeTask = Task.Delay(timeout).ContinueWith(t => sessionProvider.Dispose()); var result = await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation) + "1"); await sessionDisposeTask; result.Should().BeFalse(); }
public async Task SwitchToTheSameBroker() { using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) { var guid = new Guid(); var session = sessionProvider.GetOrCreate(guid); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker)); await session.EnsureHostStartedAsync(new RHostStartupInfo { Name = nameof(session) }, null, 1000); var switch1Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker) + "1"); var switch2Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker) + "1"); await Task.WhenAll(switch1Task, switch2Task); switch1Task.Status.Should().Be(TaskStatus.RanToCompletion); switch2Task.Status.Should().Be(TaskStatus.RanToCompletion); } }
public async Task SwitchMultipleSessions() { using (var sessionProvider = new RSessionProvider(_services)) { await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchMultipleSessions)).Should().BeCompletedAsync(); var sessions = new IRSession[4]; for (var i = 0; i < sessions.Length; i++) { sessions[i] = sessionProvider.GetOrCreate(nameof(SwitchMultipleSessions) + i); sessions[i].RestartOnBrokerSwitch = true; } await ParallelTools.WhenAll(30000, sessions.Select((s, i) => s.EnsureHostStartedAsync(new RHostStartupInfo(), null, 30000)).ToArray()); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchMultipleSessions) + "1").Should().BeCompletedAsync(); sessions.Should().OnlyContain(s => s.IsHostRunning); } }
public RInteractiveWorkflow(IConnectionManagerProvider connectionsProvider , IRHistoryProvider historyProvider , IRPackageManagerProvider packagesProvider , IRPlotManagerProvider plotsProvider , IActiveWpfTextViewTracker activeTextViewTracker , IDebuggerModeTracker debuggerModeTracker , ICoreShell coreShell , IRSettings settings) { _activeTextViewTracker = activeTextViewTracker; _debuggerModeTracker = debuggerModeTracker; _settings = settings; Shell = coreShell; Console = new InteractiveWindowConsole(this); RSessions = new RSessionProvider(coreShell.Services, Console); RSession = RSessions.GetOrCreate(SessionNames.InteractiveWindow); Connections = connectionsProvider.CreateConnectionManager(this); History = historyProvider.CreateRHistory(this); Packages = packagesProvider.CreateRPackageManager(settings, this); Plots = plotsProvider.CreatePlotManager(settings, this, new FileSystem()); _operations = new RInteractiveWorkflowOperations(this, _debuggerModeTracker, Shell); _activeTextViewTracker.LastActiveTextViewChanged += LastActiveTextViewChanged; RSession.Disconnected += RSessionDisconnected; _debuggerModeTracker.EnterBreakMode += DebuggerEnteredBreakMode; _debuggerModeTracker.LeaveBreakMode += DebuggerLeftBreakMode; _settings.PropertyChanged += OnSettingsChanged; _disposableBag = DisposableBag.Create <RInteractiveWorkflow>() .Add(() => _settings.PropertyChanged -= OnSettingsChanged) .Add(() => _debuggerModeTracker.EnterBreakMode -= DebuggerEnteredBreakMode) .Add(() => _debuggerModeTracker.LeaveBreakMode -= DebuggerLeftBreakMode) .Add(() => _activeTextViewTracker.LastActiveTextViewChanged -= LastActiveTextViewChanged) .Add(() => RSession.Disconnected -= RSessionDisconnected) .Add(RSessions) .Add(Operations) .Add(Connections); }
public async Task SwitchToInvalid() { using (var sessionProvider = new RSessionProvider(_coreServices)) { var session = sessionProvider.GetOrCreate(nameof(SwitchToInvalid)); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToInvalid)); await session.EnsureHostStartedAsync(new RHostStartupInfo(), null, 1000); var switch1Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToInvalid) + "1", BrokerConnectionInfo.Create("JHFFR", @"\\JHF\F\R")); await Task.Yield(); var switch2Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToInvalid) + "2"); await ParallelTools.WhenAll(30000, switch1Task, switch2Task); switch1Task.Should().BeRanToCompletion(); switch1Task.Result.Should().BeFalse(); switch2Task.Should().BeRanToCompletion(); switch2Task.Result.Should().BeTrue(); } }
public async Task SwitchMultipleSessions() { using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) { await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchMultipleSessions)); var sessions = new IRSession[4]; for (var i = 0; i < sessions.Length; i++) { sessions[i] = sessionProvider.GetOrCreate(new Guid()); } await Task.WhenAll(sessions.Select((s, i) => s.EnsureHostStartedAsync(new RHostStartupInfo { Name = nameof(sessions) + i }, null, 30000))); await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchMultipleSessions) + "1"); sessions.Should().OnlyContain(s => s.IsHostRunning); } }
public async Task RemoveBroker() { var sessionProvider = new RSessionProvider(_services); var session1 = sessionProvider.GetOrCreate(nameof(RemoveBroker) + 1); var session2 = sessionProvider.GetOrCreate(nameof(RemoveBroker) + 2); session2.RestartOnBrokerSwitch = true; await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(RemoveBroker)).Should().BeCompletedAsync(); session1.HostStarted.Should().NotBeCompleted(); session2.HostStarted.Should().BeCompleted(); await session1.EnsureHostStartedAsync(new RHostStartupInfo(), null, 5000).Should().BeCompletedAsync(); await sessionProvider.RemoveBrokerAsync(); session1.IsHostRunning.Should().BeFalse(); session2.IsHostRunning.Should().BeFalse(); }
public async Task ConnectWhenSwitching_SwitchFailed() { using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) { await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(ConnectWhenSwitching_SwitchFailed)); var switchTask = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(ConnectWhenSwitching_SwitchFailed) + "1", @"\\JHF\F\R"); await Task.Yield(); var session = sessionProvider.GetOrCreate(new Guid()); var startHostTask = session.EnsureHostStartedAsync(new RHostStartupInfo { Name = nameof(session) }, null, 1000); await Task.WhenAll(switchTask, startHostTask); startHostTask.Should().BeRanToCompletion(); switchTask.Should().BeRanToCompletion(); switchTask.Result.Should().BeFalse(); } }
private async Task <IEnumerable <string> > ExportToPdfAsync(string[] inputs, string filePath, int width, int height) { var app = new RHostClientTestApp { PlotHandler = OnPlot, PlotDeviceCreateHandler = OnDeviceCreate, PlotDeviceDestroyHandler = OnDeviceDestroy }; using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) { await sessionProvider.TrySwitchBrokerAsync(nameof(IdeGraphicsDeviceTest)); var session = sessionProvider.GetOrCreate(Guid.NewGuid()); await session.StartHostAsync(new RHostStartupInfo { Name = _testMethod.Name }, app, 50000); foreach (var input in inputs) { using (var interaction = await session.BeginInteractionAsync()) { await interaction.RespondAsync(input.EnsureLineBreak()); } } string script = String.Format(@" device_id <- rtvs:::graphics.ide.getactivedeviceid() rtvs:::export_to_pdf(device_id, rtvs:::graphics.ide.getactiveplotid(device_id), {0}, {1}) ", width, height); var blobid = await session.EvaluateAsync <ulong>(script, REvaluationKind.Normal); using (DataTransferSession dts = new DataTransferSession(session, new FileSystem())) { await dts.FetchFileAsync(new RBlobInfo(blobid), filePath); } await session.StopHostAsync(); } // Ensure that all plot files created by the graphics device have been deleted foreach (var plot in OriginalPlotMessages) { File.Exists(plot.FilePath).Should().BeFalse(); } return(PlotFilePaths.AsReadOnly()); }