public void EmptiesBufferAfterSerialization()
            {
                var buffer = new TelemetryBuffer(new StubTelemetrySerializer(), new StubApplicationLifecycle());

                buffer.Capacity = 10;
                buffer.Process(new StubTelemetry());

                buffer.FlushAsync().GetAwaiter().GetResult();

                Assert.Equal(0, buffer.Count());
            }
            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 WaitsUntilTelemetryBufferIsSafeToModify()
            {
                var telemetryBuffer = new TelemetryBuffer(new StubTelemetrySerializer(), new StubApplicationLifecycle());

                telemetryBuffer.Process(new StubTelemetry());

                Task anotherThread;

                lock (telemetryBuffer)
                {
                    anotherThread = TaskEx.Run(() => telemetryBuffer.FlushAsync());
                    Assert.False(anotherThread.Wait(10));
                }

                Assert.True(anotherThread.Wait(50));
            }
            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 DoesNotContinueOnCapturedSynchronizationContextToImprovePerformance()
            {
                var buffer = new TelemetryBuffer(new StubTelemetrySerializer(), new StubApplicationLifecycle());

                buffer.Process(new StubTelemetry());

                bool postedBack = false;

                using (var context = new StubSynchronizationContext())
                {
                    context.OnPost = (callback, state) =>
                    {
                        postedBack = true;
                        callback(state);
                    };

                    buffer.FlushAsync().GetAwaiter().GetResult();

                    Assert.False(postedBack);
                }
            }
            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);
            }