public async Task ConnectWhenSwitching()
        {
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                var guid1      = new Guid();
                var guid2      = new Guid();
                var session1   = sessionProvider.GetOrCreate(guid1);
                var session2   = sessionProvider.GetOrCreate(guid2);
                var switchTask = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(ConnectWhenSwitching));

                var startHost1Task = Task.Delay(50).ContinueWith(t => session1.EnsureHostStartedAsync(new RHostStartupInfo {
                    Name = nameof(session1)
                }, null, 1000)).Unwrap();

                var startHost2Task = Task.Delay(100).ContinueWith(t => session2.EnsureHostStartedAsync(new RHostStartupInfo {
                    Name = nameof(session2)
                }, null, 1000)).Unwrap();

                await Task.WhenAll(switchTask, startHost1Task, startHost2Task);

                startHost1Task.Should().BeRanToCompletion();
                startHost2Task.Should().BeRanToCompletion();

                await Task.WhenAll(session1.HostStarted, session2.HostStarted);
            }
        }
Example #2
0
        public async Task ValidateEncodings()
        {
            var etc       = new EncodingTypeConverter();
            var codePages = etc.GetStandardValues();

            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                await sessionProvider.TrySwitchBrokerAsync(nameof(ValidateEncodings));

                using (var script = new VsRHostScript(sessionProvider)) {
                    foreach (var cp in codePages)
                    {
                        if ((int)cp > 0)
                        {
                            var expression = Invariant($"Sys.setlocale('LC_CTYPE', '.{cp}')\n");
                            using (var inter = await script.Session.BeginInteractionAsync()) {
                                await inter.RespondAsync(expression);
                            }

                            var res = await script.Session.EvaluateAsync("Sys.getlocale()", REvaluationKind.Normal);

                            var s = res.Result.ToString();

                            s.Should().NotBeNull().And.Contain(cp.ToString());
                        }
                    }
                }
            }
        }
Example #3
0
        public ValuesTest(TestMethodFixture testMethod)
        {
            _testMethod      = testMethod.MethodInfo;
            _sessionProvider = new RSessionProvider(TestCoreServices.CreateReal());

            _session = _sessionProvider.GetOrCreate(Guid.NewGuid());
        }
        public async Task SwitchWhenConnecting()
        {
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                var guid    = new Guid();
                var session = sessionProvider.GetOrCreate(guid);

                await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchWhenConnecting));

                var startHostTask = session.EnsureHostStartedAsync(new RHostStartupInfo {
                    Name = nameof(session)
                }, null, 1000);

                await Task.Yield();

                var switch1Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchWhenConnecting) + "1");
                var switch2Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchWhenConnecting) + "2");

                await Task.WhenAll(startHostTask, switch1Task, switch2Task);

                switch1Task.Status.Should().Be(TaskStatus.RanToCompletion);
                switch2Task.Status.Should().Be(TaskStatus.RanToCompletion);
                startHostTask.Status.Should().Be(TaskStatus.RanToCompletion);

                await session.HostStarted;
                session.HostStarted.Should().BeRanToCompletion();
            }
        }
Example #5
0
        public ProjectCommandsTest()
        {
            var sessionProvider     = new RSessionProvider(TestCoreServices.CreateReal());
            var connectionsProvider = VsAppShell.Current.ExportProvider.GetExportedValue <IConnectionManagerProvider>();
            var historyProvider     = VsAppShell.Current.ExportProvider.GetExportedValue <IRHistoryProvider>();
            var packagesProvider    = VsAppShell.Current.ExportProvider.GetExportedValue <IRPackageManagerProvider>();

            _interactiveWorkflowProvider = TestRInteractiveWorkflowProviderFactory.Create(nameof(ProjectCommandsTest), sessionProvider, connectionsProvider, historyProvider, packagesProvider);
        }
 public void ParallelAccess()
 {
     using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
         var guids = new[] { new Guid(), new Guid() };
         ParallelTools.Invoke(100, i => {
             var session = sessionProvider.GetOrCreate(guids[i % 2]);
             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>();
        }
Example #9
0
        public CurrentDirectoryTest()
        {
            _sessionProvider = new RSessionProvider(TestCoreServices.CreateReal());

            var connectionsProvider   = VsAppShell.Current.ExportProvider.GetExportedValue <IConnectionManagerProvider>();
            var historyProvider       = VsAppShell.Current.ExportProvider.GetExportedValue <IRHistoryProvider>();
            var packagesProvider      = VsAppShell.Current.ExportProvider.GetExportedValue <IRPackageManagerProvider>();
            var plotsProvider         = VsAppShell.Current.ExportProvider.GetExportedValue <IRPlotManagerProvider>();
            var activeTextViewTracker = new ActiveTextViewTrackerMock(string.Empty, string.Empty);
            var debuggerModeTracker   = new TestDebuggerModeTracker();

            _interactiveWorkflow = UIThreadHelper.Instance.Invoke(() => new RInteractiveWorkflow(
                                                                      _sessionProvider, connectionsProvider, historyProvider, packagesProvider, plotsProvider, activeTextViewTracker,
                                                                      debuggerModeTracker, VsAppShell.Current, RToolsSettings.Current, null, () => { }));
        }
        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);
            }
        }
