public void FlushesBufferWhenNumberOfTelemetryItemsReachesMax()
            {
                var bufferFlushed = new ManualResetEventSlim();
                IEnumerable <ITelemetry> flushedTelemetry = null;
                var serializer = new StubTelemetrySerializer
                {
                    OnSerialize = telemetry =>
                    {
                        flushedTelemetry = telemetry.ToList();
                        bufferFlushed.Set();
                    },
                };

                var telemetryBuffer = new TelemetryBuffer(serializer, new StubApplicationLifecycle());

                var sentTelemetry = new List <ITelemetry> {
                    new StubTelemetry(), new StubTelemetry()
                };

                telemetryBuffer.Capacity = sentTelemetry.Count;
                foreach (ITelemetry item in sentTelemetry)
                {
                    telemetryBuffer.Process(item);
                }

                Assert.True(bufferFlushed.Wait(30));
                Assert.Equal(sentTelemetry, flushedTelemetry);
            }
            public async Task CallsSerializeTelemetryIfBufferIsEmpty()
            {
                bool telemetrySerialized = false;
                var  serializer          = new StubTelemetrySerializer
                {
                    OnSerializeAsync = (telemetry, cancellationToken) =>
                    {
                        telemetrySerialized = true;
                        return(Task.FromResult(true));
                    }
                };

                var telemetryBuffer = new TelemetryBuffer(serializer, new StubApplicationLifecycle());
                var taskResult      = await telemetryBuffer.FlushAsync(default);
            public void DoesntSerializeTelemetryIfBufferIsEmpty()
            {
                bool telemetrySerialized = false;
                var  serializer          = new StubTelemetrySerializer();

                serializer.OnSerialize = telemetry =>
                {
                    telemetrySerialized = true;
                };
                var telemetryBuffer = new TelemetryBuffer(serializer, new StubApplicationLifecycle());

                telemetryBuffer.FlushAsync().GetAwaiter().GetResult();

                Assert.False(telemetrySerialized);
            }
            public void StartsTimerThatFlushesBufferAfterMaxTransmissionDelay()
            {
                var telemetrySerialized = new ManualResetEventSlim();
                var serializer          = new StubTelemetrySerializer();

                serializer.OnSerialize = telemetry =>
                {
                    telemetrySerialized.Set();
                };

                var buffer = new TelemetryBuffer(serializer, new StubApplicationLifecycle());

                buffer.MaxTransmissionDelay = TimeSpan.FromMilliseconds(1);
                buffer.Process(new StubTelemetry());

                Assert.True(telemetrySerialized.Wait(50));
            }
            public void FlushesBufferToPreventLossOfTelemetry()
            {
                var applicationLifecycle = new StubApplicationLifecycle();
                var telemetrySerialized  = new ManualResetEventSlim();
                var serializer           = new StubTelemetrySerializer();

                serializer.OnSerialize = telemetry =>
                {
                    telemetrySerialized.Set();
                };
                var buffer = new TelemetryBuffer(serializer, applicationLifecycle);

                buffer.Process(new StubTelemetry());

                applicationLifecycle.OnStopping(ApplicationStoppingEventArgs.Empty);

                Assert.True(telemetrySerialized.Wait(50));
            }
            public void DoesNotCancelPreviousFlush()
            {
                var telemetrySerialized = new ManualResetEventSlim();
                var serializer          = new StubTelemetrySerializer();

                serializer.OnSerialize = telemetry =>
                {
                    telemetrySerialized.Set();
                };
                var buffer = new TelemetryBuffer(serializer, new StubApplicationLifecycle());

                buffer.MaxTransmissionDelay = TimeSpan.FromMilliseconds(1);
                buffer.Process(new StubTelemetry());

                buffer.MaxTransmissionDelay = TimeSpan.FromSeconds(42);
                buffer.Process(new StubTelemetry());

                Assert.True(telemetrySerialized.Wait(TimeSpan.FromMilliseconds(100)));
            }
            public void SerializesTelemetryIfBufferIsNotEmpty()
            {
                List <ITelemetry> serializedTelemetry = null;
                var serializer = new StubTelemetrySerializer();

                serializer.OnSerialize = telemetry =>
                {
                    serializedTelemetry = new List <ITelemetry>(telemetry);
                };

                var telemetryBuffer = new TelemetryBuffer(serializer, new StubApplicationLifecycle());

                var expectedTelemetry = new StubTelemetry();

                telemetryBuffer.Process(expectedTelemetry);

                telemetryBuffer.FlushAsync().GetAwaiter().GetResult();

                Assert.Same(expectedTelemetry, serializedTelemetry.Single());
            }
            public void CancelsPreviouslyStartedAutomaticFlushToPreventPreventPrematureTransmission()
            {
                var serializer = new StubTelemetrySerializer();
                var buffer     = new TelemetryBuffer(serializer, new StubApplicationLifecycle());

                buffer.MaxTransmissionDelay = TimeSpan.FromMilliseconds(1);
                buffer.Process(new StubTelemetry());

                buffer.MaxTransmissionDelay = TimeSpan.FromMilliseconds(100);
                buffer.FlushAsync().Wait();

                var autoFlushed = new ManualResetEventSlim();

                serializer.OnSerialize = telemetry =>
                {
                    autoFlushed.Set();
                };
                buffer.Process(new StubTelemetry());

                Assert.False(autoFlushed.Wait(30));
            }
            public void SerializesBufferOnThreadPoolToFreeUpCustomersThread()
            {
                int serializerThreadId = -1;
                var serializerInvoked  = new ManualResetEventSlim();
                var serializer         = new StubTelemetrySerializer();

                serializer.OnSerialize = telemetry =>
                {
                    serializerThreadId = Thread.CurrentThread.ManagedThreadId;
                    serializerInvoked.Set();
                };

                var telemetryBuffer = new TelemetryBuffer(serializer, new StubApplicationLifecycle());

                telemetryBuffer.Process(new StubTelemetry());

                Task dontWait = telemetryBuffer.FlushAsync();

                Assert.True(serializerInvoked.Wait(100));
                Assert.NotEqual(serializerThreadId, Thread.CurrentThread.ManagedThreadId);
            }