public async Task EnqueuesTransmissionWithSetTransmissionStatusEvent()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

                transmitter.OnEnqueue = t =>
                {
                    transmission = t;
                };

                var serializer = new TelemetrySerializer(transmitter)
                {
                    EndpointAddress = new Uri("http://expected.uri")
                };

                // Set TransmissionStatusEvent does not change the behavior, only wires up event to transmission.
                serializer.TransmissionStatusEvent += delegate(object sender, TransmissionStatusEventArgs args) { };
                var taskResult = await serializer.SerializeAsync(new[] { new StubSerializableTelemetry() }, default);

                Assert.AreEqual(serializer.EndpointAddress, transmission.EndpointAddress);
                Assert.AreEqual("application/x-json-stream", transmission.ContentType);
                Assert.AreEqual("gzip", transmission.ContentEncoding);

                var expectedContent = "{" +
                                      "\"name\":\"StubTelemetryName\"," +
                                      "\"time\":\"0001-01-01T00:00:00.0000000Z\"," +
                                      "\"data\":{\"baseType\":\"StubTelemetryBaseType\"," +
                                      "\"baseData\":{}" +
                                      "}" +
                                      "}";

                Assert.AreEqual(expectedContent, Unzip(transmission.Content));
            }
            public async Task EnqueuesTransmissionWithExpectedPropertiesForKnownTelemetry()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

                transmitter.OnEnqueue = t =>
                {
                    transmission = t;
                };

                var serializer = new TelemetrySerializer(transmitter)
                {
                    EndpointAddress = new Uri("http://expected.uri")
                };
                var taskResult = await serializer.SerializeAsync(new[] { new StubSerializableTelemetry() }, default);

                Assert.AreEqual(serializer.EndpointAddress, transmission.EndpointAddress);
                Assert.AreEqual("application/x-json-stream", transmission.ContentType);
                Assert.AreEqual("gzip", transmission.ContentEncoding);

                var expectedContent = "{" +
                                      "\"name\":\"StubTelemetryName\"," +
                                      "\"time\":\"0001-01-01T00:00:00.0000000Z\"," +
                                      "\"data\":{\"baseType\":\"StubTelemetryBaseType\"," +
                                      "\"baseData\":{}" +
                                      "}" +
                                      "}";

                Assert.AreEqual(expectedContent, Unzip(transmission.Content));
                Assert.IsTrue(taskResult);
            }
            public async Task EnqueuesTransmissionWithExpectedPropertiesForUnknownTelemetry()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

                transmitter.OnEnqueue = t =>
                {
                    transmission = t;
                    transmission.IsFlushAsyncInProgress = true;
                };

                var serializer = new TelemetrySerializer(transmitter)
                {
                    EndpointAddress = new Uri("http://expected.uri")
                };
                var taskResult = await serializer.SerializeAsync(new[] { new StubTelemetry() }, default);

                Assert.AreEqual(serializer.EndpointAddress, transmission.EndpointAddress);
                Assert.AreEqual("application/x-json-stream", transmission.ContentType);
                Assert.AreEqual("gzip", transmission.ContentEncoding);
                Assert.AreEqual("{" +
                                "\"name\":\"AppEvents\"," +
                                "\"time\":\"0001-01-01T00:00:00.0000000Z\"," +
                                "\"data\":{\"baseType\":\"EventData\"," +
                                "\"baseData\":{\"ver\":2," +
                                "\"name\":\"ConvertedTelemetry\"}" +
                                "}" +
                                "}", Unzip(transmission.Content));
                Assert.IsTrue(taskResult);
            }
            public void ReturnsBoolenTaskWhenTelemetryIsNullOrEmpty()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

                transmitter.OnEnqueue = t =>
                {
                    transmission = t;
                };

                var serializer = new TelemetrySerializer(transmitter)
                {
                    EndpointAddress = new Uri("http://expected.uri")
                };
                var result = serializer.SerializeAsync(null, default);

                Assert.IsInstanceOfType(result, typeof(Task <bool>));
                Assert.IsTrue(result.Result);

                result = serializer.SerializeAsync(new List <ITelemetry>(), default);
                Assert.IsInstanceOfType(result, typeof(Task <bool>));
                Assert.IsTrue(result.Result);
            }
            public async Task SerializeAsyncRespectsCancellationToken()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

                transmitter.OnEnqueue = t =>
                {
                    transmission = t;
                };

                var serializer = new TelemetrySerializer(transmitter)
                {
                    EndpointAddress = new Uri("http://expected.uri")
                };
                await Assert.ThrowsExceptionAsync <TaskCanceledException>(() => serializer.SerializeAsync(new[] { new StubTelemetry() }, new CancellationToken(true)));
            }
            public async Task ReturnsFalseWhenTransmissionIsNotSentOrStored()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

                transmitter.OnEnqueue = t =>
                {
                    transmission = t;
                    transmission.IsFlushAsyncInProgress = false;
                };

                var serializer = new TelemetrySerializer(transmitter)
                {
                    EndpointAddress = new Uri("http://expected.uri")
                };
                var taskResult = await serializer.SerializeAsync(new[] { new StubSerializableTelemetry() }, default);

                Assert.IsFalse(taskResult);
            }
            public void ThrowsExceptionWhenEndpointAddressIsNull()
            {
                var serializer = new TelemetrySerializer(new StubTransmitter());

                AssertEx.Throws <Exception>(() => serializer.SerializeAsync(null, default));
            }