public async Task AssertRegisterSdkToServiceCollection()
        {
            var options         = new AgonesSdkOptions();
            var serviceProvider = new ServiceCollection()
                                  .AddSingleton <IAgonesSdk>(new MockAgonesSdk(options))
                                  .BuildServiceProvider();
            var mockSdk = serviceProvider.GetRequiredService <IAgonesSdk>();

            // Property
            Assert.True(mockSdk.HealthEnabled);
            Assert.True(mockSdk.Options.CacheRequest);
            Assert.Equal(TimeSpan.FromSeconds(2), mockSdk.Options.HealthInterval);
            Assert.Equal("Agones", mockSdk.Options.HttpClientName);
            Assert.Equal("AgonesSdkCsharp", mockSdk.Options.HttpClientUserAgent);

            // Method
            var cts = new CancellationTokenSource();

            Assert.Null(await Record.ExceptionAsync(() => mockSdk.Allocate(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mockSdk.Annotation("key", "value", cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mockSdk.GameServer(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mockSdk.Health(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mockSdk.Label("key", "value", cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mockSdk.Ready(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mockSdk.Reserve(1, cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mockSdk.Shutdown(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mockSdk.Watch(cts.Token)));
        }
        public void AssertOptionDefaultValue()
        {
            var options = new AgonesSdkOptions();

            // Property
            Assert.True(options.CacheRequest);
            Assert.Equal(TimeSpan.FromSeconds(2), options.HealthInterval);
            Assert.Equal("Agones", options.HttpClientName);
            Assert.Equal("AgonesSdkCsharp", options.HttpClientUserAgent);
        }
        /// <summary>
        /// Add Agones Sdk and run Health Check in the background.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hostBuilder"></param>
        /// <param name="configureSdk"></param>
        /// <param name="configureHosting"></param>
        /// <returns></returns>
        public static IHostBuilder UseAgones <T>(this IHostBuilder hostBuilder, Action <AgonesSdkOptions> configureSdk, Action <AgonesSdkHostingOptions> configureHosting) where T : class, IAgonesSdk
        {
            return(hostBuilder.ConfigureServices((hostContext, services) =>
            {
                var sdkOptions = new AgonesSdkOptions();
                configureSdk.Invoke(sdkOptions);

                var serviceOptions = new AgonesSdkHostingOptions();
                configureHosting.Invoke(serviceOptions);

                if (serviceOptions.UseDefaultHttpClientFactory)
                {
                    var sp = services.BuildServiceProvider();
                    var loggerFactory = sp.GetService <ILoggerFactory>();
                    var logger = loggerFactory.CreateLogger <AgonesHealthCheckService>();
                    var circuitLogger = loggerFactory.CreateLogger <AgonesCircuitDelegate>();

                    var httpClientBuilder = services.AddHttpClient(sdkOptions.HttpClientName, client =>
                    {
                        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                        client.DefaultRequestHeaders.Add("User-Agent", sdkOptions.HttpClientUserAgent);
                    })
                                            .AddTransientHttpErrorPolicy(x => x.CircuitBreakerAsync(
                                                                             serviceOptions.HandledEventsAllowedBeforeCirtcuitBreaking,
                                                                             serviceOptions.CirtcuitBreakingDuration,
                                                                             onBreak: (response, state, duration, context) => serviceOptions.OnBreak(response, state, duration, context, circuitLogger),
                                                                             onReset: (Context context) => serviceOptions.OnReset(context, circuitLogger),
                                                                             onHalfOpen: () => serviceOptions.OnHalfOpen(circuitLogger))
                                                                         );
                }

                services.AddSingleton <AgonesSdkOptions>(sdkOptions);
                services.AddSingleton <IAgonesSdk, T>();
                if (serviceOptions.RegisterHealthCheckService)
                {
                    services.AddHostedService <AgonesHealthCheckService>();
                }
            }));
        }
        public async Task AssertSdkComplete()
        {
            var options = new AgonesSdkOptions();
            var mock    = new MockAgonesSdk(options);
            var cts     = new CancellationTokenSource();

            Assert.Null(await Record.ExceptionAsync(() => mock.Allocate(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mock.Annotation("key", "value", cts.Token)));
            {
                GameServerResponse response = null;
                var exception = await Record.ExceptionAsync(async() => response = await mock.GameServer(cts.Token));

                Assert.Null(exception);
                Assert.NotNull(response.ObjectMeta);
                Assert.NotNull(response.Status);
            }
            Assert.Null(await Record.ExceptionAsync(() => mock.Health(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mock.Label("key", "value", cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mock.Ready(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mock.Reserve(1, cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mock.Shutdown(cts.Token)));
            Assert.Null(await Record.ExceptionAsync(() => mock.Watch(cts.Token)));
        }
Beispiel #5
0
 public CircuitMockAgonesSdk(AgonesSdkOptions options, IHttpClientFactory httpClientFactory) : base(options, httpClientFactory)
 {
 }
Beispiel #6
0
 public HogeSdk(AgonesSdkOptions options) => (Options, mockResponse) = (options, CreateMockResponse());