Exemple #1
0
        public void Test_ApplicationHost_RunOnceMultipleTimes()
        {
            // Run first time.
            // Arrange
            var builder = new AppHostBuilder()
                          .ConfigureServices((config, logger, services) => services.AddSingleton <SampleDependency>()
                                             .AddHostedProcess <SimpleService>());

            // Act
            var processHost = builder.Build();
            var appHost     = new AppHost(((AppHost)processHost)._webHost,
                                          ((AppHost)processHost)._retryPolicies,
                                          ((AppHost)processHost)._serviceProvider,
                                          new List <Type>(), -1, true);

            appHost.RunOnce();

            // Assert
            appHost.Status.Should().Be(HostStatus.Stopped);

            // Arrage - Run second time - reach code branches that check for already created params.
            appHost = new AppHost(((AppHost)processHost)._webHost,
                                  ((AppHost)processHost)._retryPolicies,
                                  new ServiceCollection().BuildServiceProvider(),
                                  new List <Type>(), -1, true);

            // Act
            appHost.RunOnce();

            // Assert
            appHost.Status.Should().Be(HostStatus.Stopped);
        }
Exemple #2
0
        public void NullAssemblyForEmbeddedFontThrows()
        {
            var builder = new AppHostBuilder()
                          .ConfigureFonts((_, fonts) => fonts.AddEmeddedResourceFont(null, "test.ttf"));

            var ex = Assert.Throws <ArgumentNullException>(() => builder.Build());

            Assert.Equal("assembly", ex.ParamName);
        }
Exemple #3
0
        public void Test_ApplicationHost_MultipleBuild()
        {
            // Arrange - setup host.
            var builder = new AppHostBuilder(true).CreateDefaultBuilder()
                          .ConfigureAppConfiguration(configBuilder =>
            {
                configBuilder.AddInMemoryCollection(new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("a", "b")
                });
            });

            // Act - first build works fine.
            builder.Build();
            builder.Config.GetValue <string>("a").Should().Be("b");

            // Assert - Should fail the second build method call.
            Assert.Throws <InvalidOperationException>(() => builder.Build());
        }
Exemple #4
0
        static void Main(string[] args)
        {
            var builder = new AppHostBuilder()
                          .WithRoute("sync(controller=SyncToMedium,action=Sync)")
                          .WithRoute("{controller=help} {action=help}")
                          .WithHelp("This tool updates feedly feeds with medium subscriptions.");
            var host = builder.Build();

            host.Run(args);
        }
        static int Main(string[] args)
        {
            var builder = new AppHostBuilder()
                          .WithRoute("doit(controller=do,action=it)")
                          .WithRoute("hi(controller=action,action=hi) -n{name}")
                          .WithRoute("{controller=help} {action=help}");
            var host = builder.Build();

            return(host.Run(args));
        }
Exemple #6
0
        public void Test_ApplicationHost_HealthProbe()
        {
            var builder = new AppHostBuilder(true)
                          .ConfigureAppConfiguration(config =>
            {
                config.AddInMemoryCollection(new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("Key1", "Val1")
                });

                // Do "global" initialization here; Only called once.
                var currentDir = Directory.GetCurrentDirectory();

                // Method 1 - app settings.
                File.WriteAllText(Path.Combine(currentDir, "appsettings.json"), "{ \"TestKey1\":\"testVal1\", \"TestKey2\": { \"TestKey3\":\"testVal3\" } }");

                config.AddJsonFile("appsettings.json");
            })
                          .ConfigureServices((config, logger, services) =>
            {
                var allSettings = config.GetAllSettings().ToList();
                allSettings.Count(i => i.Key == "Key1").Should().Be(1);
                allSettings.Count(i => i.Key == "Key2").Should().Be(0);

                var allSettingsSkipped = config.GetAllSettings(new[] { typeof(MemoryConfigurationProvider) }).ToList();
                allSettingsSkipped.Count(i => i.Key == "Key1").Should().Be(0);

                var configString = config.GetAllSettingsAsString();
                configString.Length.Should().BeGreaterThan(0);

                var configSkipped = config.GetAllSettingsAsString(new[] { typeof(MemoryConfigurationProvider) });
                Assert.True(configSkipped.Length != configString.Length);
                services.AddSingleton <SampleDependency>();
            })
                          .ConfigureLogging((config, logging) => { })
                          .UseDefaultRetryPolicies()
                          .AddRetryWaitPolicy <InvalidOperationException>(5, 5)
                          .AddRetryWaitPolicy <TimeoutException>()
                          .AddHttpClient("test", "http://localhost:882/")
                          .AddHealthProbe("882")
                          .AddHostedProcess <SimpleService>()
                          .AddHostedProcess <BackgroundTimerService>();

            var processHost = builder.Build();

            var httpClient = ((AppHost)processHost)._serviceProvider.GetService <IHttpClientFactory>();

            var res = httpClient.CreateClient("test").GetAsync("probe").GetAwaiter().GetResult();

            res.StatusCode.Should().Be(200);
        }
