public async Task SetBackendsAsync_DeepClones()
        {
            // Arrange
            var repo     = new InMemoryBackendsRepo();
            var backends = new Dictionary <string, Backend>
            {
                { "backend1", new Backend {
                      CircuitBreakerOptions = new CircuitBreakerOptions()
                      {
                          MaxConcurrentRequests = 10
                      }
                  } }
            };

            // Act
            await repo.SetBackendsAsync(backends, CancellationToken.None);

            // Modify input, should not affect output
            backends["backend1"].CircuitBreakerOptions.MaxConcurrentRequests = -1;
            backends.Add("backend2", new Backend {
                CircuitBreakerOptions = new CircuitBreakerOptions()
                {
                    MaxConcurrentRequests = 30
                }
            });

            var result = await repo.GetBackendsAsync(CancellationToken.None);

            // Assert
            Assert.Single(result);
            Assert.NotSame(backends, result);
            Assert.NotSame(backends["backend1"].CircuitBreakerOptions, result["backend1"].CircuitBreakerOptions);
            Assert.Equal(10, result["backend1"].CircuitBreakerOptions.MaxConcurrentRequests);
        }
        public void SetBackendsAsync_CompletesSynchronously()
        {
            // Arrange
            var repo = new InMemoryBackendsRepo();

            // Act
            var task = repo.SetBackendsAsync(new Dictionary <string, Backend>(), CancellationToken.None);

            // Assert
            Assert.True(task.IsCompleted, "should complete synchronously");
        }
        public async Task SetBackendsAsync_IgnoresCancellation()
        {
            // Arrange
            var repo = new InMemoryBackendsRepo();

            using (var cts = new CancellationTokenSource())
            {
                cts.Cancel();

                // Act & Assert
                await repo.SetBackendsAsync(new Dictionary <string, Backend>(), cts.Token);
            }
        }
Esempio n. 4
0
        public async Task GetBackendsAsync_DeepClones()
        {
            // Arrange
            var repo     = new InMemoryBackendsRepo();
            var backends = new Dictionary <string, Backend>
            {
                { "backend1", new Backend {
                      CircuitBreakerOptions = new CircuitBreakerOptions()
                      {
                          MaxConcurrentRequests = 10
                      }
                  } }
            };

            // Act
            await repo.SetBackendsAsync(backends, CancellationToken.None);

            var result1 = await repo.GetBackendsAsync(CancellationToken.None);

            // Modify first results, should not affect future results
            result1["backend1"].CircuitBreakerOptions.MaxConcurrentRequests = -1;
            result1.Add("backend2", new Backend {
                CircuitBreakerOptions = new CircuitBreakerOptions()
                {
                    MaxConcurrentRequests = 30
                }
            });

            var result2 = await repo.GetBackendsAsync(CancellationToken.None);

            // Assert
            result2.Should().HaveCount(1);
            result2.Should().NotBeSameAs(result1);
            result2.Should().NotBeSameAs(backends);
            result2["backend1"].CircuitBreakerOptions.MaxConcurrentRequests.Should().Be(10);
        }