Exemple #1
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);
        }
Exemple #2
0
        public void ProcessMessages_PluginMessage_SendsErrorWhenPluginNotRegistered()
        {
            var    assemblyLoadContext = CreateTestAssemblyLoadContext <TestPlugin>();
            var    serviceProvider     = new TestServiceProvider();
            object rawWrappedData      = null;
            var    pluginHandler       = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            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(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);
        }
Exemple #3
0
        public void ProcessMessages_UnregisterPlugin_SendsErrorWhenUnregisteringUnknownPlugin()
        {
            var    assemblyLoadContext     = CreateTestAssemblyLoadContext <TestPlugin>();
            var    serviceProvider         = new TestServiceProvider();
            object rawWrappedData          = null;
            var    pluginHandler           = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            var    unregisterPluginMessage = new PluginMessage
            {
                MessageName = "UnregisterPlugin",
                PluginId    = RandomGuidId
            };
            var expectedErrorMessage =
                $"No plugin with id '{RandomGuidId}' has been registered. Cannot unregister plugin.";

            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);
        }
Exemple #4
0
        public void ProcessMessages_RegisterPlugin_SendsErrorOnInvalidPluginTypes()
        {
            var    assemblyLoadContext = CreateTestAssemblyLoadContext <InvalidTestPlugin>();
            var    serviceProvider     = new TestServiceProvider();
            object rawWrappedData      = null;
            var    pluginHandler       = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            var    pluginMessage       = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(InvalidTestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };
            var expectedErrorMessage =
                $"Cannot process plugin message. Plugin id '{RandomGuidId}' of type " +
                "'Microsoft.Dnx.DesignTimeHost.PluginHandlerFacts+InvalidTestPlugin' must be assignable " +
                "to type 'Microsoft.Dnx.DesignTimeHost.IPlugin'.";

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

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

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

            Assert.False(responseMessage.Success);
            Assert.Equal("RegisterPlugin", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.Equal(expectedErrorMessage, responseMessage.Error, StringComparer.Ordinal);
        }
Exemple #5
0
        public void ProcessMessages_RegisterPlugin_SendsErrorWhenUnableToLocatePluginType()
        {
            var    assemblyLoadContext = CreateTestAssemblyLoadContext <TestPlugin>();
            var    serviceProvider     = new TestServiceProvider();
            object rawWrappedData      = null;
            var    pluginHandler       = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            var    pluginMessage       = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(TestPlugin).FullName + "_" },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };
            var expectedErrorMessage =
                $"Could not locate plugin id '{RandomGuidId}' of type '{typeof(TestPlugin).FullName + "_"}' " +
                "in assembly 'CustomAssembly'.";

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

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

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

            Assert.False(responseMessage.Success);
            Assert.Equal("RegisterPlugin", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.Equal(expectedErrorMessage, responseMessage.Error, StringComparer.Ordinal);
        }
Exemple #6
0
        public void ProcessMessages_RegisterPlugin_SendsPluginsProtocolWhenClientProtocolNotProvided()
        {
            var    assemblyLoadContext = CreateTestAssemblyLoadContext <TestPlugin>();
            var    serviceProvider     = new TestServiceProvider();
            object rawWrappedData      = null;
            var    pluginHandler       = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            var    pluginMessage       = new PluginMessage
            {
                Data = new JObject
                {
                    { "AssemblyName", "CustomAssembly" },
                    { "TypeName", typeof(TestPlugin).FullName },
                },
                MessageName = "RegisterPlugin",
                PluginId    = RandomGuidId
            };

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

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

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

            Assert.True(responseMessage.Success);
            Assert.Equal("RegisterPlugin", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.Equal(3, responseMessage.Protocol);
        }
Exemple #7
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);
        }
Exemple #8
0
        public void ProcessMessages_NoOpsWithoutMessagesEnqueued()
        {
            var    assemblyLoadContext  = CreateTestAssemblyLoadContext <TestPlugin>();
            var    serviceProvider      = new TestServiceProvider();
            object rawMessageBrokerData = null;
            var    pluginHandler        = new PluginHandler(serviceProvider, (data) => rawMessageBrokerData = data);

            pluginHandler.ProcessMessages(assemblyLoadContext);

            Assert.Null(rawMessageBrokerData);
        }
Exemple #9
0
        public void ProcessMessages_UnregisterPlugin_UnregistersPlugin()
        {
            var    assemblyLoadContext  = CreateTestAssemblyLoadContext <TestPlugin>();
            var    serviceProvider      = new TestServiceProvider();
            object rawWrappedData       = null;
            var    pluginHandler        = new PluginHandler(serviceProvider, (data) => rawWrappedData = data);
            var    expectedErrorMessage =
                $"Message received for unregistered plugin id '{RandomGuidId}'. Plugins must first be registered " +
                "before they can receive messages.";
            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);
            pluginHandler.ProcessMessages(assemblyLoadContext);

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

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

            Assert.True(responseMessage.Success);
            Assert.Equal("UnregisterPlugin", responseMessage.MessageName, StringComparer.Ordinal);
            Assert.Null(responseMessage.Error);
        }
Exemple #10
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);
        }
Exemple #11
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);
        }
Exemple #12
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);
        }
Exemple #13
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);
        }