예제 #1
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);
        }