public async Task ShouldConfigureAndRunEventBus() { // Make the delay very large so we only test one interval run var options = new EventBusOptions { MaxQueriesPerInterval = 5, RefreshIntervalMS = 10000, PollingIntervalMS = 10000 }; SetupEventBusStub(options); using (var hostedService = new QueryEventBusTimedHostedService(_mockLogger.Object, _mockEventBus.Object, _mockEventBusOptionsAccessor.Object)) { await hostedService.StartAsync(new CancellationToken()); await WaitForEvent(EventType.ExecutePendingQueriesAsync); Assert.Equal(3, _eventList.Count); Assert.Collection(_eventList, event1 => Assert.Equal(EventType.SetMaxQueryPerInterval, event1), event2 => Assert.Equal(EventType.ResetQueryExecutedCounter, event2), event3 => Assert.Equal(EventType.ExecutePendingQueriesAsync, event3) ); Assert.Equal(options.MaxQueriesPerInterval, _mockEventBusSetInterval); } }
public async Task ShouldRunAndRefreshEventBusPeriodically_MultipleIntervals() { int waitMs = 1000; var options = new EventBusOptions { MaxQueriesPerInterval = 5, RefreshIntervalMS = 100, // use a bigger interval for less noise PollingIntervalMS = 50 }; SetupEventBusStub(options); using (var hostedService = new QueryEventBusTimedHostedService(_mockLogger.Object, _mockEventBus.Object, _mockEventBusOptionsAccessor.Object)) { var stopwatch = new Stopwatch(); await hostedService.StartAsync(new CancellationToken()); await Task.Delay(waitMs); var approxNumExecuteQueryCalls = (waitMs / options.PollingIntervalMS); var approxNumRefreshCalls = (waitMs / options.RefreshIntervalMS); var actualNumExecuteCalls = _eventList.Count(e => e == EventType.ExecutePendingQueriesAsync); var actualNumRefreshCalls = _eventList.Count(e => e == EventType.ResetQueryExecutedCounter); const int allowedDeviation = 3; Assert.True(Math.Abs(actualNumExecuteCalls - approxNumExecuteQueryCalls) < allowedDeviation); Assert.True(Math.Abs(actualNumRefreshCalls - approxNumRefreshCalls) < allowedDeviation); } }
public async Task ShouldThrowException_WhenPollingIntervalMSIsEqualOrLessThanZero(int settingValue) { var options = new EventBusOptions { PollingIntervalMS = settingValue, // use a bigger interval for less noise }; SetupEventBusStub(options); using (var hostedService = new QueryEventBusTimedHostedService(_mockLogger.Object, _mockEventBus.Object, _mockEventBusOptionsAccessor.Object)) { await Assert.ThrowsAsync <ArgumentException>(() => hostedService.StartAsync(new CancellationToken())); } }