コード例 #1
0
 public MessageBrokerCreationTestPlugin(
     IPluginMessageBroker messageBroker,
     PluginTypeCreationChecker creationChecker)
     : base(creationChecker)
 {
     _messageBroker = messageBroker;
 }
コード例 #2
0
        public void ProcessMessages_RegisterPlugin_DoesNotCacheCreatedPlugin()
        {
            var assemblyLoadContext = CreateTestAssemblyLoadContext <CreationTestPlugin>();
            var creationChecker     = new PluginTypeCreationChecker();
            var serviceLookups      = new Dictionary <Type, object>
            {
                { typeof(PluginTypeCreationChecker), creationChecker }
            };
            var serviceProvider = new TestServiceProvider(serviceLookups);
            var pluginHandler   = new PluginHandler(serviceProvider, (_) => { });
            var pluginMessage   = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(CreationTestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };

            pluginHandler.OnReceive(pluginMessage);
            pluginHandler.ProcessMessages(assemblyLoadContext);
            Assert.True(creationChecker.Created);
            creationChecker.Created = false;
            pluginHandler.OnReceive(pluginMessage);
            pluginHandler.ProcessMessages(assemblyLoadContext);
            Assert.True(creationChecker.Created);
        }
コード例 #3
0
        public void TryRegisterFaultedPlugins_RecoversFaultedPluginRegistrations()
        {
            var pluginType      = typeof(MessageBrokerCreationTestPlugin);
            var typeNameLookups = new Dictionary <string, Type>
            {
                { pluginType.FullName, pluginType }
            };
            var testAssembly    = new TestAssembly(typeNameLookups);
            var assemblyLookups = new Dictionary <string, Assembly>
            {
                { "CustomAssembly", testAssembly }
            };
            var assemblyLoadContext = new FailureAssemblyLoadContext(assemblyLookups);
            var creationChecker     = new PluginTypeCreationChecker();
            var serviceLookups      = new Dictionary <Type, object>
            {
                { typeof(PluginTypeCreationChecker), creationChecker }
            };
            var    serviceProvider       = new TestServiceProvider(serviceLookups);
            object rawMessageBrokerData  = null;
            var    pluginHandler         = new PluginHandler(serviceProvider, (data) => rawMessageBrokerData = data);
            var    registerPluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(MessageBrokerCreationTestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };

            pluginHandler.OnReceive(registerPluginMessage);
            pluginHandler.ProcessMessages(assemblyLoadContext);

            var messageBrokerData = Assert.IsType <PluginMessageBroker.PluginMessageWrapperData>(rawMessageBrokerData);

            Assert.Equal(RandomGuidId, messageBrokerData.PluginId);
            var responseMessage = Assert.IsType <RegisterPluginResponseMessage>(messageBrokerData.Data);

            Assert.False(responseMessage.Success);
            Assert.Equal("RegisterPlugin", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.NotEmpty(responseMessage.Error);
            Assert.False(creationChecker.Created);

            pluginHandler.TryRegisterFaultedPlugins(assemblyLoadContext);

            messageBrokerData = Assert.IsType <PluginMessageBroker.PluginMessageWrapperData>(rawMessageBrokerData);
            Assert.Equal(RandomGuidId, messageBrokerData.PluginId);
            responseMessage = Assert.IsType <RegisterPluginResponseMessage>(messageBrokerData.Data);
            Assert.True(responseMessage.Success);
            Assert.Equal("RegisterPlugin", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.Equal(1, responseMessage.Protocol);
            Assert.Null(responseMessage.Error);
            Assert.True(creationChecker.Created);
        }
コード例 #4
0
        public void ProcessMessages_PluginMessage_SendsErrorForPluginExceptions()
        {
            var assemblyLoadContext = CreateTestAssemblyLoadContext <CreationTestPlugin>();
            var creationChecker     = new PluginTypeCreationChecker();
            var serviceLookups      = new Dictionary <Type, object>
            {
                { typeof(PluginTypeCreationChecker), creationChecker }
            };
            var    serviceProvider       = new TestServiceProvider(serviceLookups);
            object rawWrappedData        = null;
            var    pluginHandler         = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            var    registerPluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(CreationTestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };
            var pluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "Data", "Hello Plugin" },
                },
                MessageName = "PluginMessage",
                PluginId    = RandomGuidId
            };
            var expectedErrorMessage =
                $"Plugin '{RandomGuidId}' encountered an exception when processing a message. Exception message: 'Cannot process messages.'";

            pluginHandler.OnReceive(registerPluginMessage);
            pluginHandler.OnReceive(pluginMessage);
            pluginHandler.ProcessMessages(assemblyLoadContext);

            var messageBrokerData = Assert.IsType <PluginMessageBroker.PluginMessageWrapperData>(rawWrappedData);

            Assert.Equal(RandomGuidId, messageBrokerData.PluginId);
            var responseMessage = Assert.IsType <PluginResponseMessage>(messageBrokerData.Data);

            Assert.False(responseMessage.Success);
            Assert.Equal("PluginMessage", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.Equal(expectedErrorMessage, responseMessage.Error, StringComparer.Ordinal);
        }
コード例 #5
0
        public void ProcessMessages_RegisterPlugin_CreatesPluginWithCustomMessageBroker()
        {
            var    assemblyLoadContext  = CreateTestAssemblyLoadContext <MessageBrokerCreationTestPlugin>();
            var    creationChecker      = new PluginTypeCreationChecker();
            object rawMessageBrokerData = null;
            var    pluginMessageBroker  = new PluginMessageBroker(RandomGuidId, (data) => rawMessageBrokerData = data);
            var    serviceLookups       = new Dictionary <Type, object>
            {
                { typeof(PluginTypeCreationChecker), creationChecker },
                { typeof(IPluginMessageBroker), pluginMessageBroker }
            };
            var serviceProvider       = new TestServiceProvider(serviceLookups);
            var pluginHandler         = new PluginHandler(serviceProvider, (_) => { });
            var registerPluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(MessageBrokerCreationTestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };
            var pluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "Data", "Hello Plugin" },
                },
                MessageName = "PluginMessage",
                PluginId    = RandomGuidId
            };

            pluginHandler.OnReceive(registerPluginMessage);
            pluginHandler.OnReceive(pluginMessage);
            pluginHandler.ProcessMessages(assemblyLoadContext);

            Assert.True(creationChecker.Created);
            Assert.NotNull(rawMessageBrokerData);
            var messageBrokerData = Assert.IsType <PluginMessageBroker.PluginMessageWrapperData>(rawMessageBrokerData);

            Assert.Equal(RandomGuidId, messageBrokerData.PluginId);
            Assert.Equal("Created", messageBrokerData.Data.ToString(), StringComparer.Ordinal);
        }
コード例 #6
0
 public CreationTestPlugin(PluginTypeCreationChecker creationChecker)
 {
     creationChecker.Created = true;
 }