public void When_command_type_registered_then_kernel_registers_envelope_type_for_serialization(bool withHandler)
        {
            KernelCommandEnvelope.RegisterDefaults();

            using var kernel = new FakeKernel();

            if (withHandler)
            {
                kernel.RegisterCommandHandler <CustomCommandTypes.FirstSubmission.MyCommand>(
                    (_, _) => Task.CompletedTask);
            }
            else
            {
                kernel.RegisterCommandType <CustomCommandTypes.FirstSubmission.MyCommand>();
            }

            var    originalCommand             = new CustomCommandTypes.FirstSubmission.MyCommand("xyzzy");
            string envelopeJson                = KernelCommandEnvelope.Serialize(originalCommand);
            var    roundTrippedCommandEnvelope = KernelCommandEnvelope.Deserialize(envelopeJson);

            roundTrippedCommandEnvelope
            .Command
            .Should()
            .BeOfType <CustomCommandTypes.FirstSubmission.MyCommand>()
            .Which
            .Info
            .Should()
            .Be(originalCommand.Info);
        }
        public async Task stdio_command_extends_the_protocol_with_quit_command()
        {
            await _parser.InvokeAsync("stdio");

            var envelope = KernelCommandEnvelope.Deserialize(@"{ commandType: ""Quit"", command : { } }");

            envelope.Command.Should()
            .BeOfType <Quit>();
        }
Esempio n. 3
0
        public void All_command_types_are_round_trip_serializable(IKernelCommand command)
        {
            var originalEnvelope = KernelCommandEnvelope.Create(command);

            var json = KernelCommandEnvelope.Serialize(originalEnvelope);

            _output.WriteLine(json);

            var deserializedEnvelope = KernelCommandEnvelope.Deserialize(json);

            deserializedEnvelope
            .Should()
            .BeEquivalentTo(originalEnvelope);
        }
        public void All_command_types_are_round_trip_serializable(KernelCommand command)
        {
            var originalEnvelope = KernelCommandEnvelope.Create(command);

            var json = KernelCommandEnvelope.Serialize(originalEnvelope);

            _output.WriteLine(json);

            var deserializedEnvelope = KernelCommandEnvelope.Deserialize(json);

            deserializedEnvelope
            .Should()
            .BeEquivalentTo(originalEnvelope,
                            o => o.Excluding(e => e.Command.Properties)
                            .Excluding(e => e.Command.Handler));
        }
Esempio n. 5
0
        public async IAsyncEnumerable <CommandOrEvent> CommandsOrEventsAsync([EnumeratorCancellation] CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                KernelCommand kernelCommand = null;
                KernelEvent   kernelEvent   = null;

                var message = await ReadMessageAsync(cancellationToken);

                if (string.IsNullOrWhiteSpace(message))
                {
                    continue;
                }

                var isParseError = false;
                try
                {
                    var jsonObject = JsonDocument.Parse(message).RootElement;
                    if (IsEventEnvelope(jsonObject))
                    {
                        var kernelEventEnvelope = KernelEventEnvelope.Deserialize(jsonObject);
                        kernelEvent = kernelEventEnvelope.Event;
                    }
                    else if (IsCommandEnvelope(jsonObject))
                    {
                        var kernelCommandEnvelope = KernelCommandEnvelope.Deserialize(jsonObject);
                        kernelCommand = kernelCommandEnvelope.Command;
                    }
                    else
                    {
                        kernelEvent = new DiagnosticLogEntryProduced(
                            $"Expected {nameof(KernelCommandEnvelope)} or {nameof(KernelEventEnvelope)} but received: \n{message}", KernelCommand.None);
                        isParseError = true;
                    }
                }
                catch (Exception ex)
                {
                    kernelEvent = new DiagnosticLogEntryProduced(
                        $"Error while parsing Envelope: {message} \n{ex.Message}", KernelCommand.None);
                    isParseError = true;
                }

                yield return(kernelCommand is null ? new CommandOrEvent(kernelEvent, isParseError) : new CommandOrEvent(kernelCommand));
            }
        }
