public async Task IntervalFlushTriggerTwoConcurrentProcesses() { var time = 2000; _handler = GetFlushHandler(100, 20, 300, 2); _requestHandlerBehavior = MultipleTaskResponseBehavior(Task.Delay(time), Task.CompletedTask, Task.Delay(time)); _ = _handler.Process(new Track(null, null, null, null)); await Task.Delay(400); for (int i = 0; i < 3; i++) { await _handler.Process(new Track(null, null, null, null)); //There is only the first process _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(1)); } await Task.Delay(400); //The second process should be triggered _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(2)); _handler.Flush(); //Validating that flush doesn't triggered another process _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(2)); }
public void Init() { _requestHandlerBehavior = SingleTaskResponseBehavior(Task.CompletedTask); _mockRequestHandler = new Mock <IRequestHandler>(); _mockRequestHandler.Setup(r => r.MakeRequest(It.IsAny <Batch>())) .Returns(() => _requestHandlerBehavior()) .Verifiable(); _handler = GetFlushHandler(100, 20, 2000); Logger.Handlers += LoggingHandler; }
public async Task ProcessActionFlushWhenQueueIsFull() { var queueSize = 10; _handler = GetFlushHandler(queueSize, 20, 20000); await Task.Delay(50); for (int i = 0; i < queueSize + 1; i++) { _ = _handler.Process(new Track(null, null, null, null)); } _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(1)); }
public void ProcessDropsActionsThatAreBiggerThan32Kb() { _handler = GetFlushHandler(10, 20, 20000); var actions = GetActions(2, GetEventName(32 * 1024)); foreach (var action in actions) { _ = _handler.Process(action); } _handler.Flush(); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Never); }
public async Task FlushSplitEventsInBatches() { var queueSize = 100; _handler = GetFlushHandler(queueSize, 20, 20000); await Task.Delay(100); for (int i = 0; i < queueSize; i++) { _ = _handler.Process(new Track(null, null, null, null)); } _handler.Flush(); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(5)); }
public async Task IntervalFlushSendsBatchesThatAreSmallerThan512Kb() { _handler = GetFlushHandler(1000, 1000, 10000); await Task.Delay(100); var actions = GetActions(999, GetEventName(30)); foreach (var action in actions) { _ = _handler.Process(action); } _handler.Flush(); _mockRequestHandler.Verify(r => r.MakeRequest(It.Is <Batch>(b => b.batch.Sum(a => a.Size) < MaxBatchSize)), times: Times.Exactly(1)); }
public void IntervalFlushIsTriggeredPeriodically() { var interval = 600; _handler = GetFlushHandler(100, 20, interval); Thread.Sleep(100); int trials = 5; for (int i = 0; i < trials; i++) { _handler.Process(new Track(null, null, null, null)).GetAwaiter().GetResult(); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(i)); Thread.Sleep(interval); } _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(trials)); }
public async Task IntervalFlushIsTriggeredPeriodically() { var interval = 600; _handler = GetFlushHandler(100, 20, interval); await Task.Delay(100); int trials = 5; for (int i = 0; i < trials; i++) { await _handler.Process(new Track(null, null, null, null)); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(i)); await Task.Delay(interval); } _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(trials)); }
public async Task FlushWaitsForPreviousFlushesTriggeredByInterval() { var time = 1500; _handler = GetFlushHandler(100, 20, 500); _requestHandlerBehavior = MultipleTaskResponseBehavior(Task.Delay(time)); DateTime start = DateTime.Now; _ = _handler.Process(new Track(null, null, null, null)); await Task.Delay(500); _handler.Flush(); TimeSpan duration = DateTime.Now.Subtract(start); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(1)); //50 millisecons as error margin Assert.IsTrue(duration.CompareTo(TimeSpan.FromMilliseconds(time - 50)) >= 0); }
public async Task BatchMeetTheMaxNumberOfActions() { var flushAt = 20; var actionNumber = 45; _handler = GetFlushHandler(1000, flushAt, 10000); await Task.Delay(100); var actions = GetActions(actionNumber, GetEventName(30)); foreach (var action in actions) { _ = _handler.Process(action); } _handler.Flush(); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), Times.Exactly(3)); _mockRequestHandler.Verify(r => r.MakeRequest(It.Is <Batch>(b => b.batch.Count == flushAt)), Times.Exactly(2)); _mockRequestHandler.Verify(r => r.MakeRequest(It.Is <Batch>(b => b.batch.Count == 5)), Times.Exactly(1)); }
public void IntervalFlushLimitConcurrentProcesses() { var time = 2000; _handler = GetFlushHandler(100, 20, 300); _requestHandlerBehavior = MultipleTaskResponseBehavior(time, 0, time); _ = _handler.Process(new Track(null, null, null, null)); Thread.Sleep(400); for (int i = 0; i < 3; i++) { _handler.Process(new Track(null, null, null, null)).GetAwaiter().GetResult(); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(1)); Thread.Sleep(300); } _handler.Flush(); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(2)); }
public async Task IntervalFlushLimitConcurrentProcesses() { var time = 2000; _handler = GetFlushHandler(100, 20, 300); _requestHandlerBehavior = MultipleTaskResponseBehavior(Task.Delay(time), Task.CompletedTask, Task.Delay(time)); _ = _handler.Process(new Track(null, null, null, null)); await Task.Delay(400); for (int i = 0; i < 3; i++) { await _handler.Process(new Track(null, null, null, null)); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(1)); await Task.Delay(300); } _handler.Flush(); _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(2)); }
public void IntervalFlushTriggerTwoConcurrentProcesses() { var time = 2000; _handler = GetFlushHandler(100, 20, 300, 2); _requestHandlerBehavior = MultipleTaskResponseBehavior(time, 0, time); _ = _handler.Process(new Track(null, null, null, null)); Thread.Sleep(400); for (int i = 0; i < 3; i++) { _handler.Process(new Track(null, null, null, null)).GetAwaiter().GetResult(); //There is only the first process _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(1)); } Thread.Sleep(400); //The second process should be triggered _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(2)); _handler.Flush(); //Validating that flush doesn't triggered another process _mockRequestHandler.Verify(r => r.MakeRequest(It.IsAny <Batch>()), times: Times.Exactly(2)); }