コード例 #1
0
        public async Task ExecuteAsyncRetries()
        {
            var appBuilder = new Mock <IApplicationBuilder>();

            appBuilder.Setup(x => x.CreateApplication()).Returns(new Application());

            Uri uri    = new Uri("http://example.com");
            var config = new SpringBootConfig()
            {
                SpringBootServerUrl = uri, RetryTimeout = TimeSpan.FromMilliseconds(5)
            };

            var acessor = new Mock <IOptionsMonitor <SpringBootConfig> >();

            acessor.Setup(x => x.CurrentValue).Returns(config);

            var logger      = new Mock <ILogger <SpringBootClient> >();
            var restAPiMock = new Mock <ISpringBootAdminRESTAPI>();

            restAPiMock.Setup(x => x.PostAsync(It.IsNotNull <Application>(), It.Is <Uri>(x => x == uri))).Throws(new DuplicateKeyException());

            var sbc = new SpringBootClient(logger.Object, appBuilder.Object, acessor.Object, restAPiMock.Object);

            await sbc.StartAsync(CancellationToken.None).ConfigureAwait(false);

            await Task.Delay(11).ConfigureAwait(false);

            restAPiMock.Verify(x => x.PostAsync(It.IsAny <Application>(), It.IsAny <Uri>()), Times.AtLeast(2));
            sbc.Dispose();
        }
コード例 #2
0
        public async Task ExecuteAsyncStopsWhenCancelled()
        {
            var appBuilder = new Mock <IApplicationBuilder>();

            appBuilder.Setup(x => x.CreateApplication()).Returns(new Application());

            Uri uri    = new Uri("http://example.com");
            var config = new SpringBootConfig()
            {
                SpringBootServerUrl = uri, RetryTimeout = TimeSpan.FromMilliseconds(5)
            };

            var acessor = new Mock <IOptionsMonitor <SpringBootConfig> >();

            acessor.Setup(x => x.CurrentValue).Returns(config);

            var logger      = new Mock <ILogger <SpringBootClient> >();
            var restAPiMock = new Mock <ISpringBootAdminRESTAPI>();

            restAPiMock.Setup(x => x.PostAsync(It.IsNotNull <Application>(), It.Is <Uri>(x => x == uri))).Throws(new DuplicateKeyException());

            var sbc = new SpringBootClient(logger.Object, appBuilder.Object, acessor.Object, restAPiMock.Object);

            // the token passed to startasync is not the one used by ExecuteAsync (https://github.com/dotnet/extensions/issues/1245)
            await sbc.StartAsync(CancellationToken.None).ConfigureAwait(false);

            await Task.Delay(11).ConfigureAwait(false);

            await sbc.StopAsync(CancellationToken.None).ConfigureAwait(false);

            sbc.Registering.Should().BeFalse();
            sbc.Dispose();
        }