public void Setup()
        {
            // Create a Storage Mock
            this.StorageBaseMock = new Moq.Mock<StorageBase>();
            this.OnEnqueue = null;

            // Setup  Storage.EnqueueAsync to call OnEnqueue and increase this.numberOfCallsToStorageEnqueue by 1
            this.StorageBaseMock.Setup((storage) => storage.EnqueueAsync(It.IsAny<Transmission>()))
                .Callback(
                () =>
                {
                    this.numberOfCallsToStorageEnqueue++;
                    if (OnEnqueue != null)
                    {
                        OnEnqueue();
                    }
                })
                .Returns(
                () =>
                {
                    var completionSource = new TaskCompletionSource<bool>();
                    completionSource.SetResult(true);
                    return completionSource.Task as Task;
                });

            // Create telemetry buffer mock
            this.TelemetryBufferMock = new TelemetryBuffer();
            this.TelemetryBufferMock.Capacity = 1;
        }
        public void WhenNewValueIsLessThanOneSetToDefault()
        {
            var buffer = new TelemetryBuffer();
            buffer.Capacity = 0;

            Assert.Equal(buffer.Capacity, 500);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PersistenceChannel"/> class.
 /// </summary>
 /// <param name="storageFolderName">
 /// A folder name. Under this folder all the transmissions will be saved. 
 /// Setting this value groups channels, even from different processes. 
 /// If 2 (or more) channels has the same <c>storageFolderName</c> only one channel will perform the sending even if the channel is in a different process/AppDomain/Thread.  
 /// </param>
 /// <param name="sendersCount">
 /// Defines the number of senders. A sender is a long-running thread that sends telemetry batches in intervals defined by <see cref="SendingInterval"/>. 
 /// So the amount of senders also defined the maximum amount of http channels opened at the same time.
 /// </param>        
 public PersistenceChannel(string storageFolderName, int sendersCount = 3)
 {   
     this.TelemetryBuffer = new TelemetryBuffer();
     this.storage = new Storage(storageFolderName);
     this.Transmitter = new PersistenceTransmitter(this.storage, sendersCount);
     this.flushManager = new FlushManager(this.storage, this.TelemetryBuffer);
     this.EndpointAddress = Constants.TelemetryServiceEndpoint;
     this.developerMode = false;
 }
        public void TelemetryBufferCallingOnFullActionWhenBufferCapacityReached()
        {
            IEnumerable<ITelemetry> items = null;
            TelemetryBuffer buffer = new TelemetryBuffer { Capacity = 2 };
            buffer.OnFull = () => { items = buffer.Dequeue(); };

            buffer.Enqueue(new EventTelemetry("Event1"));
            buffer.Enqueue(new EventTelemetry("Event2"));

            Assert.NotNull(items);
            Assert.Equal(2, items.Count());
        }
        public void WhenSendIsCalledTheEventIsBeingQueuedInTheBuffer()
        {
            var telemetryBuffer = new TelemetryBuffer();
            var channel = new InMemoryChannel(telemetryBuffer, new InMemoryTransmitter(telemetryBuffer));
            var sentTelemetry = new StubTelemetry();

            channel.Send(sentTelemetry);
            IEnumerable<ITelemetry> telemetries = telemetryBuffer.Dequeue();

            Assert.Equal(1, telemetries.Count());
            Assert.Same(sentTelemetry, telemetries.First());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FlushManager"/> class.
        /// </summary>        
        /// <param name="storage">The storage that persists the telemetries.</param>
        /// <param name="telemetryBuffer">In memory buffer that holds telemetries.</param>
        /// <param name="supportAutoFlush">A boolean value that determines if flush will happen automatically. Used by unit tests.</param>
        internal FlushManager(StorageBase storage, TelemetryBuffer telemetryBuffer, bool supportAutoFlush = true)
        {
            this.storage = storage;
            this.telemetryBuffer = telemetryBuffer;
            this.telemetryBuffer.OnFull = this.OnTelemetryBufferFull;
            this.FlushDelay = TimeSpan.FromSeconds(30);

            if (supportAutoFlush)
            {
                Task.Factory.StartNew(this.FlushLoop, TaskCreationOptions.LongRunning)
                    .ContinueWith(t => CoreEventSource.Log.LogVerbose("FlushManager: Failure in FlushLoop: Exception: " + t.Exception.ToString()), TaskContinuationOptions.OnlyOnFaulted);
            }
        }
        internal InMemoryTransmitter(TelemetryBuffer buffer)
        {
            this.buffer = buffer;
            this.buffer.OnFull = this.OnBufferFull;

            // Starting the Runner
            Task.Factory.StartNew(this.Runner, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)
                .ContinueWith(
                    task => 
                    {
                        string msg = string.Format(CultureInfo.InvariantCulture, "InMemoryTransmitter: Unhandled exception in Runner: {0}", task.Exception);
                        CoreEventSource.Log.LogVerbose(msg);
                    }, 
                    TaskContinuationOptions.OnlyOnFaulted);
        }
        internal ServerTelemetryChannel(INetwork network, IApplicationLifecycle applicationLifecycle)
        {
            var policies = new TransmissionPolicy[] 
            { 
                new ApplicationLifecycleTransmissionPolicy(applicationLifecycle),
                new ErrorHandlingTransmissionPolicy(),
                new NetworkAvailabilityTransmissionPolicy(network),
                new ThrottlingTransmissionPolicy()
            };

            this.Transmitter = new Transmitter(policies: policies);

            this.TelemetrySerializer = new TelemetrySerializer(this.Transmitter);
            this.TelemetryBuffer = new TelemetryBuffer(this.TelemetrySerializer, applicationLifecycle);
            this.telemetryBufferCapacity = this.TelemetryBuffer.Capacity;

            this.TelemetryProcessor = this.TelemetryBuffer;
            this.isInitialized = false;
        }
Exemplo n.º 9
0
        public void TelemetryWithNoInstrumentationKeyIsDropped()
        {
            var telemetryBuffer = new TelemetryBuffer();
            var channel         = new InMemoryChannel(telemetryBuffer, new InMemoryTransmitter(telemetryBuffer));
            var sentTelemetry   = new StubTelemetry();

            // No instrumentation key

            using (TestEventListener listener = new TestEventListener())
            {
                listener.EnableEvents(CoreEventSource.Log, EventLevel.Verbose);

                channel.Send(sentTelemetry);
                IEnumerable <ITelemetry> telemetries = telemetryBuffer.Dequeue();

                Assert.IsNull(telemetries);

                var expectedMessage = listener.Messages.First();
                Assert.AreEqual(35, expectedMessage.EventId);
            }
        }
        public void TelemetryBufferDoesNotGrowBeyondMaxBacklogSize()
        {
            TelemetryBuffer buffer = new TelemetryBuffer {
                Capacity = 2, BacklogSize = 1002
            };

            buffer.OnFull = () => { //intentionally blank to simulate situation where buffer
                                    //is not emptied.
            };

            // Add more items to buffer than the max backlog size
            for (int i = 0; i < 1005; i++)
            {
                buffer.Enqueue(new EventTelemetry("Event" + i));
            }



            // validate that items are not added after maxunsentbacklogsize is reached.
            // this also validate that items can still be added after Capacity is reached as it is only a soft limit.
            int bufferItemCount = buffer.Dequeue().Count();

            Assert.AreEqual(1002, bufferItemCount);
        }
            public void ThrowsArgumentOutOfRangeExceptionWhenNewValueIsLessThanOne()
            {
                var buffer = new TelemetryBuffer();

                Assert.Throws <ArgumentOutOfRangeException>(() => buffer.Capacity = 0);
            }
 /// <summary>
 /// Initializes a new instance of the <see cref="InMemoryChannel" /> class. Used in unit tests for constructor injection.  
 /// </summary>
 /// <param name="telemetryBuffer">The telemetry buffer that will be used to enqueue new events.</param>
 /// <param name="transmitter">The in memory transmitter that will send the events queued in the buffer.</param>
 internal InMemoryChannel(TelemetryBuffer telemetryBuffer, InMemoryTransmitter transmitter)
 {
     this.buffer = telemetryBuffer;
     this.transmitter = transmitter;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InMemoryChannel" /> class.
 /// </summary>
 public InMemoryChannel()
 {
     this.buffer      = new TelemetryBuffer();
     this.transmitter = new InMemoryTransmitter(this.buffer);
 }
 public void ThrowsArgumentOutOfRangeExceptionWhenNewValueIsLessThanOne()
 {
     var buffer = new TelemetryBuffer();
     Assert.Throws<ArgumentOutOfRangeException>(() => buffer.Capacity = 0);
 }
 public void DefaultValueIsAppropriateForProductionEnvironmentAndUnitTests()
 {
     var buffer = new TelemetryBuffer();
     Assert.Equal(500, buffer.Capacity);
 }
Exemplo n.º 16
0
        public void DefaultValueIsAppropriateForProductionEnvironmentAndUnitTests()
        {
            var buffer = new TelemetryBuffer();

            Assert.Equal(500, buffer.Capacity);
        }
 public void CanBeSetByChannelToTunePerformance()
 {
     var buffer = new TelemetryBuffer();
     buffer.Capacity = 42;
     Assert.Equal(42, buffer.Capacity);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InMemoryChannel" /> class. Used in unit tests for constructor injection.
 /// </summary>
 /// <param name="telemetryBuffer">The telemetry buffer that will be used to enqueue new events.</param>
 /// <param name="transmitter">The in memory transmitter that will send the events queued in the buffer.</param>
 internal InMemoryChannel(TelemetryBuffer telemetryBuffer, InMemoryTransmitter transmitter)
 {
     this.buffer      = telemetryBuffer;
     this.transmitter = transmitter;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InMemoryChannel" /> class.
 /// </summary>
 public InMemoryChannel()
 {
     this.buffer = new TelemetryBuffer();
     this.transmitter = new InMemoryTransmitter(this.buffer);
 }