public void StatsEngineQueue_Is_Busy() { var dataTransportService = Mock.Create <IDataTransportService>(); var metricBuilder = WireModels.Utilities.GetSimpleMetricBuilder(); var outOfBandMetricSource = Mock.Create <IOutOfBandMetricSource>(); var agentHealthReporter = Mock.Create <IAgentHealthReporter>(); var dnsStatic = Mock.Create <IDnsStatic>(); var processStatic = Mock.Create <IProcessStatic>(); var scheduler = Mock.Create <IScheduler>(); Mock.Arrange(() => scheduler.ExecuteEvery(Arg.IsAny <Action>(), Arg.IsAny <TimeSpan>(), Arg.IsAny <TimeSpan?>())) .DoInstead <Action, TimeSpan, TimeSpan?>((action, _, __) => _harvestAction = action); _metricAggregator = new MetricAggregator(dataTransportService, metricBuilder, _metricNameService, new[] { outOfBandMetricSource }, processStatic, scheduler); EventBus <AgentConnectedEvent> .Publish(new AgentConnectedEvent()); var sentMetrics = Enumerable.Empty <MetricWireModel>(); Mock.Arrange(() => dataTransportService.Send(Arg.IsAny <IEnumerable <MetricWireModel> >())) .DoInstead <IEnumerable <MetricWireModel> >(metrics => sentMetrics = metrics); var maxThreads = 25; List <Thread> threads = new List <Thread>(); for (int i = 0; i < maxThreads; i++) { var test = new TestMetricWireModel(); test.CreateMetric(metricBuilder); Thread thread = new Thread(() => { _metricAggregator.Collect(test); }); thread.IsBackground = true; threads.Add(thread); thread.Start(); } for (int i = 0; i < maxThreads; i++) { threads[i].Join(); } var queueCountBeforeHarvest = _metricAggregator.StatsEngineQueue.StatsEngineCount; //Check if the queue got queued up when multiple threads use the queue. if (queueCountBeforeHarvest == 1) { Assert.Inconclusive(); } Assert.IsTrue(queueCountBeforeHarvest > 1); _harvestAction(); var queueCountAfterHarvest = _metricAggregator.StatsEngineQueue.StatsEngineCount; //Check if the queue is empty after harvest. Assert.IsTrue(queueCountAfterHarvest == 0); //Check the number of metrics being sent up. Assert.IsTrue(sentMetrics.Count() == 3, "Count was " + sentMetrics.Count()); // there should be one supportability and two DotNet (one scoped and one unscoped) string[] names = new string[] { "Supportability/MetricHarvest/transmit", "DotNet/test_metric" }; foreach (MetricWireModel current in sentMetrics) { Assert.IsTrue(names.Contains(current.MetricName.Name), "Name is not present: " + current.MetricName.Name); } }