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));
            }
Ejemplo n.º 2
0
            public void EnqueuesTransmissionWithExpectedPropertiesForUnknownTelemetry()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

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

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

                serializer.Serialize(new[] { new StubTelemetry() });

                Assert.AreEqual(serializer.EndpointAddress, transmission.EndpointAddress);
                Assert.AreEqual("application/x-json-stream", transmission.ContentType);
                Assert.AreEqual("gzip", transmission.ContentEncoding);
                Assert.AreEqual("{" +
                                "\"name\":\"Microsoft.ApplicationInsights.Event\"," +
                                "\"time\":\"0001-01-01T00:00:00.0000000Z\"," +
                                "\"data\":{\"baseType\":\"EventData\"," +
                                "\"baseData\":{\"ver\":2," +
                                "\"name\":\"ConvertedTelemetry\"}" +
                                "}" +
                                "}", Unzip(transmission.Content));
            }
 public void CanBeChangedByChannelBasedOnConfigurationToRedirectTelemetryToDifferentEnvironment()
 {
     var serializer = new TelemetrySerializer(new StubTransmitter());
     var expectedValue = new Uri("int://environment");
     serializer.EndpointAddress = expectedValue;
     Assert.Equal(expectedValue, serializer.EndpointAddress);
 }
            public void EnqueuesTransmissionWithExpectedPropertiesForKnownTelemetry()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

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

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

                serializer.Serialize(new[] { new StubSerializableTelemetry() });

                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 void DoesNotContinueAsyncOperationsOnCapturedSynchronizationContextToImprovePerformance()
            {
                var transmitter = new StubTransmitter()
                {
                    OnEnqueue = transmission => Task.Run(() => { })
                };
                var serializer = new TelemetrySerializer(transmitter)
                {
                    EndpointAddress = new Uri("https://127.0.0.1")
                };

                bool postedBack = false;

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

                    serializer.Serialize(new[] { new StubTelemetry() });
                }

                Assert.IsFalse(postedBack);
            }
            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);
            }
Ejemplo n.º 7
0
            public void CanBeChangedByChannelBasedOnConfigurationToRedirectTelemetryToDifferentEnvironment()
            {
                var serializer    = new TelemetrySerializer(new StubTransmitter());
                var expectedValue = new Uri("int://environment");

                serializer.EndpointAddress = expectedValue;
                Assert.AreEqual(expectedValue, serializer.EndpointAddress);
            }
            public void SerializesTelemetryOnSameThreadBecauseItAssumesItIsAlreadyOnThreadPool()
            {
                int serializationThreadId = -1;
                var transmitter = new StubTransmitter();
                transmitter.OnEnqueue = t => 
                {
                    serializationThreadId = Thread.CurrentThread.ManagedThreadId;
                };

                var serializer = new TelemetrySerializer(transmitter);
                serializer.Serialize(new[] { new StubTelemetry() });

                Assert.Equal(serializationThreadId, Thread.CurrentThread.ManagedThreadId);
            }
Ejemplo n.º 9
0
            public void SerializesTelemetryOnSameThreadBecauseItAssumesItIsAlreadyOnThreadPool()
            {
                int serializationThreadId = -1;
                var transmitter           = new StubTransmitter();

                transmitter.OnEnqueue = t =>
                {
                    serializationThreadId = Thread.CurrentThread.ManagedThreadId;
                };

                var serializer = new TelemetrySerializer(transmitter);

                serializer.Serialize(new[] { new StubTelemetry() });

                Assert.AreEqual(serializationThreadId, Thread.CurrentThread.ManagedThreadId);
            }
            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 TelemetryBuffer(TelemetrySerializer serializer, IApplicationLifecycle applicationLifecycle)
            : this()
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            if (applicationLifecycle == null)
            {
                throw new ArgumentNullException("applicationLifecycle");
            }

            this.serializer = serializer;

            applicationLifecycle.Stopping += this.HandleApplicationStoppingEvent;
        }
        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 Implementation.TelemetryBuffer(this.TelemetrySerializer, applicationLifecycle);
            this.telemetryBufferCapacity = this.TelemetryBuffer.Capacity;

            this.TelemetryProcessor = this.TelemetryBuffer;
        }
            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);
            }
Ejemplo n.º 14
0
            public void EnqueuesTransmissionWithExpectedProperties()
            {
                Transmission transmission = null;
                var          transmitter  = new StubTransmitter();

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

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

                serializer.Serialize(new[] { new StubTelemetry() });

                Assert.Equal(serializer.EndpointAddress, transmission.EndpointAddress);
                Assert.Equal("application/x-json-stream", transmission.ContentType);
                Assert.Equal("gzip", transmission.ContentEncoding);
                Assert.Equal(string.Empty, Unzip(transmission.Content));
            }
        public TelemetryBuffer(TelemetrySerializer serializer, IApplicationLifecycle applicationLifecycle)
            : this()
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

