示例#1
0
        public void LimitedAmountOfSpansShouldBeSent_WhenSpansAreCapturedConcurrently()
        {
            // Arrange
            const int spansCount        = 10;
            const int maxSpansCount     = 2;
            var       mockPayloadSender = new MockPayloadSender();
            var       mockConfig        = new MockConfigSnapshot(transactionMaxSpans: maxSpansCount.ToString());

            // Act
            using (var agent = new ApmAgent(new TestAgentComponents(config: mockConfig, payloadSender: mockPayloadSender)))
            {
                agent.Tracer.CaptureTransaction("test transaction name", "test transaction type",
                                                transaction =>
                {
                    MultiThreadsTestUtils.TestOnThreads(spansCount, threadIndex =>
                    {
                        transaction.CaptureSpan($"test span name #{threadIndex}", "test span type", span => { });
                        return(1);
                    });
                });
            }

            // Assert
            mockPayloadSender.WaitForTransactions();
            mockPayloadSender.Transactions.Count.Should().Be(1);
            mockPayloadSender.WaitForSpans();
            mockPayloadSender.Spans.Count.Should().Be(maxSpansCount);
            mockPayloadSender.FirstTransaction.SpanCount.Dropped.Should().Be(spansCount - maxSpansCount);
        }
        internal void with_result_multiple_threads(string dbgWayToCallDesc, Func <LazyContextualInit <string>, Func <string>, string> wayToCall)
        {
            var counter     = new ThreadSafeIntCounter();
            var lazyCtxInit = new LazyContextualInit <string>();

            var threadResults = MultiThreadsTestUtils.TestOnThreads(threadIndex => wayToCall(lazyCtxInit, () => counter.Increment().ToString()));

            lazyCtxInit.IsInited.Should().BeTrue($"{nameof(dbgWayToCallDesc)}: {dbgWayToCallDesc}");
            counter.Value.Should().Be(1);

            threadResults.ForEach(x => x.Should().Be("1"));
        }
        internal void no_result_multiple_threads(string dbgWayToCallDesc, Func <LazyContextualInit, Action, bool> wayToCall)
        {
            var counter     = new ThreadSafeIntCounter();
            var lazyCtxInit = new LazyContextualInit();

            var threadResults = MultiThreadsTestUtils.TestOnThreads(threadIndex => wayToCall(lazyCtxInit, () => counter.Increment()));

            lazyCtxInit.IsInited.Should().BeTrue($"{nameof(dbgWayToCallDesc)}: {dbgWayToCallDesc}");
            counter.Value.Should().Be(1);

            threadResults.Where(isInitedByThisCall => isInitedByThisCall).Should().ContainSingle();
            threadResults.Where(isInitedByThisCall => !isInitedByThisCall).Should().HaveCount(threadResults.Count - 1);
        }