public void NewTelemetryConfigurationWithoutChannelCreatesDefaultInMemoryChannel()
        {
            TelemetryConfiguration config = new TelemetryConfiguration();
            var channel = config.TelemetryChannel as Channel.InMemoryChannel;

            Assert.IsNotNull(channel);
            config.Dispose();
            Assert.IsTrue(channel.IsDisposed);
        }
        public void NewTelemetryConfigurationWithInstrumentationKeyButNoChannelCreatesDefaultInMemoryChannel()
        {
            string expectedKey            = "expected";
            TelemetryConfiguration config = new TelemetryConfiguration(expectedKey);

            Assert.AreEqual(expectedKey, config.InstrumentationKey);
            var channel = config.TelemetryChannel as Channel.InMemoryChannel;

            Assert.IsNotNull(channel);
            config.Dispose();
            Assert.IsTrue(channel.IsDisposed);
        }
        public void NewTelemetryConfigurationWithChannelUsesSpecifiedChannel()
        {
            StubTelemetryChannel stubChannel = new StubTelemetryChannel();
            bool channelDisposed             = false;

            stubChannel.OnDispose += () => { channelDisposed = true; };
            TelemetryConfiguration config = new TelemetryConfiguration(string.Empty, stubChannel);

            Assert.AreSame(stubChannel, config.TelemetryChannel);
            config.Dispose();
            Assert.IsFalse(channelDisposed);
        }
        public void ConfigurationDisposesAllSinks()
        {
            var configuration      = new TelemetryConfiguration();
            var commonChainBuilder = new TelemetryProcessorChainBuilder(configuration);

            configuration.TelemetryProcessorChainBuilder = commonChainBuilder;

            var  firstChannel         = new StubTelemetryChannel();
            bool firstChannelDisposed = false;

            firstChannel.OnDispose = () => firstChannelDisposed = true;
            configuration.DefaultTelemetrySink.TelemetryChannel = firstChannel;
            var  firstSinkChainBuilder = new TelemetryProcessorChainBuilder(configuration, configuration.DefaultTelemetrySink);
            bool firstSinkTelemetryProcessorDisposed = false;

            firstSinkChainBuilder.Use((next) =>
            {
                var firstSinkTelemetryProcessor       = new StubTelemetryProcessor(next);
                firstSinkTelemetryProcessor.OnDispose = () => firstSinkTelemetryProcessorDisposed = true;
                return(firstSinkTelemetryProcessor);
            });
            configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder = firstSinkChainBuilder;

            var  secondChannel         = new StubTelemetryChannel();
            bool secondChannelDisposed = false;

            secondChannel.OnDispose = () => secondChannelDisposed = true;
            var  secondSink             = new TelemetrySink(configuration, secondChannel);
            var  secondSinkChainBuilder = new TelemetryProcessorChainBuilder(configuration, secondSink);
            bool secondSinkTelemetryProcessorDisposed = false;

            secondSinkChainBuilder.Use((next) =>
            {
                var secondSinkTelemetryProcessor       = new StubTelemetryProcessor(next);
                secondSinkTelemetryProcessor.OnDispose = () => secondSinkTelemetryProcessorDisposed = true;
                return(secondSinkTelemetryProcessor);
            });
            secondSink.TelemetryProcessorChainBuilder = secondSinkChainBuilder;
            configuration.TelemetrySinks.Add(secondSink);

            var client = new TelemetryClient(configuration);

            client.TrackTrace("t1");
            configuration.Dispose();

            // We expect the channels to not be disposed (because they were created externally to sinks), but the processors should be disposed.
            Assert.IsTrue(firstSinkTelemetryProcessorDisposed);
            Assert.IsTrue(secondSinkTelemetryProcessorDisposed);
            Assert.IsFalse(firstChannelDisposed);
            Assert.IsFalse(secondChannelDisposed);
        }
        public void NewTelemetryConfigurationWithInstrumentationKeyAndChannelUsesSpecifiedKeyAndChannel()
        {
            string expectedKey = "expected";
            StubTelemetryChannel stubChannel = new StubTelemetryChannel();
            bool channelDisposed             = false;

            stubChannel.OnDispose += () => { channelDisposed = true; };
            TelemetryConfiguration config = new TelemetryConfiguration(expectedKey, stubChannel);

            Assert.AreEqual(expectedKey, config.InstrumentationKey);
            Assert.AreSame(stubChannel, config.TelemetryChannel);
            config.Dispose();
            Assert.IsFalse(channelDisposed);
        }