Exemplo n.º 1
0
        public static void CreateConsumerRegistration(string solutionDirectory, Consumer consumer, string projectBaseName)
        {
            var className = $@"{consumer.EndpointRegistrationMethodName}Registration";
            var classPath = ClassPathHelper.WebApiConsumersServiceExtensionsClassPath(solutionDirectory, $"{className}.cs", projectBaseName);
            var consumerFeatureClassPath = ClassPathHelper.ConsumerFeaturesClassPath(solutionDirectory, $"{consumer.ConsumerName}.cs", consumer.DomainDirectory, projectBaseName);

            if (!Directory.Exists(classPath.ClassDirectory))
            {
                Directory.CreateDirectory(classPath.ClassDirectory);
            }

            if (File.Exists(classPath.FullClassPath))
            {
                throw new FileAlreadyExistsException(classPath.FullClassPath);
            }

            var quorumText = consumer.IsQuorum ? $@"

            // a replicated queue to provide high availability and data safety. available in RMQ 3.8+
            re.SetQuorumQueue();" : "";

            var lazyText = consumer.IsLazy ? $@"

            // enables a lazy queue for more stable cluster with better predictive performance.
            // Please note that you should disable lazy queues if you require really high performance, if the queues are always short, or if you have set a max-length policy.
            re.SetQueueArgument(""declare"", ""lazy"");" : "";

            //re.Lazy = true;" : "";

            using FileStream fs = File.Create(classPath.FullClassPath);
            var data = "";

            if (ExchangeTypeEnum.FromName(consumer.ExchangeType) == ExchangeTypeEnum.Direct ||
                ExchangeTypeEnum.FromName(consumer.ExchangeType) == ExchangeTypeEnum.Topic)
            {
                data = GetDirectOrTopicConsumerRegistration(classPath.ClassNamespace, className, consumer, lazyText, quorumText, consumerFeatureClassPath.ClassNamespace);
            }
            else
            {
                data = GetFanoutConsumerRegistration(classPath.ClassNamespace, className, consumer, lazyText, quorumText, consumerFeatureClassPath.ClassNamespace);
            }

            fs.Write(Encoding.UTF8.GetBytes(data));
        }