#if !NETSTANDARD
            // We don't have implementation for IApplicationLifecycle for .NET Core
            if (applicationLifecycle == null)
            {
                throw new ArgumentNullException("applicationLifecycle");
            }
#endif

            if (applicationLifecycle != null)
            {
                applicationLifecycle.Stopping += this.HandleApplicationStoppingEvent;
            }

            this.serializer = serializer;
        }
            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 void DefaultValuePointsToModernEndpointInProductionEnvironment()
 {
     var serializer = new TelemetrySerializer(new StubTransmitter());
     Assert.Equal("https://dc.services.visualstudio.com/v2/track", serializer.EndpointAddress.ToString());
 }
 public void ThrowsArgumentNullExceptionWhenTelemetryIsNullToPreventUsageErrors()
 {
     var serializer = new TelemetrySerializer(new StubTransmitter());
     Assert.Throws<ArgumentNullException>(() => serializer.Serialize(null));
 }
Ejemplo n.º 19
0
            public void ThrowsArgumentExceptionWhenTelemetryIsEmptyToPreventUsageErrors()
            {
                var serializer = new TelemetrySerializer(new StubTransmitter());

                AssertEx.Throws <ArgumentException>(() => serializer.Serialize(new List <ITelemetry>()));
            }
Ejemplo n.º 20
0
            public void ThrowsArgumentNullExceptionWhenTelemetryIsNullToPreventUsageErrors()
            {
                var serializer = new TelemetrySerializer(new StubTransmitter());

                AssertEx.Throws <ArgumentNullException>(() => serializer.Serialize(null));
            }
 public void ThrowsArgumentExceptionWhenTelemetryIsEmptyToPreventUsageErrors()
 {
     var serializer = new TelemetrySerializer(new StubTransmitter());
     Assert.Throws<ArgumentException>(() => serializer.Serialize(Enumerable.Empty<ITelemetry>()));
 }
Ejemplo n.º 22
0
            public void SetterThrowsArgumentNullExceptionToPreventUsageErrors()
            {
                var serializer = new TelemetrySerializer(new StubTransmitter());

                AssertEx.Throws <ArgumentNullException>(() => serializer.EndpointAddress = null);
            }
Ejemplo n.º 23
0
            public void DefaultValuePointsToModernEndpointInProductionEnvironment()
            {
                var serializer = new TelemetrySerializer(new StubTransmitter());

                Assert.AreEqual("https://dc.services.visualstudio.com/v2/track", serializer.EndpointAddress.ToString());
            }
            public void DefaultValueIsNull()
            {
                var serializer = new TelemetrySerializer(new StubTransmitter());

                Assert.AreEqual(null, serializer.EndpointAddress);
            }
     public void EnqueuesTransmissionWithExpectedProperties()
     {
         Transmission transmission = null;
         var transmitter = new StubTransmitter();
         transmitter.OnEnqueue = t =>
         {
             transmission = t;
         };
 
         var serializer = new TelemetrySerializer(transmitter) { EndpointAddress = new Uri("http://expected.uri") };
         serializer.Serialize(new[] { new StubTelemetry() });
 
         Assert.Equal(serializer.EndpointAddress, transmission.EndpointAddress);
         Assert.Equal("application/x-json-stream", transmission.ContentType);
         Assert.Equal("gzip", transmission.ContentEncoding);
         Assert.Equal(string.Empty, Unzip(transmission.Content));
     }
            public void ThrowsExceptionWhenEndpointAddressIsNull()
            {
                var serializer = new TelemetrySerializer(new StubTransmitter());

                AssertEx.Throws <Exception>(() => serializer.SerializeAsync(null, default));
            }
 public void SetterThrowsArgumentNullExceptionToPreventUsageErrors()
 {
     var serializer = new TelemetrySerializer(new StubTransmitter());
     Assert.Throws<ArgumentNullException>(() => serializer.EndpointAddress = null);
 }
            public void DoesNotContinueAsyncOperationsOnCapturedSynchronizationContextToImprovePerformance()
            {
                var transmitter = new StubTransmitter() { OnEnqueue = transmission => TaskEx.Run(() => { }) };
                var serializer = new TelemetrySerializer(transmitter);

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

                    serializer.Serialize(new[] { new StubTelemetry() });
                }

                Assert.False(postedBack);
            }