TrySwitchBrokerAsync() public method

public TrySwitchBrokerAsync ( string name, string path = null, CancellationToken cancellationToken = default(CancellationToken) ) : Task
name string
path string
cancellationToken System.Threading.CancellationToken
return Task
Exemplo n.º 1
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());
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        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 ParallelTools.WhenAll(30000, switchTask, startHost1Task, startHost2Task);

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

                await ParallelTools.WhenAll(30000, session1.HostStarted, session2.HostStarted);
            }
        }
Exemplo n.º 3
0
        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 ParallelTools.WhenAll(switchTask, startHostTask);

                startHostTask.Should().BeRanToCompletion();
                switchTask.Should().BeRanToCompletion();
                switchTask.Result.Should().BeFalse();
            }
        }
Exemplo n.º 4
0
        public async Task SwitchBroker_DisposeSessionProvider(int timeout) {
            var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal());
            var guid = new Guid();
            var session1 = sessionProvider.GetOrCreate(guid);
            var session2 = sessionProvider.GetOrCreate(guid);
            session2.RestartOnBrokerSwitch = true;

            await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation));
            await session1.EnsureHostStartedAsync(new RHostStartupInfo {
                Name = nameof(session1)
            }, null, 5000).Should().BeCompletedAsync();
            await session2.HostStarted.Should().BeCompletedAsync();

            var sessionProviderDisposeTask = Task.Delay(timeout).ContinueWith(t => sessionProvider.Dispose());
            var result = await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation) + "1");
            await sessionProviderDisposeTask;

            result.Should().BeFalse();
        }
Exemplo n.º 5
0
        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();
            }
        }
Exemplo n.º 6
0
        public async Task SwitchBroker_DisposeSession(int timeout, bool restartOnBrokerSwitch) {
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                var guid = new Guid();
                var session = sessionProvider.GetOrCreate(guid);
                session.RestartOnBrokerSwitch = restartOnBrokerSwitch;

                await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation));
                await session.EnsureHostStartedAsync(new RHostStartupInfo {
                    Name = nameof(session)
                }, null, 5000);

                var sessionDisposeTask = Task.Delay(timeout).ContinueWith(t => session.Dispose());
                var result = await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchBrokerWithCancellation) + "1");
                await sessionDisposeTask;

                result.Should().BeTrue();
            }
        }
Exemplo n.º 7
0
        public async Task SwitchToTheSameBroker() {
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                var guid = new Guid();
                var session = sessionProvider.GetOrCreate(guid);
                session.RestartOnBrokerSwitch = true;

                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 ParallelTools.WhenAll(30000, switch1Task, switch2Task);

                switch1Task.Status.Should().Be(TaskStatus.RanToCompletion);
                switch2Task.Status.Should().Be(TaskStatus.RanToCompletion);
            }
        }
Exemplo n.º 8
0
        public async Task SwitchToTheSameBroker_NoSessions() {
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                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);
            }
        }
Exemplo n.º 9
0
        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(SwitchToInvalid));
                await session.EnsureHostStartedAsync(new RHostStartupInfo {
                    Name = nameof(session)
                }, null, 1000);

                var switch1Task = sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchToInvalid) + "1", @"\\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();
            }
        }
Exemplo n.º 10
0
        public async Task SwitchWhileEnsureHostStartedAsyncFailed() {
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                var guid = new Guid();
                var session = sessionProvider.GetOrCreate(guid);
                session.RestartOnBrokerSwitch = true;

                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();
            }
        }
Exemplo n.º 11
0
        public async Task SwitchMultipleSessions() {
            using (var sessionProvider = new RSessionProvider(TestCoreServices.CreateReal())) {
                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(new Guid());
                    sessions[i].RestartOnBrokerSwitch = true;
                }

                await ParallelTools.WhenAll(30000, sessions.Select((s, i) => s.EnsureHostStartedAsync(new RHostStartupInfo {
                    Name = nameof(sessions) + i
                }, null, 30000)).ToArray());

                await sessionProvider.TrySwitchBrokerAsync(nameof(RSessionProviderTest) + nameof(SwitchMultipleSessions) + "1").Should().BeCompletedAsync();

                sessions.Should().OnlyContain(s => s.IsHostRunning);
            }
        }
Exemplo n.º 12
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, true, null, CancellationToken.None);
                }

                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();
        }
Exemplo n.º 13
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();
        }
Exemplo n.º 14
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();
            }
        }
Exemplo n.º 15
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;
        }