예제 #1
0
        public void MultiSubscriber()
        {
            var subscriber1Result = new List <KeyValuePair <string, object> >();
            Predicate <string> subscriber1Predicate = telemetryName => (telemetryName == "DataForSubscriber1");
            var subscriber1Oberserver = new ObserverToList <TelemData>(subscriber1Result);

            var subscriber2Result = new List <KeyValuePair <string, object> >();
            Predicate <string> subscriber2Predicate = telemetryName => (telemetryName == "DataForSubscriber2");
            var subscriber2Oberserver = new ObserverToList <TelemData>(subscriber2Result);

            // Get two subscribers going.
            using (var subscription1 = TelemetryListener.DefaultListener.Subscribe(subscriber1Oberserver, subscriber1Predicate))
            {
                using (var subscription2 = TelemetryListener.DefaultListener.Subscribe(subscriber2Oberserver, subscriber2Predicate))
                {
                    // Things that neither subscribe to get filtered out.
                    if (TelemetryListener.DefaultListener.IsEnabled("DataToFilterOut"))
                    {
                        TelemetryListener.DefaultListener.WriteTelemetry("DataToFilterOut", -1);
                    }

                    Assert.Equal(0, subscriber1Result.Count);
                    Assert.Equal(0, subscriber2Result.Count);

                    /****************************************************/
                    // If a Source does not use the IsEnabled, then every subscriber gets it.
                    subscriber1Result.Clear();
                    subscriber2Result.Clear();
                    TelemetryListener.DefaultListener.WriteTelemetry("UnfilteredData", 3);

                    Assert.Equal(1, subscriber1Result.Count);
                    Assert.Equal("UnfilteredData", subscriber1Result[0].Key);
                    Assert.Equal(3, (int)subscriber1Result[0].Value);

                    Assert.Equal(1, subscriber2Result.Count);
                    Assert.Equal("UnfilteredData", subscriber2Result[0].Key);
                    Assert.Equal(3, (int)subscriber2Result[0].Value);

                    /****************************************************/
                    // Filters not filter out everything, they are just a performance optimization.
                    // Here you actually get more than you want even though you use a filter
                    subscriber1Result.Clear();
                    subscriber2Result.Clear();
                    if (TelemetryListener.DefaultListener.IsEnabled("DataForSubscriber1"))
                    {
                        TelemetryListener.DefaultListener.WriteTelemetry("DataForSubscriber1", 1);
                    }

                    Assert.Equal(1, subscriber1Result.Count);
                    Assert.Equal("DataForSubscriber1", subscriber1Result[0].Key);
                    Assert.Equal(1, (int)subscriber1Result[0].Value);

                    // Subscriber 2 happens to get it
                    Assert.Equal(1, subscriber2Result.Count);
                    Assert.Equal("DataForSubscriber1", subscriber2Result[0].Key);
                    Assert.Equal(1, (int)subscriber2Result[0].Value);

                    /****************************************************/
                    subscriber1Result.Clear();
                    subscriber2Result.Clear();
                    if (TelemetryListener.DefaultListener.IsEnabled("DataForSubscriber2"))
                    {
                        TelemetryListener.DefaultListener.WriteTelemetry("DataForSubscriber2", 2);
                    }

                    // Subscriber 1 happens to get it
                    Assert.Equal(1, subscriber1Result.Count);
                    Assert.Equal("DataForSubscriber2", subscriber1Result[0].Key);
                    Assert.Equal(2, (int)subscriber1Result[0].Value);

                    Assert.Equal(1, subscriber2Result.Count);
                    Assert.Equal("DataForSubscriber2", subscriber2Result[0].Key);
                    Assert.Equal(2, (int)subscriber2Result[0].Value);
                }   // subscriber2 drops out

                /*********************************************************************/
                /* Only Subscriber 1 is left */
                /*********************************************************************/

                // Things that neither subscribe to get filtered out.
                subscriber1Result.Clear();
                subscriber2Result.Clear();
                if (TelemetryListener.DefaultListener.IsEnabled("DataToFilterOut"))
                {
                    TelemetryListener.DefaultListener.WriteTelemetry("DataToFilterOut", -1);
                }

                Assert.Equal(0, subscriber1Result.Count);
                Assert.Equal(0, subscriber2Result.Count);

                /****************************************************/
                // If a Source does not use the IsEnabled, then every subscriber gets it.
                subscriber1Result.Clear();
                TelemetryListener.DefaultListener.WriteTelemetry("UnfilteredData", 3);

                Assert.Equal(1, subscriber1Result.Count);
                Assert.Equal("UnfilteredData", subscriber1Result[0].Key);
                Assert.Equal(3, (int)subscriber1Result[0].Value);

                // Subscriber 2 has dropped out.
                Assert.Equal(0, subscriber2Result.Count);

                /****************************************************/
                // Filters not filter out everything, they are just a performance optimization.
                // Here you actually get more than you want even though you use a filter
                subscriber1Result.Clear();
                if (TelemetryListener.DefaultListener.IsEnabled("DataForSubscriber1"))
                {
                    TelemetryListener.DefaultListener.WriteTelemetry("DataForSubscriber1", 1);
                }

                Assert.Equal(1, subscriber1Result.Count);
                Assert.Equal("DataForSubscriber1", subscriber1Result[0].Key);
                Assert.Equal(1, (int)subscriber1Result[0].Value);

                // Subscriber 2 has dropped out.
                Assert.Equal(0, subscriber2Result.Count);

                /****************************************************/
                subscriber1Result.Clear();
                if (TelemetryListener.DefaultListener.IsEnabled("DataForSubscriber2"))
                {
                    TelemetryListener.DefaultListener.WriteTelemetry("DataForSubscriber2", 2);
                }

                // Subscriber 1 filters
                Assert.Equal(0, subscriber1Result.Count);
                // Subscriber 2 has dropped out
                Assert.Equal(0, subscriber2Result.Count);
            } // subscriber1 drops out

            /*********************************************************************/
            /* No Subscribers are left */
            /*********************************************************************/

            // Things that neither subscribe to get filtered out.
            subscriber1Result.Clear();
            subscriber2Result.Clear();
            if (TelemetryListener.DefaultListener.IsEnabled("DataToFilterOut"))
            {
                TelemetryListener.DefaultListener.WriteTelemetry("DataToFilterOut", -1);
            }

            Assert.Equal(0, subscriber1Result.Count);
            Assert.Equal(0, subscriber2Result.Count);

            /****************************************************/
            // If a Source does not use the IsEnabled, then every subscriber gets it.

            TelemetryListener.DefaultListener.WriteTelemetry("UnfilteredData", 3);

            // No one subscribing
            Assert.Equal(0, subscriber1Result.Count);
            Assert.Equal(0, subscriber2Result.Count);

            /****************************************************/
            // Filters not filter out everything, they are just a performance optimization.
            // Here you actually get more than you want even though you use a filter
            if (TelemetryListener.DefaultListener.IsEnabled("DataForSubscriber1"))
            {
                TelemetryListener.DefaultListener.WriteTelemetry("DataForSubscriber1", 1);
            }

            // No one subscribing
            Assert.Equal(0, subscriber1Result.Count);
            Assert.Equal(0, subscriber2Result.Count);

            /****************************************************/
            if (TelemetryListener.DefaultListener.IsEnabled("DataForSubscriber2"))
            {
                TelemetryListener.DefaultListener.WriteTelemetry("DataForSubscriber2", 2);
            }

            // No one subscribing
            Assert.Equal(0, subscriber1Result.Count);
            Assert.Equal(0, subscriber2Result.Count);
        }