Esempio n. 6
0
        protected override async Task <CommandOrEvent> ReadCommandOrEventAsync(CancellationToken cancellationToken)
        {
            KernelCommand kernelCommand = null;
            KernelEvent   kernelEvent   = null;

            var message = await ReadMessageAsync(cancellationToken);

            if (string.IsNullOrWhiteSpace(message))
            {
                return(null);
            }

            var isParseError = false;

            try
            {
                var jsonObject = JsonDocument.Parse(message).RootElement;
                if (IsEventEnvelope(jsonObject))
                {
                    var kernelEventEnvelope = KernelEventEnvelope.Deserialize(jsonObject);
                    kernelEvent = kernelEventEnvelope.Event;
                }
                else if (IsCommandEnvelope(jsonObject))
                {
                    var kernelCommandEnvelope = KernelCommandEnvelope.Deserialize(jsonObject);
                    kernelCommand = kernelCommandEnvelope.Command;
                }
                else
                {
                    kernelEvent = new DiagnosticLogEntryProduced(
                        $"Expected {nameof(KernelCommandEnvelope)} or {nameof(KernelEventEnvelope)} but received: \n{message}", KernelCommand.None);
                    isParseError = true;
                }
            }
            catch (Exception ex)
            {
                kernelEvent = new DiagnosticLogEntryProduced(
                    $"Error while parsing Envelope: {message} \n{ex.Message}", KernelCommand.None);
                isParseError = true;
            }

            return(kernelCommand is null ? new CommandOrEvent(kernelEvent, isParseError) : new CommandOrEvent(kernelCommand));
        }
        public void When_command_type_reregistered_with_changed_type_command_then_kernel_registers_updated_envelope_type_for_serialization(bool withHandler)
        {
            // Notebook authors should be able to develop their custom commands experimentally and progressively,
            // so we don't want any "you have to restart your kernel now" situations just because you already
            // called RegisterCommandHandler once for a particular command type.
            KernelCommandEnvelope.RegisterDefaults();

            using var kernel = new FakeKernel();

            if (withHandler)
            {
                kernel.RegisterCommandHandler <CustomCommandTypes.FirstSubmission.MyCommand>(
                    (_, _) => Task.CompletedTask);
                kernel.RegisterCommandHandler <CustomCommandTypes.SecondSubmission.MyCommand>(
                    (_, _) => Task.CompletedTask);
            }
            else
            {
                kernel.RegisterCommandType <CustomCommandTypes.FirstSubmission.MyCommand>();
                kernel.RegisterCommandType <CustomCommandTypes.SecondSubmission.MyCommand>();
            }

            var    originalCommand             = new CustomCommandTypes.SecondSubmission.MyCommand("xyzzy", 42);
            string envelopeJson                = KernelCommandEnvelope.Serialize(originalCommand);
            var    roundTrippedCommandEnvelope = KernelCommandEnvelope.Deserialize(envelopeJson);

            roundTrippedCommandEnvelope
            .Command
            .Should()
            .BeOfType <CustomCommandTypes.SecondSubmission.MyCommand>()
            .Which
            .Info
            .Should()
            .Be(originalCommand.Info);
            roundTrippedCommandEnvelope
            .Command
            .As <CustomCommandTypes.SecondSubmission.MyCommand>()
            .AdditionalProperty
            .Should()
            .Be(originalCommand.AdditionalProperty);
        }
Esempio n. 8
0
 public async Task SubmitCommand(string kernelCommandEnvelope)
 {
     var envelope = KernelCommandEnvelope.Deserialize(kernelCommandEnvelope);
     var command  = envelope.Command;
     await _connection.Kernel.SendAsync(command);
 }
Esempio n. 9
0
 public Task SendAsync(KernelCommand kernelCommand, CancellationToken cancellationToken)
 {
     _onSendAsync?.Invoke(new CommandOrEvent(KernelCommandEnvelope.Deserialize(KernelCommandEnvelope.Serialize(kernelCommand)).Command));
     return(Task.CompletedTask);
 }