Exemple #1
0
        public async Task TraceErrorTest()
        {
            // setup
            var countCall = 0;

            string[] traceReceived = {};
            var      config        = new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAcces
            };

            using (var ts = new TraceSourceSync(config))
            {
                ts.OnTracesSent += o =>
                {
                    Interlocked.Increment(ref countCall);
                    // remplace sans lock car l'appel doit être unique et countCall sers de vérif
                    traceReceived = o.Select(t => t.Message).ToArray();
                };

                //Test
                Trace.TraceError("TraceError");
                Trace.TraceError("format {0} - {1}", "param1", "param2");
                await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }
            //Verify
            Assert.AreEqual(1, countCall);
            Assert.IsTrue(traceReceived.Contains("TraceError"));
            Assert.IsTrue(traceReceived.Contains(@"format {0} - {1}")); // pas de formatage par défaut => on laisse les arguments à coté
        }
        public async Task InterceptionTest_Property_With_custom_Option()
        {
            //Configuration
            var count = 0;

            using (var tracer = new TraceSourceSync(new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer, PageSize = 2
            }))
            {
                _container.RegisterInstance(tracer);

                _container.RegisterInterceptor <InterceptorTraceInjectTracer>(LifeTime.Singleton);
                _container.Register <IClassVoidTest, ClassVoidTest>(LifeTime.Singleton);

                _container.Intercept <IClassVoidTest>().With <InterceptorTraceInjectTracer>(InterceptionHookOption.MethodsOnly);

                tracer.OnTracesSent += trace =>
                {
                    Interlocked.Increment(ref count);
                };

                //Test
                var instance = _container.Resolve <IClassVoidTest>();
                instance.Prop = 45;    // setter
                var _ = instance.Prop; // getter

                //Verify
                await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            // aucun retour
            Assert.AreEqual(0, count);
        }
Exemple #3
0
        public async Task TraceDataTest_null()
        {
            // setup
            var countCall = 0;

            string[] traceReceived = { };
            var      config        = new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer
            };

            using (var ts = new TraceSourceSync(config))
            {
                ts.OnTracesSent += o =>
                {
                    Interlocked.Increment(ref countCall);
                    // remplace sans lock car l'appel doit être unique et countCall sers de vérif
                    traceReceived = o.Select(t => t.Message).ToArray();
                };

                //Test
                ts.TraceData(TraceEventType.Information, 32, null);
                await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }
            //Verify
            Assert.AreEqual(1, countCall);
            Assert.AreEqual(1, traceReceived.Length, $"array == {string.Join(", ", traceReceived)}");
            Assert.IsTrue(traceReceived.Contains("NULL_DATA"), $"array == {string.Join(", ", traceReceived)}");
        }
