public void UpdateMaxConcurrent_ReplacesBulkhead() { // Arrange var key = AnyGroupKey; const int initialExpectedCount = 5; const int newExpectedCount = 6; var mockMetricEvents = new Mock <IMetricEvents>(MockBehavior.Strict); mockMetricEvents.Setup(m => m.BulkheadGauge(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())); var mockConfig = new MjolnirConfiguration { BulkheadConfigurations = new Dictionary <string, BulkheadConfiguration> { { key.Name, new BulkheadConfiguration { MaxConcurrent = initialExpectedCount } } } }; var mockLogFactory = new Mock <IMjolnirLogFactory>(MockBehavior.Strict); mockLogFactory.Setup(m => m.CreateLog <SemaphoreBulkheadHolder>()).Returns(new DefaultMjolnirLog <SemaphoreBulkheadHolder>()); var holder = new SemaphoreBulkheadHolder(key, mockMetricEvents.Object, mockConfig, mockLogFactory.Object); // Act var firstBulkhead = holder.Bulkhead; holder.UpdateMaxConcurrent(newExpectedCount); var secondBulkhead = holder.Bulkhead; // Assert // Shouldn't change any existing referenced bulkheads... Assert.Equal(initialExpectedCount, firstBulkhead.CountAvailable); // ...but newly-retrieved bulkheads should get a new instance // with the updated count. Assert.Equal(newExpectedCount, secondBulkhead.CountAvailable); // And they shouldn't be the same bulkhead (which should be obvious by this point). Assert.False(firstBulkhead == secondBulkhead); }
public void UpdateMaxConcurrent_IgnoresInvalidValues() { // Arrange var key = AnyGroupKey; const int invalidMaxConcurrent = -1; var mockMetricEvents = new Mock <IMetricEvents>(MockBehavior.Strict); mockMetricEvents.Setup(m => m.BulkheadGauge(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())); var mockConfig = new MjolnirConfiguration { BulkheadConfigurations = new Dictionary <string, BulkheadConfiguration> { { key.Name, new BulkheadConfiguration { MaxConcurrent = AnyPositiveInt } } } }; var mockLog = new Mock <IMjolnirLog <SemaphoreBulkheadHolder> >(MockBehavior.Strict); mockLog.Setup(m => m.Error(It.IsAny <string>())); var mockLogFactory = new Mock <IMjolnirLogFactory>(MockBehavior.Strict); mockLogFactory.Setup(m => m.CreateLog <SemaphoreBulkheadHolder>()).Returns(mockLog.Object); var holder = new SemaphoreBulkheadHolder(key, mockMetricEvents.Object, mockConfig, mockLogFactory.Object); // Act var initialBulkhead = holder.Bulkhead; holder.UpdateMaxConcurrent(invalidMaxConcurrent); // Assert // Bulkhead should be unchanged. Assert.True(initialBulkhead == holder.Bulkhead); mockLog.Verify(m => m.Error($"Semaphore bulkhead config for key {key.Name} changed to an invalid limit of {invalidMaxConcurrent}, the bulkhead will not be changed"), Times.Once); }
public void Construct_SetsInitialBulkhead() { // Arrange var key = AnyGroupKey; var expectedMaxConcurrent = AnyPositiveInt; var mockMetricEvents = new Mock <IMetricEvents>(MockBehavior.Strict); mockMetricEvents.Setup(m => m.BulkheadGauge(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())); var mockConfig = new MjolnirConfiguration { BulkheadConfigurations = new Dictionary <string, BulkheadConfiguration> { { key.Name, new BulkheadConfiguration { MaxConcurrent = expectedMaxConcurrent } } } }; var mockLogFactory = new Mock <IMjolnirLogFactory>(MockBehavior.Strict); mockLogFactory.Setup(m => m.CreateLog <SemaphoreBulkheadHolder>()).Returns(new DefaultMjolnirLog <SemaphoreBulkheadHolder>()); // Act var holder = new SemaphoreBulkheadHolder(key, mockMetricEvents.Object, mockConfig, mockLogFactory.Object); // Assert Assert.Equal(key.Name, holder.Bulkhead.Name); Assert.Equal(expectedMaxConcurrent, holder.Bulkhead.CountAvailable); }
public ChangeBulkheadLimitSyncCommand(string bulkheadKey, SemaphoreBulkheadHolder holder, int changeLimitTo) : base(bulkheadKey, bulkheadKey, TimeSpan.FromSeconds(1000)) { _holder = holder; _changeLimitTo = changeLimitTo; }