Exemple #7
0
        public void BadFileNameForEmbeddedFontThrows(string filename)
        {
            var builder = new AppHostBuilder()
                          .ConfigureFonts((_, fonts) => fonts.AddEmeddedResourceFont(GetType().Assembly, filename));

            var ex = Assert.ThrowsAny <ArgumentException>(() => builder.Build());

            Assert.Equal("filename", ex.ParamName);

            if (filename == null)
            {
                Assert.IsType <ArgumentNullException>(ex);
            }
        }
Exemple #8
0
        public void Test_AppHost_IHostedService()
        {
            var hostedProcess = new SampleProcess()
            {
                Name = "Process1"
            };
            var hostedProcess2 = new SampleProcess()
            {
                Name = "Process2"
            };
            var hostedService = new SampleService()
            {
                Name = "Service1"
            };
            var hostedService2 = new SampleService()
            {
                Name = "Service2"
            };

            var hostBuilder = new AppHostBuilder().CreateDefaultBuilder();

            hostBuilder.AddHostedProcess(hostedProcess);
            hostBuilder.AddHostedProcess(hostedProcess2);
            hostBuilder.AddHostedProcess <SampleProcess>();
            hostBuilder.AddHostedService(hostedService);
            hostBuilder.AddHostedService(hostedService2);
            hostBuilder.AddHostedService <SampleService>();
            var host = hostBuilder.Build();

            host.RunOnce();

            //hostedProcess.StartCalls.Should().Be(1);
            //hostedProcess.StopCalls.Should().Be(1);
            //hostedProcess.ErrorCalls.Should().Be(1);
            //hostedService.StartCalls.Should().Be(1);
            //hostedService.StopCalls.Should().Be(1);
        }
Exemple #9
0
        public void Test_ApplicationHost_ServiceEndPoint()
        {
            // Arrange.
            var builder = new AppHostBuilder()
                          .ConfigureServices((config, logger, services) => { services.AddSingleton <SampleDependency>(); })
                          .AddHttpClient("test", "http://localhost:889/")
                          .AddHealthProbe("889")
                          .AddHostedProcess <SimpleService>()
                          .AddHostedProcess <BackgroundTimerService>()
                          .AddHostedProcess <SimpleService2>()
                          .AddHostedProcess <SimpleService2>()
                          .UseHostedProcessEndpoints();

            builder._processTypes.Add(typeof(SimpleService3));

            // Act
            var processHost = builder.Build();
            var httpClient  = ((AppHost)processHost)._serviceProvider.GetService <IHttpClientFactory>()
                              .CreateClient("test");

            // Assert
            httpClient.GetAsync("probe").GetAwaiter().GetResult().StatusCode.Should().Be(200);
            httpClient.GetAsync(typeof(SimpleService).Name).GetAwaiter().GetResult().StatusCode.Should().Be(200);
            httpClient.GetAsync(typeof(BackgroundTimerService).Name).GetAwaiter().GetResult().StatusCode.Should().Be(200);
            httpClient.GetAsync(typeof(SimpleService2).Name).GetAwaiter().GetResult().StatusCode.Should().Be(200);
            httpClient.GetAsync(typeof(SimpleService3).Name).GetAwaiter().GetResult().StatusCode.Should().Be(404);
            httpClient.GetAsync("swagger").GetAwaiter().GetResult().StatusCode.Should().Be(200);

            StopHost((AppHost)processHost).ConfigureAwait(false);

            processHost.RunAndBlock();

            builder = new AppHostBuilder().CreateDefaultBuilder()
                      .ConfigureServices((config, logger, services) => { services.AddSingleton <SampleDependency>(); })
                      .AddHostedProcess <SimpleService>().UseHostedProcessEndpoints();
            builder.Build().RunOnce();
        }