Exemple #4
0
        public async Task WriteIfTest()
        {
            // setup
            var countCall = 0;

            string[] traceReceived = { };
            var      config        = new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer
            };

            using (var ts = new TraceSourceSync(config))
            {
                ts.OnTracesSent += o =>
                {
                    Interlocked.Increment(ref countCall);
                    // remplace sans lock car l'appel doit être unique et countCall sers de vérif
                    traceReceived = o.Select(t => t.Message).ToArray();
                };

                //Test
                Trace.WriteIf(true, "WriteIf true");
                Trace.WriteIf(false, "WriteIf false");
                await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }
            //Verify
            Assert.AreEqual(1, countCall);
            Assert.AreEqual(1, traceReceived.Length);
            Assert.IsTrue(traceReceived.Contains("WriteIf true"));
        }
        public async Task TimerTraceTesttV1()
        {
            // setup
            int pagesReceived = 0;
            // la date cible est maintenant + 0.5 seconde
            var timeTarget = DateTime.UtcNow.AddMilliseconds(500);

            // on décide d'envoyer une page toute les 100 ms, sur une demie seconde, on devrait avoir 5 pages environ
            var config = new TraceConfig
            {
                PageSize            = 1000,
                MaximumTimeForFlush = TimeSpan.FromMilliseconds(100)
            };

            using (var traceListener = new TraceSourceSync(config))
            {
                // quand on reçoit une page, on incrémente le compteur
                traceListener.OnTracesSent += o => Interlocked.Increment(ref pagesReceived);

                // tant que la seconde n'est pas atteinte, on trace des messages (avec une attente)
                while (DateTime.UtcNow < timeTarget)
                {
                    Trace.TraceInformation("Method1");
                    await Task.Delay(50);
                }

                //Test
                await traceListener.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            //Verify que l'on a 6 pages ou moins (les 5 de flush + l'éventuelle dernière)
            // PK moins? => car selon le Task.Delay, il se peut que l'on n'écrive aucune trace dans un cycle de 200ms et donc, que l'on envoie rien
            Assert.IsTrue(pagesReceived > 0 && pagesReceived <= 6, $"INVALID number of pages received = {pagesReceived}");
        }
        public async Task TimeWatchInterceptorTestV2()
        {
            //Configuration
            var count = 0;

            TraceData[] traceReceived = { };

            _container.RegisterInterceptor(PredefinedInterceptors.TimeWatchInterceptor, LifeTime.Singleton);
            _container.Intercept <IClassVoidTest>().With(PredefinedInterceptors.TimeWatchInterceptor);

            var instance = _container.Resolve <IClassVoidTest>();

            using (var tracer = new TraceSourceSync(_traceConfig))
            {
                tracer.OnTracesSent += trace =>
                {
                    traceReceived = trace;
                    Interlocked.Increment(ref count);
                };

                //Test
                await instance.MethodCallWaitAsync(TimeSpan.FromMilliseconds(50)).ConfigureAwait(false);

                //Verify
                await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            // un seul retour
            Assert.AreEqual(1, count);
            Assert.AreEqual(1, traceReceived.Length);
            Assert.IsTrue(traceReceived[0].Message.StartsWith("TIME_IClassVoidTest.MethodCallWaitAsync = "));
        }
Exemple #7
0
        public async Task TraceEventTest()
        {
            // setup
            var countCall = 0;

            string[] traceReceived = { };
            var      config        = new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer
            };

            using (var ts = new TraceSourceSync(config))
            {
                ts.OnTracesSent += o =>
                {
                    Interlocked.Increment(ref countCall);
                    // remplace sans lock car l'appel doit être unique et countCall sers de vérif
                    traceReceived = o.Select(t => t.Message).ToArray();
                };

                //Test
                ts.TraceEvent(TraceEventType.Information, 56);
                ts.TraceEvent(TraceEventType.Information, 56, "message");
                ts.TraceEvent(TraceEventType.Information, 56, "format {0} - {1}", "param1", "param2");
                await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            //Verify
            Assert.AreEqual(1, countCall);
            Assert.AreEqual(3, traceReceived.Length);
            Assert.IsTrue(traceReceived.Contains("TraceEvent id:56"), $"array contains: {string.Join(Environment.NewLine, traceReceived)}");
            Assert.IsTrue(traceReceived.Contains("message"), $"array contains: {string.Join(Environment.NewLine, traceReceived)}");
            Assert.IsTrue(traceReceived.Contains("format {0} - {1}"), $"array contains: {string.Join(Environment.NewLine, traceReceived)}");
        }
Exemple #8
0
        public async Task MethodInterceptorTest_PerfV11()
        {
            //Configuration
            var count = 0;

            TraceData[] traceReceived = { };
            const int   upper         = 1_000;

            _container.RegisterInterceptor(PredefinedInterceptors.MethodTraceInterceptor, LifeTime.Singleton);
            _container.Intercept <IClassVoidTest>().With(PredefinedInterceptors.MethodTraceInterceptor);

            var instance = _container.Resolve <IClassVoidTest>();

            _traceConfig.TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndClearAll;
            using (var tracer = new TraceSourceSync(_traceConfig))
            {
                tracer.OnTracesSent += trace =>
                {
                    traceReceived = trace;
                    Interlocked.Increment(ref count);
                };

                //Test
                for (var i = 0; i < upper; i++)
                {
                    var _ = await instance.MethodCallForPerfInt();
                }

                //Verify
                await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }
            // un seul retour (avec les deux traces des intercepteurs)
            Assert.AreEqual(1, count);
            Assert.AreEqual(2 * upper, traceReceived.Length);
        }
Exemple #9
0
        public async Task MethodInterceptorTestV1()
        {
            //Configuration
            var count = 0;

            TraceData[] traceReceived = { };

            _container.RegisterInterceptor(PredefinedInterceptors.MethodTraceInterceptor, LifeTime.Singleton);
            _container.Intercept <IClassVoidTest>().With(PredefinedInterceptors.MethodTraceInterceptor);

            var instance = _container.Resolve <IClassVoidTest>();

            using (var tracer = new TraceSourceSync(_traceConfig))
            {
                tracer.OnTracesSent += trace =>
                {
                    traceReceived = trace;
                    Interlocked.Increment(ref count);
                };

                //Test
                instance.MethodCall();

                //Verify
                await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            // un seul retour (avec les deux traces des intercepteurs)
            Assert.AreEqual(1, count);
            Assert.AreEqual(2, traceReceived.Length);
            Assert.AreEqual("START_IClassVoidTest.MethodCall()", traceReceived[0].Message);
            Assert.AreEqual("STOP_IClassVoidTest.MethodCall", traceReceived[1].Message);
        }
Exemple #10
0
 public async Task DefaultTraceLevelCheck()
 {
     // setup
     using (var ts = new TraceSourceSync())
     {
         //Verify
         Assert.AreEqual(SourceLevels.Information, ts.TraceLevel);
         await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
     }
 }
Exemple #11
0
        public async Task CleanListenersTestV4()
        {
            // setup
            var config = new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndClearAll
            };

            using (var ts = new TraceSourceSync(config))
            {
                //Test
                await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }
            //Verify
            // vérification que la liste des Listeners statiques est vide:
            Assert.AreEqual(0, Trace.Listeners.Count);
        }
Exemple #12
0
        public async Task CleanListenersTestV3()
        {
            // setup
            var config = new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer
            };

            using (var ts = new TraceSourceSync(config))
            {
                //Test
                await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }
            //Verify
            // vérification que l'on ne pollue pas les Listeners statiques MAIS que l'on a retiré le listener par défaut:
            Assert.IsTrue(Trace.Listeners.Cast <TraceListener>().All(o => o.Name != "Default"));
            Assert.IsTrue(Trace.Listeners.Cast <TraceListener>().All(o => o.Name != @"MainTracerSync"));
        }
Exemple #13
0
        public async Task CleanListenersTestV2()
        {
            // setup
            var listenerCount = Trace.Listeners.Count;
            var config        = new TraceConfig {
                TraceBehavior = TraceStaticBehavior.DoNothing
            };

            using (var ts = new TraceSourceSync(config))
            {
                //Test
                await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }
            //Verify
            // vérification que l'on ne pollue pas les Listeners statiques:
            Assert.IsTrue(Trace.Listeners.Cast <TraceListener>().All(o => o.Name != @"MainTracerSync"));
            Assert.AreEqual(listenerCount, Trace.Listeners.Count);
        }
Exemple #14
0
        public async Task Trace_Performance_Trace_Data()
        {
            // setup
            const int upper    = 100_000;
            var       count    = 0;
            var       nbTraces = 0;
            var       key      = new object();

            var config = new TraceConfig
            {
                TraceBehavior       = TraceStaticBehavior.AddListenerToStaticAccessAndClearAll,
                PageSize            = upper,
                MaximumTimeForFlush = TimeSpan.FromSeconds(5)
            };
            Stopwatch watch;

            using (var ts = new TraceSourceSync(config))
            {
                ts.OnTracesSent += t =>
                {
                    lock (key)
                    {
                        count++;
                        nbTraces += t.Length;
                    }
                };

                watch = Stopwatch.StartNew();
                ////Test
                for (var i = 0; i < upper; i++)
                {
                    Trace.Write("error test trace TU");
                }
                watch.Stop();

                //Verify
                await ts.FlushAndCompleteAddingAsync();
            }

            // nombre de page renvoyé == 1 seule
            Assert.AreEqual(1, count);

            Assert.AreEqual(upper, nbTraces);
        }
Exemple #15
0
        public async Task MethodInterceptorTestPerformanceV1()
        {
            //Configuration
            var       count = 0;
            const int upper = 100_000;

            TraceData[] traceReceived = { };

            _container.RegisterInterceptor(PredefinedInterceptors.MethodTraceInterceptor, LifeTime.Singleton);
            _container.Intercept <IClassVoidTest>().With(PredefinedInterceptors.MethodTraceInterceptor);

            var instance = _container.Resolve <IClassVoidTest>();

            // supprime tt les traceurs (influent en debug)
            _traceConfig.TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndClearAll;

            using (var tracer = new TraceSourceSync(_traceConfig))
            {
                tracer.OnTracesSent += trace =>
                {
                    traceReceived = trace;
                    Interlocked.Increment(ref count);
                };

                //Test
                var watch = Stopwatch.StartNew();
                for (var i = 0; i < upper; i++)
                {
                    instance.MethodCall();
                }
                watch.Stop();

                //Verify
                await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false);

                Assert.IsTrue(watch.Elapsed < TimeSpan.FromSeconds(1), $"TROP LENT: Le temps passé est de {watch.ElapsedMilliseconds} ms");
            }

            // un seul retour (avec les deux traces des intercepteurs)
            Assert.AreEqual(1, count);
            Assert.AreEqual(2 * upper, traceReceived.Length);
        }
        public async Task InterceptionTest_Attribute()
        {
            //Configuration
            int count;

            TraceData[] traceReceived;
            using (var tracer = new TraceSourceSync(
                       new TraceConfig
            {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer,
                PageSize = 10000
            }))
            {
                _container.RegisterInstance(tracer);

                _container.RegisterInterceptor <InterceptorTraceInjectTracer>(LifeTime.Singleton);
                _container.Register <IClassVoidAttributeTest, ClassVoidAttributeTest>(LifeTime.Singleton);

                _container.Intercept <IClassVoidAttributeTest>().With <InterceptorTraceInjectTracer>(InterceptionHookOption.InterceptionAttributOnly);

                count                = 0;
                traceReceived        = null;
                tracer.OnTracesSent += trace =>
                {
                    traceReceived = trace;
                    Interlocked.Increment(ref count);
                };

                //Test
                var instance = _container.Resolve <IClassVoidAttributeTest>();
                instance.Prop = 45;    // setter
                var _ = instance.Prop; // getter
                instance.MethodCall();

                //Verify
                await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            // un seul retour
            Assert.AreEqual(1, count);
            Assert.AreEqual(3, traceReceived.Length);
        }
Exemple #17
0
        public async Task FlushAndCompleteAddingAsync_EmptyPage_Test()
        {
            // setup
            var count = 0;

            using (var ts = new TraceSourceSync(new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer
            }))
            {
                ts.OnTracesSent += t =>
                {
                    Interlocked.Increment(ref count);
                };

                //Verify
                await ts.FlushAndCompleteAddingAsync();
            }

            Assert.AreEqual(0, count); // pas de réception de page car page vide
        }
        public async Task TestSwitchLevelInformationV3()
        {
            // setup
            var received = false;
            var config   = new TraceConfig
            {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer,
                TraceLevel    = SourceLevels.Information
            };

            using (var ts = new TraceSourceSync(config))
            {
                ts.OnTracesSent += t => { received = true; };

                Trace.TraceError("test");
                await ts.FlushAndCompleteAddingAsync();
            }

            //Verify
            Assert.IsTrue(received);
        }
