コード例 #1
0
        public void ProcessMessages_PluginMessage_SendsErrorWhenUnregistered()
        {
            var    assemblyLoadContext   = CreateTestAssemblyLoadContext <MessageTestPlugin>();
            var    serviceProvider       = new TestServiceProvider();
            object rawWrappedData        = null;
            var    pluginHandler         = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            var    registerPluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(MessageTestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };
            var unregisterPluginMessage = new PluginMessage
            {
                MessageName = "UnregisterPlugin",
                PluginId    = RandomGuidId
            };
            var pluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "Data", "Hello Plugin" },
                },
                MessageName = "PluginMessage",
                PluginId    = RandomGuidId
            };
            var expectedErrorMessage =
                $"Message received for unregistered plugin id '{RandomGuidId}'. Plugins must first be registered " +
                "before they can receive messages.";

            pluginHandler.OnReceive(registerPluginMessage);
            pluginHandler.OnReceive(unregisterPluginMessage);
            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);
        }
コード例 #2
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);
        }
コード例 #3
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);
        }
コード例 #4
0
        public void ProcessMessages_PluginMessage_ProcessesMessages()
        {
            var    assemblyLoadContext   = CreateTestAssemblyLoadContext <MessageTestPlugin>();
            var    serviceProvider       = new TestServiceProvider();
            object rawMessageBrokerData  = null;
            var    pluginHandler         = new PluginHandler(serviceProvider, (data) => rawMessageBrokerData = data);
            var    registerPluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(MessageTestPlugin).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.NotNull(rawMessageBrokerData);
            var messageBrokerData = Assert.IsType <PluginMessageBroker.PluginMessageWrapperData>(rawMessageBrokerData);

            Assert.Equal(RandomGuidId, messageBrokerData.PluginId);
            var actualMessage = (string)messageBrokerData.Data;

            Assert.Equal("Hello Plugin!", actualMessage, StringComparer.Ordinal);
        }
コード例 #5
0
        public void ProcessMessages_UnregisterPlugin_SendsErrorWhenUnregisteringPluginMoreThanOnce()
        {
            var    assemblyLoadContext   = CreateTestAssemblyLoadContext <TestPlugin>();
            var    serviceProvider       = new TestServiceProvider();
            object rawWrappedData        = null;
            var    pluginHandler         = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            var    registerPluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(TestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };
            var unregisterPluginMessage = new PluginMessage
            {
                MessageName = "UnregisterPlugin",
                PluginId    = RandomGuidId
            };
            var expectedErrorMessage =
                $"No plugin with id '{RandomGuidId}' has been registered. Cannot unregister plugin.";

            pluginHandler.OnReceive(registerPluginMessage);
            pluginHandler.OnReceive(unregisterPluginMessage);
            pluginHandler.OnReceive(unregisterPluginMessage);
            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("UnregisterPlugin", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.Equal(expectedErrorMessage, responseMessage.Error, StringComparer.Ordinal);
        }
コード例 #6
0
        public void ProcessMessages_RegisterPlugin_CreatesPluginWithAssemblyLoadContext()
        {
            var    assemblyLoadContext = CreateTestAssemblyLoadContext <AssemblyLoadContextRelayTestPlugin>();
            var    serviceLookups      = new Dictionary <Type, object>();
            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(AssemblyLoadContextRelayTestPlugin).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.NotNull(rawWrappedData);
            var wrappedData = Assert.IsType <PluginMessageBroker.PluginMessageWrapperData>(rawWrappedData);

            Assert.Equal(RandomGuidId, wrappedData.PluginId);
            Assert.Same(assemblyLoadContext, wrappedData.Data);
        }
コード例 #7
0
        public void OnReceive_DoesNotProcessMessages()
        {
            object rawMessageBrokerData  = null;
            var    serviceProvider       = new TestServiceProvider();
            var    pluginHandler         = new PluginHandler(serviceProvider, (data) => rawMessageBrokerData = data);
            var    registerPluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(TestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };
            var unregisterPluginMessage = new PluginMessage
            {
                MessageName = "UnregisterPlugin",
                PluginId    = RandomGuidId
            };
            var pluginMessage = new PluginMessage
            {
                Data = new JObject
                {
                    { "Data", "Hello Plugin" },
                },
                MessageName = "PluginMessage",
                PluginId    = RandomGuidId
            };

            pluginHandler.OnReceive(registerPluginMessage);
            pluginHandler.OnReceive(pluginMessage);
            pluginHandler.OnReceive(unregisterPluginMessage);

            Assert.Null(rawMessageBrokerData);
        }
コード例 #8
0
        public void ProcessMessages_UnregisterPlugin_RemovesFaultedPlugins()
        {
            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
            };
            var unregisterPluginMessage = new PluginMessage
            {
                MessageName = "UnregisterPlugin",
                PluginId    = RandomGuidId
            };

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

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

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

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

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

            messageBrokerData = Assert.IsType <PluginMessageBroker.PluginMessageWrapperData>(rawMessageBrokerData);
            Assert.Equal(RandomGuidId, messageBrokerData.PluginId);
            responseMessage = Assert.IsType <PluginResponseMessage>(messageBrokerData.Data);
            Assert.True(responseMessage.Success);
            Assert.Equal("UnregisterPlugin", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.Null(responseMessage.Error);
            Assert.False(creationChecker.Created);
        }