<commit_before>﻿using Criteo.Profiling.Tracing.Dispatcher;
using Moq;
using NUnit.Framework;

namespace Criteo.Profiling.Tracing.UTest
{
    [TestFixture]
    class T_Trace
    {
        [SetUp]
        public void SetUp()
        {
            TraceManager.ClearTracers();
            TraceManager.Stop();
        }

        [Test]
        public void TracerRecordShouldBeCalledIfTracingIsStarted()
        {
            var trace = Trace.Create();

            var mockTracer = new Mock<ITracer>();
            TraceManager.RegisterTracer(mockTracer.Object);

            TraceManager.Start(new Configuration());
            Assert.IsTrue(TraceManager.Started);

            trace.Record(Annotations.ServerSend());
            TraceManager.Stop();

            mockTracer.Verify(tracer => tracer.Record(It.IsAny<Record>()), Times.Once());
        }

        [Test]
        public void RecordsShouldntBeSentToTracersIfTracingIsStopped()
        {
            var trace = Trace.Create();

            var mockTracer = new Mock<ITracer>();
            TraceManager.RegisterTracer(mockTracer.Object);

            Assert.IsFalse(TraceManager.Started);

            trace.Record(Annotations.ServerSend());

            mockTracer.Verify(tracer => tracer.Record(It.IsAny<Record>()), Times.Never());
        }

        [Test]
        public void ChildTraceIsCorrectlyCreated()
        {
            var parent = Trace.Create();
            var child = parent.Child();

            // Should share the same global id
            Assert.AreEqual(parent.CurrentSpan.TraceId, child.CurrentSpan.TraceId);
            Assert.AreEqual(parent.CorrelationId, child.CorrelationId);

            // Parent id of the child should be the parent span id
            Assert.AreEqual(parent.CurrentSpan.SpanId, child.CurrentSpan.ParentSpanId);

            // Flags should be copied
            Assert.AreEqual(parent.CurrentSpan.Flags, child.CurrentSpan.Flags);

            // Child cannot have the same span id
            Assert.AreNotEqual(parent.CurrentSpan.SpanId, child.CurrentSpan.SpanId);
        }

        [Test]
        public void TraceCreatesCorrectRecord()
        {
            var trace = Trace.Create();

            var dispatcher = new Mock<IRecordDispatcher>();
            TraceManager.Start(new Configuration(), dispatcher.Object);

            var clientRcv = Annotations.ClientRecv();
            trace.Record(clientRcv);

            dispatcher.Verify(d => d.Dispatch(It.Is<Record>(r => r.Annotation == clientRcv && r.SpanState.Equals(trace.CurrentSpan))), Times.Once());
        }


        [TestCase(SpanFlags.SamplingKnown)]
        [TestCase(SpanFlags.None)]
        [TestCase(SpanFlags.SamplingKnown | SpanFlags.Sampled)]
        public void TraceSamplingForced(SpanFlags initialFlags)
        {
            var spanState = new SpanState(1, 0, 1, initialFlags);
            var trace = Trace.CreateFromId(spanState);

            trace.ForceSampled();
            Assert.AreEqual(SamplingStatus.Sampled, trace.CurrentSpan.SamplingStatus);
        }

    }
}<commit_msg>Add unit test Record() with specified time<commit_after>﻿using System;
using Criteo.Profiling.Tracing.Dispatcher;
using Moq;
using NUnit.Framework;

namespace Criteo.Profiling.Tracing.UTest
{
    [TestFixture]
    class T_Trace
    {
        [SetUp]
        public void SetUp()
        {
            TraceManager.ClearTracers();
            TraceManager.Stop();
        }

        [Test]
        public void TracerRecordShouldBeCalledIfTracingIsStarted()
        {
            var trace = Trace.Create();

            var tracer = new Mock<ITracer>();
            TraceManager.RegisterTracer(tracer.Object);

            TraceManager.Start(new Configuration());
            Assert.IsTrue(TraceManager.Started);

            trace.Record(Annotations.ServerSend());
            TraceManager.Stop();

            tracer.Verify(t => t.Record(It.IsAny<Record>()), Times.Once());
        }

        [Test]
        public void RecordsShouldntBeSentToTracersIfTracingIsStopped()
        {
            var trace = Trace.Create();

            var tracer = new Mock<ITracer>();
            TraceManager.RegisterTracer(tracer.Object);

            Assert.IsFalse(TraceManager.Started);

            trace.Record(Annotations.ServerSend());

            tracer.Verify(t => t.Record(It.IsAny<Record>()), Times.Never());
        }

        [Test]
        public void ChildTraceIsCorrectlyCreated()
        {
            var parentTrace = Trace.Create();
            var childTrace = parentTrace.Child();

            // Should share the same global id
            Assert.AreEqual(parentTrace.CurrentSpan.TraceId, childTrace.CurrentSpan.TraceId);
            Assert.AreEqual(parentTrace.CorrelationId, childTrace.CorrelationId);

            // Parent id of the child should be the parent span id
            Assert.AreEqual(parentTrace.CurrentSpan.SpanId, childTrace.CurrentSpan.ParentSpanId);

            // Flags should be copied
            Assert.AreEqual(parentTrace.CurrentSpan.Flags, childTrace.CurrentSpan.Flags);

            // Child cannot have the same span id
            Assert.AreNotEqual(parentTrace.CurrentSpan.SpanId, childTrace.CurrentSpan.SpanId);
        }

        [Test]
        public void TraceCreatesCorrectRecord()
        {
            var trace = Trace.Create();

            var dispatcher = new Mock<IRecordDispatcher>();
            TraceManager.Start(new Configuration(), dispatcher.Object);

            var someAnnotation = Annotations.ClientRecv();
            trace.Record(someAnnotation);

            dispatcher.Verify(d => d.Dispatch(It.Is<Record>(r => r.Annotation.Equals(someAnnotation) && r.SpanState.Equals(trace.CurrentSpan))), Times.Once());
        }

        [Test]
        public void TraceCreatesCorrectRecordWithTimeSpecified()
        {
            var trace = Trace.Create();

            var dispatcher = new Mock<IRecordDispatcher>();
            TraceManager.Start(new Configuration(), dispatcher.Object);

            var someAnnotation = Annotations.ClientRecv();
            var recordTime = new DateTime(2010, 2, 3, 14, 3, 1, DateTimeKind.Utc);
            trace.Record(someAnnotation, recordTime);

            dispatcher.Verify(d => d.Dispatch(It.Is<Record>(r => r.Annotation.Equals(someAnnotation) && r.SpanState.Equals(trace.CurrentSpan) && r.Timestamp.Equals(recordTime))), Times.Once());
        }


        [TestCase(SpanFlags.SamplingKnown)]
        [TestCase(SpanFlags.None)]
        [TestCase(SpanFlags.SamplingKnown | SpanFlags.Sampled)]
        public void TraceSamplingForced(SpanFlags initialFlags)
        {
            var spanState = new SpanState(1, 0, 1, initialFlags);
            var trace = Trace.CreateFromId(spanState);

            trace.ForceSampled();
            Assert.AreEqual(SamplingStatus.Sampled, trace.CurrentSpan.SamplingStatus);
        }

    }
}
{'fraction_non_alphanumeric': 0.10636758321273516, 'fraction_numerical': 0.0023154848046309695, 'mean_word_length': 2.2599056603773584, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2388328', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1819, 'n_words': 338}