Example #11
0
        public async Task StopBeforeInitialized_RHostMissing()
        {
            var brokerClient = new LocalBrokerClient(nameof(RSessionTest), @"C:\",
                                                     TestCoreServices.CreateReal(), new NullConsole(), Environment.SystemDirectory);
            var         session = new RSession(0, brokerClient, () => { });
            Func <Task> start   = () => session.StartHostAsync(new RHostStartupInfo {
                Name = _testMethod.Name
            }, null, 10000);
            var startTask = Task.Run(start).SilenceException <RHostBrokerBinaryMissingException>();

            await session.StopHostAsync();

            session.IsHostRunning.Should().BeFalse();

            await startTask;
        }
        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();
            }
        }
Example #13
0
        private IRInteractiveWorkflow CreateRInteractiveWorkflow()
        {
            var sessionProvider = _sessionProvider ?? new RSessionProvider(TestCoreServices.CreateReal());

            return(new RInteractiveWorkflow(sessionProvider
                                            , _connectionManagerProvider
                                            , _historyProvider
                                            , _packagesProvider
                                            , _plotsProvider
                                            , _activeTextViewTracker
                                            , _debuggerModeTracker
                                            , _shell
                                            , _settings
                                            , _wss
                                            , () => DisposeInstance(sessionProvider)));
        }
        public async Task SwitchBrokerWithCancellation(int timeout)
        {
            using (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, 1000);

                var result = await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation) + "1",
                                                                        cancellationToken : new CancellationTokenSource(timeout).Token);

                result.Should().BeFalse();
            }
        }
Example #15
0
        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();
        }
Example #16
0
        private async Task ExecuteInSession(string[] inputs, IRSessionCallback app)
        {
            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());
                    }
                }

                await session.StopHostAsync();
            }
        }
        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();
            }
        }
Example #18
0
        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);
            }
        }
Example #19
0
        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());
        }
        public async Task SwitchToInvalid()
        {
            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", @"\\JHF\F\R");
                await Task.Yield();

                var switch2Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToTheSameBroker) + "2");
                await Task.WhenAll(switch1Task, switch2Task);

                switch1Task.Should().BeRanToCompletion();
                switch1Task.Result.Should().BeFalse();
                switch2Task.Should().BeRanToCompletion();
                switch2Task.Result.Should().BeTrue();
            }
        }
Example #21
0
        private async Task <IEnumerable <string> > ExportToImageAsync(string[] inputs, string[] format, string[] paths, int widthInPixels, int heightInPixels, int resolution)
        {
            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());
                    }
                }

                for (int i = 0; i < format.Length; ++i)
                {
                    await ExportToImageAsync(session, format[i], paths[i], widthInPixels, heightInPixels, resolution);
                }

                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());
        }
Example #22
0
        private async Task <XDocument> RunGraphicsTest(string code, string outputFilePath)
        {
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                await sessionProvider.TrySwitchBrokerAsync(nameof(XamlGraphicsDeviceTest));

                var session = sessionProvider.GetOrCreate(Guid.NewGuid());
                await session.StartHostAsync(new RHostStartupInfo {
                    Name = _testMethod.Name
                }, new RHostClientTestApp(), 50000);

                using (var interaction = await session.BeginInteractionAsync()) {
                    await interaction.RespondAsync(code);
                }

                await session.StopHostAsync();
            }

            File.Exists(outputFilePath).Should().BeTrue();
            var doc    = XDocument.Load(outputFilePath);
            var docXml = doc.ToString();

            Console.WriteLine(docXml);
            return(doc);
        }
Example #23
0
 public SessionProviderFixture()
 {
     SessionProvider = new RSessionProvider(TestCoreServices.CreateReal());
 }
Example #24
0
 private static IBrokerClient CreateLocalBrokerClient(string name)
 {
     return(new LocalBrokerClient(name, new RInstallation().GetCompatibleEngines().FirstOrDefault()?.InstallPath, TestCoreServices.CreateReal(), new NullConsole()));
 }
 public CoreServicesFixture()
 {
     _services = TestCoreServices.CreateReal();
     _log      = new LogProxy();
     _log.SetLog(_services.Log);
 }
Example #26
0
        public void StartRHostMissing()
        {
            var         brokerClient = new LocalBrokerClient(nameof(RSessionTest), @"C:\", TestCoreServices.CreateReal(), new NullConsole(), Environment.SystemDirectory);
            var         session      = new RSession(0, brokerClient, () => { });
            Func <Task> start        = () => session.StartHostAsync(new RHostStartupInfo {
                Name = _testMethod.Name
            }, null, 10000);

            start.ShouldThrow <RHostBrokerBinaryMissingException>();
        }