internal async Task MaxBatchEventCount_test(TestArgs args, int expectedNumberOfBatches) { var expectedNumberOfBatchesSentTcs = new TaskCompletionSource <object>(); var actualNumberOfBatches = 0; var handler = new MockHttpMessageHandler((r, c) => { if (Interlocked.Increment(ref actualNumberOfBatches) == expectedNumberOfBatches) { expectedNumberOfBatchesSentTcs.SetResult(null); } return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))); }); var configurationReader = args.BuildConfig(_logger); var service = Service.GetDefaultService(configurationReader, _logger); var payloadSender = new PayloadSenderV2(_logger, configurationReader, service, new Api.System(), handler , /* dbgName: */ TestDisplayName); using (var agent = new ApmAgent(new TestAgentComponents(_logger, payloadSender: payloadSender))) { var numberOfEventsEnqueuedSuccessfully = 0; for (var txIndex = 1;; ++txIndex) { if (EnqueueDummyEvent(payloadSender, agent, txIndex)) { ++numberOfEventsEnqueuedSuccessfully; } else { Thread.Yield(); } if (numberOfEventsEnqueuedSuccessfully == expectedNumberOfBatches * args.MaxBatchEventCount) { break; } } (await Task.WhenAny(expectedNumberOfBatchesSentTcs.Task, Task.Delay(30.Seconds()))) .Should() .Be(expectedNumberOfBatchesSentTcs.Task , $"because numberOfEventsEnqueuedSuccessfully: {numberOfEventsEnqueuedSuccessfully}"); } }
internal void MaxQueueEventCount_should_be_enforced_before_send(TestArgs args) { var sendTcs = new TaskCompletionSource <object>(); var handler = new MockHttpMessageHandler(async(r, c) => { await sendTcs.Task; return(new HttpResponseMessage(HttpStatusCode.OK)); }); var configurationReader = args.BuildConfig(_logger); var service = Service.GetDefaultService(configurationReader, _logger); var payloadSender = new PayloadSenderV2(_logger, configurationReader, service, new Api.System(), handler, /* dbgName: */ TestDisplayName); using (var agent = new ApmAgent(new TestAgentComponents(_logger, payloadSender: payloadSender))) { int?txIndexResumedEnqueuing = null; for (var txIndex = 1; txIndex <= args.MaxQueueEventCount + args.MaxBatchEventCount + 10; ++txIndex) { var enqueuedSuccessfully = EnqueueDummyEvent(payloadSender, agent, txIndex); if (txIndex <= args.MaxQueueEventCount) { enqueuedSuccessfully.Should().BeTrue($"txIndex: {txIndex}, args: {args}"); continue; } // It's possible that the events for the first batch have already been dequeued // so we can be sure that queue doesn't have any free space left only after MaxQueueEventCount + MaxBatchEventCount events if (enqueuedSuccessfully && !txIndexResumedEnqueuing.HasValue) { txIndexResumedEnqueuing = txIndex; } enqueuedSuccessfully.Should() .Be(txIndex - txIndexResumedEnqueuing < args.MaxBatchEventCount , $"txIndex: {txIndex}, txIndexResumedEnqueuing: {txIndexResumedEnqueuing}, args: {args}"); } sendTcs.SetResult(null); } }
internal async Task MaxQueueEventCount_should_be_enforced_after_send(TestArgs args) { // LoggerBase.Level = LogLevel.Debug; var sendTcs = new TaskCompletionSource <object>(); var firstBatchDequeuedTcs = new TaskCompletionSource <object>(); var handler = new MockHttpMessageHandler(async(r, c) => { firstBatchDequeuedTcs.SetResult(null); await sendTcs.Task; return(new HttpResponseMessage(HttpStatusCode.OK)); }); var configurationReader = args.BuildConfig(_logger); var service = Service.GetDefaultService(configurationReader, _logger); var payloadSender = new PayloadSenderV2(_logger, configurationReader, service, new Api.System(), handler, /* dbgName: */ TestDisplayName); using (var agent = new ApmAgent(new TestAgentComponents(_logger, payloadSender: payloadSender))) { var txIndex = 1; for (; txIndex <= args.MaxQueueEventCount; ++txIndex) { EnqueueDummyEvent(payloadSender, agent, txIndex).Should().BeTrue($"txIndex: {txIndex}, args: {args}"); } await firstBatchDequeuedTcs.Task; for (; txIndex <= args.MaxQueueEventCount + args.MaxBatchEventCount + 10; ++txIndex) { EnqueueDummyEvent(payloadSender, agent, txIndex) .Should() .Be(txIndex <= args.MaxQueueEventCount + args.MaxBatchEventCount , $"txIndex: {txIndex}, args: {args}"); } sendTcs.SetResult(null); } }
internal void FlushInterval_test(TestArgs args, int numberOfEventsToSend) { var batchSentBarrier = new Barrier(2); var barrierTimeout = 30.Seconds(); var handler = new MockHttpMessageHandler((r, c) => { batchSentBarrier.SignalAndWait(barrierTimeout).Should().BeTrue(); return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))); }); var configurationReader = args.BuildConfig(_logger); var service = Service.GetDefaultService(configurationReader, _logger); var payloadSender = new PayloadSenderV2(_logger, configurationReader, service, new Api.System(), handler, /* dbgName: */ TestDisplayName); using (var agent = new ApmAgent(new TestAgentComponents(_logger, payloadSender: payloadSender))) { for (var eventIndex = 1; eventIndex <= numberOfEventsToSend; ++eventIndex) { EnqueueDummyEvent(payloadSender, agent, eventIndex).Should().BeTrue($"eventIndex: {eventIndex}, args: {args}"); batchSentBarrier.SignalAndWait(barrierTimeout).Should().BeTrue($"eventIndex: {eventIndex}, args: {args}"); } } }