Esempio n. 1
0
        public void cannot_be_started_multiple_times()
        {
            using var kernel = new ProxyKernel("proxy", new BlockingCommandAndEventReceiver(),
                                               new RecordingKernelCommandAndEventSender());

            kernel.StartAsync();

            var restart = new Action(() => kernel.StartAsync());

            restart.Should().Throw <InvalidOperationException>()
            .Which
            .Message
            .Should()
            .Be("ProxyKernel proxy is already started.");
        }
Esempio n. 2
0
            public Task <Kernel> ConnectKernelAsync(KernelInfo kernelInfo)
            {
                var proxy = new ProxyKernel(kernelInfo.LocalName, _defaultReceiver.CreateChildReceiver(), _defaultSender);
                var _     = proxy.StartAsync();

                return(Task.FromResult((Kernel)proxy));
            }
Esempio n. 3
0
        public async Task <Kernel> ConnectKernelAsync(KernelInfo kernelInfo)
        {
            // QUESTION: (ConnectKernelAsync) tests?
            var hubConnection = new HubConnectionBuilder()
                                .WithUrl(HubUrl)
                                .Build();

            await hubConnection.StartAsync();

            await hubConnection.SendAsync("connect");

            var receiver    = new KernelCommandAndEventSignalRHubConnectionReceiver(hubConnection);
            var sender      = new KernelCommandAndEventSignalRHubConnectionSender(hubConnection);
            var proxyKernel = new ProxyKernel(kernelInfo.LocalName, receiver, sender);

            var _ = proxyKernel.StartAsync();

            proxyKernel.RegisterForDisposal(receiver);
            proxyKernel.RegisterForDisposal(async() =>
            {
                await hubConnection.DisposeAsync();
            });

            return(proxyKernel);
        }
    public override async Task <Kernel> ConnectKernelAsync(KernelInfo kernelInfo)
    {
        if (_receiver is not null)
        {
            var kernel = new ProxyKernel(kernelInfo.LocalName, _receiver.CreateChildReceiver(), _sender);
            var _      = kernel.StartAsync();
            return(kernel);
        }
        else
        {
            // QUESTION: (ConnectKernelAsync) tests?
            var command   = Command[0];
            var arguments = string.Join(" ", Command.Skip(1));

            var psi = new ProcessStartInfo
            {
                FileName               = command,
                Arguments              = arguments,
                WorkingDirectory       = WorkingDirectory.FullName,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                StandardOutputEncoding = Encoding.UTF8
            };
            _process = new Process {
                StartInfo = psi
            };
            _process.EnableRaisingEvents = true;
            var stdErr = new StringBuilder();
            _process.ErrorDataReceived += (o, args) => { stdErr.Append(args.Data); };
            await Task.Yield();

            _process.Start();
            _process.BeginErrorReadLine();

            _receiver = new MultiplexingKernelCommandAndEventReceiver(
                new KernelCommandAndEventTextReceiver(_process.StandardOutput));
            _sender = new KernelCommandAndEventTextStreamSender(_process.StandardInput);
            var kernel = new ProxyKernel(kernelInfo.LocalName, _receiver, _sender);

            var r = _receiver.CreateChildReceiver();
            var _ = kernel.StartAsync();

            var checkReady = Task.Run(async() =>
            {
                await foreach (var eoc in r.CommandsAndEventsAsync(CancellationToken.None))
                {
                    if (eoc.Event is KernelReady)
                    {
                        return;
                    }
                }
            });

            var checkProcessError = Task.Run(async() =>
            {
                while (!checkReady.IsCompleted)
                {
                    await Task.Delay(200);
                    if (_process.HasExited)
                    {
                        if (_process.ExitCode != 0)
                        {
                            throw new CommandLineInvocationException(
                                new CommandLineResult(_process.ExitCode,
                                                      error: stdErr.ToString().Split(new[] { '\r', '\n' },
                                                                                     StringSplitOptions.RemoveEmptyEntries)));
                        }
                    }
                }
            });

            await Task.WhenAny(checkProcessError, checkReady);

            return(kernel);
        }
    }