Beispiel #1
0
        public void When_command_type_registered_then_kernel_registers_envelope_type_for_serialization(bool withHandler)
        {
            Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.ResetToDefaults();

            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                = Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.Serialize(originalCommand);
            var    roundTrippedCommandEnvelope = Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.Deserialize(envelopeJson);

            roundTrippedCommandEnvelope
            .Command
            .Should()
            .BeOfType <CustomCommandTypes.FirstSubmission.MyCommand>()
            .Which
            .Info
            .Should()
            .Be(originalCommand.Info);
        }
        public async Task When_command_handler_registered_in_child_kernel_and_command_sent_to_parent_then_handler_is_executed()
        {
            using var compositeKernel = new CompositeKernel();
            var childKernel = new FakeKernel();

            compositeKernel.Add(childKernel);

            CustomCommandTypes.FirstSubmission.MyCommand commandPassedToHandler = null;
            KernelInvocationContext contextPassedToHandler = null;

            childKernel.RegisterCommandHandler <CustomCommandTypes.FirstSubmission.MyCommand>(
                (command, context) =>
            {
                commandPassedToHandler = command;
                contextPassedToHandler = context;
                return(Task.CompletedTask);
            });

            var commandSentToCompositeKernel = new CustomCommandTypes.FirstSubmission.MyCommand("xyzzy");
            await compositeKernel.SendAsync(commandSentToCompositeKernel);

            commandPassedToHandler
            .Should()
            .BeSameAs(commandSentToCompositeKernel);
            contextPassedToHandler
            .Should()
            .NotBeNull();
        }
Beispiel #3
0
        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.
            Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.ResetToDefaults();

            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                = Microsoft.DotNet.Interactive.Server.KernelCommandEnvelope.Serialize(originalCommand);
            var    roundTrippedCommandEnvelope = Microsoft.DotNet.Interactive.Server.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);
        }