Exemple #19
0
        public async Task DoNothing()
        {
            // setup
            var count = 0;

            using (var ts = new TraceSourceSync(new TraceConfig {
                TraceBehavior = TraceStaticBehavior.DoNothing
            }))
            {
                ts.OnTracesSent += o =>
                {
                    Interlocked.Increment(ref count);
                };

                //Test
                Trace.TraceInformation("Method1");
                await ts.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            //Verify
            Assert.AreEqual(0, count);
        }
Exemple #20
0
        public async Task TraceAfterDispose()
        {
            // setup
            var count = 0;

            using (var ts = new TraceSourceSync(new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer
            }))
            {
                ts.OnTracesSent += t =>
                {
                    Interlocked.Increment(ref count);
                };

                //Verify
                await ts.FlushAndCompleteAddingAsync();
            }

            Trace.TraceError("error test trace TU");

            Assert.AreEqual(0, count); // pas de réception
        }
        public async Task TestSwitchChangeLevelV1()
        {
            // setup
            var key          = new object();
            var listMessages = new List <string>();

            var config = new TraceConfig
            {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer,
                TraceLevel    = SourceLevels.Error
            };

            using (var ts = new TraceSourceSync(config))
            {
                ts.OnTracesSent += t =>
                {
                    lock (key)
                    {
                        listMessages.AddRange(t.Select(o => o.Message));
                    }
                };

                Trace.TraceInformation("test1"); // pas tracé

                ts.TraceLevel = SourceLevels.Information;
                Trace.TraceInformation("test2"); // tracé

                ts.TraceLevel = SourceLevels.Warning;
                Trace.TraceInformation("test3"); // pas tracé

                await ts.FlushAndCompleteAddingAsync();
            }

            //Verify
            Assert.AreEqual(1, listMessages.Count);
            Assert.IsTrue(listMessages.Contains("test2"));
            Assert.IsFalse(listMessages.Contains("test1"));
            Assert.IsFalse(listMessages.Contains("test3"));
        }
        public async Task InterceptionTestV2()
        {
            //Configuration
            var count = 0;

            TraceData[] traceReceived = { };

            using (var tracer = new TraceSourceSync(new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer, PageSize = 2
            }))
            {
                _container.RegisterInstance(tracer);

                _container.RegisterInterceptor <InterceptorTraceInjectTracer>(LifeTime.Singleton);
                _container.RegisterInterceptor <InterceptorTraceStatic>(LifeTime.Singleton);
                _container.Register <IClassVoidTest, ClassVoidTest>(LifeTime.Singleton);

                _container.InterceptAny(type => type.Name.Contains("lassVoidT")).With <InterceptorTraceInjectTracer>();
                _container.InterceptAny(type => type.Name.Contains("lassVoidT")).With <InterceptorTraceStatic>();

                tracer.OnTracesSent += trace =>
                {
                    traceReceived = trace;
                    Interlocked.Increment(ref count);
                };

                //Test
                var instance = _container.Resolve <IClassVoidTest>();
                instance.MethodCall();

                //Verify
                await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            // un seul retour (avec les deux traces des intercepteurs)
            Assert.AreEqual(1, count);
            Assert.IsNotNull(traceReceived.Single(o => o.Message == "IClassVoidTest_InterceptorTraceInjectTracer"));
            Assert.IsNotNull(traceReceived.Single(o => o.Message == "IClassVoidTest_InterceptorTraceStatic"));
        }
        public async Task InterceptionTest_Property()
        {
            //Configuration
            var count = 0;

            TraceData[] traceReceived = { };
            using (var tracer = new TraceSourceSync(new TraceConfig {
                TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer, PageSize = 2
            }))
            {
                _container.RegisterInstance(tracer);

                _container.RegisterInterceptor <InterceptorTraceInjectTracer>(LifeTime.Singleton);
                _container.Register <IClassVoidTest, ClassVoidTest>(LifeTime.Singleton);

                _container.InterceptAny(type => type.Name.Contains("lassVoidT")).With <InterceptorTraceInjectTracer>();

                tracer.OnTracesSent += trace =>
                {
                    traceReceived = trace;
                    Interlocked.Increment(ref count);
                };

                //Test
                var instance = _container.Resolve <IClassVoidTest>();
                instance.Prop = 45;    // setter
                instance.Prop = 45;    // setter x2
                var _ = instance.Prop; // getter = pas tracé

                //Verify
                await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false);
            }

            // un seul retour
            Assert.AreEqual(1, count);
            Assert.AreEqual(2, traceReceived.Length); //*2 car deux set et le get n'est pas tracé
        }