Exemplo n.º 2
0
        private static string WriteTestFileText(string solutionDirectory, ClassPath classPath, Consumer consumer, string projectBaseName)
        {
            var testFixtureName   = Utilities.GetIntegrationTestFixtureName();
            var testUtilClassPath = ClassPathHelper.IntegrationTestUtilitiesClassPath(solutionDirectory, projectBaseName, "");
            var consumerClassPath = ClassPathHelper.ConsumerFeaturesClassPath(solutionDirectory, "", projectBaseName);

            return(@$ "namespace {classPath.ClassNamespace};

using FluentAssertions;
using NUnit.Framework;
using System.Threading.Tasks;
using MassTransit;
using MassTransit.Testing;
using Messages;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using {consumerClassPath.ClassNamespace};
using {testUtilClassPath.ClassNamespace};
using static {testFixtureName};
Exemplo n.º 3
0
        public static void CreateConsumerFeature(string srcDirectory, Consumer consumer, string projectBaseName)
        {
            var classPath = ClassPathHelper.ConsumerFeaturesClassPath(srcDirectory, $"{consumer.ConsumerName}.cs", projectBaseName);

            if (!Directory.Exists(classPath.ClassDirectory))
            {
                Directory.CreateDirectory(classPath.ClassDirectory);
            }

            if (File.Exists(classPath.FullClassPath))
            {
                throw new FileAlreadyExistsException(classPath.FullClassPath);
            }

            using FileStream fs = File.Create(classPath.FullClassPath);
            var data = GetDirectOrTopicConsumerRegistration(classPath.ClassNamespace, consumer, srcDirectory, projectBaseName);

            fs.Write(Encoding.UTF8.GetBytes(data));
        }
Exemplo n.º 4
0
        public static void AddMTConsumer(string testDirectory, string consumerName, string projectBaseName, string srcDirectory)
        {
            var classPath = ClassPathHelper.IntegrationTestProjectRootClassPath(testDirectory, "TestFixture.cs", projectBaseName);

            if (!Directory.Exists(classPath.ClassDirectory))
            {
                throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found.");
            }

            if (!File.Exists(classPath.FullClassPath))
            {
                throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found.");
            }

            var consumerClassPath = ClassPathHelper.ConsumerFeaturesClassPath(srcDirectory, $"", projectBaseName);

            var tempPath = $"{classPath.FullClassPath}temp";
            var hasUsingForConsumerNamespace = false;

            using (var input = File.OpenText(classPath.FullClassPath))
            {
                using var output = new StreamWriter(tempPath);
                string line;
                while (null != (line = input.ReadLine()))
                {
                    var newText = $"{line}";
                    if (line.Contains($"// Consumer Registration -- Do Not Delete Comment"))
                    {
                        newText += $@"

            cfg.AddConsumer<{consumerName}>();
            cfg.AddConsumerTestHarness<{consumerName}>();";
                    }
                    if (line.Contains(consumerClassPath.ClassNamespace))
                    {
                        hasUsingForConsumerNamespace = true;
                    }

                    output.WriteLine(newText);
                }
            }

            // delete the old file and set the name of the new one to the original name
            File.Delete(classPath.FullClassPath);
            File.Move(tempPath, classPath.FullClassPath);

            if (!hasUsingForConsumerNamespace)
            {
                using (var input = File.OpenText(classPath.FullClassPath))
                {
                    using var output = new StreamWriter(tempPath);
                    string line;
                    while (null != (line = input.ReadLine()))
                    {
                        var newText = $"{line}";
                        if (line.Contains($"using MassTransit;"))
                        {
                            newText += @$ "{Environment.NewLine}using {consumerClassPath.ClassNamespace};";
                        }

                        output.WriteLine(newText);
                    }
                }

                // delete the old file and set the name of the new one to the original name
                File.Delete(classPath.FullClassPath);
                File.Move(tempPath, classPath.FullClassPath);
            }
        }
Exemplo n.º 5
0
        public static void AddMassTransit(string testDirectory, string projectBaseName)
        {
            var classPath = ClassPathHelper.IntegrationTestProjectRootClassPath(testDirectory, "TestFixture.cs", projectBaseName);

            if (!Directory.Exists(classPath.ClassDirectory))
            {
                throw new DirectoryNotFoundException($"The `{classPath.ClassDirectory}` directory could not be found.");
            }

            if (!File.Exists(classPath.FullClassPath))
            {
                throw new FileNotFoundException($"The `{classPath.FullClassPath}` file could not be found.");
            }

            var consumerFeatureClassPath = ClassPathHelper.ConsumerFeaturesClassPath(testDirectory, $"", projectBaseName);

            var usingsAdded = false;
            var tempPath    = $"{classPath.FullClassPath}temp";

            using (var input = File.OpenText(classPath.FullClassPath))
            {
                using var output = new StreamWriter(tempPath);
                string line;
                while (null != (line = input.ReadLine()))
                {
                    var newText = $"{line}";
                    if (line.Contains($"// MassTransit Setup -- Do Not Delete Comment"))
                    {
                        newText += $@"
        _provider = services.AddMassTransitInMemoryTestHarness(cfg =>
        {{
            // Consumer Registration -- Do Not Delete Comment
        }}).BuildServiceProvider();
        _harness = _provider.GetRequiredService<InMemoryTestHarness>();

        services.AddScoped(_ => Mock.Of<IPublishEndpoint>());
        await _harness.Start();";
                    }
                    else if (line.Contains($"using") && !usingsAdded)
                    {
                        newText    += $@"{Environment.NewLine}using MassTransit.Testing;
using MassTransit;";
                        usingsAdded = true;
                    }
                    else if (line.Contains($"// MassTransit Teardown -- Do Not Delete Comment"))
                    {
                        newText += $@"
        await _harness.Stop();";
                    }
                    else if (line.Contains($"// MassTransit Methods -- Do Not Delete Comment"))
                    {
                        newText += $@"
    public static async Task PublishMessage<T>(object message)
        where T : class
    {{
        await _harness.Bus.Publish<T>(message);
    }}";
                    }
                    else if (line.Contains($"private static Checkpoint _checkpoint;"))
                    {
                        newText += $@"
    public static InMemoryTestHarness _harness;
    public static ServiceProvider _provider;";
                    }

                    output.WriteLine(newText);
                }
            }

            // delete the old file and set the name of the new one to the original name
            File.Delete(classPath.FullClassPath);
            File.Move(tempPath, classPath.FullClassPath);
        }