Exemplo n.º 1
0
        /// <summary>
        /// Creates an returns a new silo.
        /// </summary>
        /// <param name="hostName">The silo name if it is not already specified in the configuration.</param>
        /// <param name="configuration">The configuration.</param>
        /// <returns>A new silo.</returns>
        public static ISiloHost CreateSiloHost(string hostName, IConfiguration configuration)
        {
            string siloName = configuration[nameof(TestSiloSpecificOptions.SiloName)] ?? hostName;

            var hostBuilder     = new HostBuilder();
            var siloBuilder     = new SiloBuilder(hostBuilder);
            var siloHostBuilder = new SiloHostBuilderAdaptor(hostBuilder, siloBuilder);

            // Add the silo builder to the host builder so that it is executed during configuration time.
            hostBuilder.Properties[nameof(SiloBuilder)] = siloBuilder;
            hostBuilder.ConfigureServices((context, services) =>
            {
                siloBuilder.Build(context, services);
            });

            siloBuilder
            .Configure <ClusterOptions>(configuration)
            .Configure <SiloOptions>(options => options.SiloName = siloName);

            hostBuilder.ConfigureHostConfiguration(cb => cb.AddConfiguration(configuration));

            hostBuilder.Properties["Configuration"] = configuration;
            ConfigureAppServices(configuration, hostBuilder, siloBuilder, siloHostBuilder);

            hostBuilder.ConfigureServices((context, services) =>
            {
                services.AddSingleton <TestHooksHostEnvironmentStatistics>();
                services.AddFromExisting <IHostEnvironmentStatistics, TestHooksHostEnvironmentStatistics>();
                services.AddSingleton <TestHooksSystemTarget>();
                ConfigureListeningPorts(context.Configuration, services);

                TryConfigureClusterMembership(context.Configuration, services);
                TryConfigureFileLogging(configuration, services, siloName);

                if (Debugger.IsAttached)
                {
                    // Test is running inside debugger - Make timeout ~= infinite
                    services.Configure <SiloMessagingOptions>(op => op.ResponseTimeout = TimeSpan.FromMilliseconds(1000000));
                }
            });

            var host = hostBuilder.Build();
            var silo = host.Services.GetRequiredService <ISiloHost>();

            InitializeTestHooksSystemTarget(silo);
            return(silo);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an returns a new silo.
        /// </summary>
        /// <param name="hostName">The silo name if it is not already specified in the configuration.</param>
        /// <param name="configurationSources">The configuration.</param>
        /// <returns>A new silo.</returns>
        public static ISiloHost CreateSiloHost(string hostName, IEnumerable <IConfigurationSource> configurationSources)
        {
            var configBuilder = new ConfigurationBuilder();

            foreach (var source in configurationSources)
            {
                configBuilder.Add(source);
            }
            var configuration = configBuilder.Build();

            string siloName = configuration[nameof(TestSiloSpecificOptions.SiloName)] ?? hostName;

            var hostBuilder     = new HostBuilder();
            var siloBuilder     = new SiloBuilder(hostBuilder);
            var siloHostBuilder = new SiloHostBuilderAdaptor(hostBuilder, siloBuilder);

            // Add the silo builder to the host builder so that it is executed during configuration time.
            hostBuilder.Properties[nameof(SiloBuilder)] = siloBuilder;
            hostBuilder.ConfigureServices((context, services) =>
            {
                siloBuilder.Build(context, services);
            });

            siloBuilder
            .Configure <ClusterOptions>(configuration)
            .Configure <SiloOptions>(options => options.SiloName = siloName);

            hostBuilder
            .ConfigureHostConfiguration(cb =>
            {
                // TODO: Instead of passing the sources individually, just chain the pre-built configuration once we upgrade to Microsoft.Extensions.Configuration 2.1
                foreach (var source in configBuilder.Sources)
                {
                    cb.Add(source);
                }
            });

            hostBuilder.Properties["Configuration"] = configuration;
            ConfigureAppServices(configuration, hostBuilder, siloBuilder, siloHostBuilder);

            hostBuilder.ConfigureServices((context, services) =>
            {
                services.AddSingleton <TestHooksHostEnvironmentStatistics>();
                services.AddFromExisting <IHostEnvironmentStatistics, TestHooksHostEnvironmentStatistics>();
                services.AddSingleton <TestHooksSystemTarget>();
                ConfigureListeningPorts(context.Configuration, services);

                TryConfigureClusterMembership(context.Configuration, services);
                TryConfigureFileLogging(configuration, services, siloName);

                if (Debugger.IsAttached)
                {
                    // Test is running inside debugger - Make timeout ~= infinite
                    services.Configure <SiloMessagingOptions>(op => op.ResponseTimeout = TimeSpan.FromMilliseconds(1000000));
                }
            });

            siloBuilder.GetApplicationPartManager().ConfigureDefaults();

            var host = hostBuilder.Build();
            var silo = host.Services.GetRequiredService <ISiloHost>();

            InitializeTestHooksSystemTarget(silo);
            return(silo);
        }