0
|
1 using System;
|
|
2 using System.Security.Principal;
|
|
3 using System.Threading;
|
|
4
|
|
5 using BLToolkit.Aspects;
|
|
6 using BLToolkit.Reflection;
|
|
7
|
|
8 using NUnit.Framework;
|
|
9
|
|
10 namespace Aspects
|
|
11 {
|
|
12 [TestFixture]
|
|
13 public class CounterAspectTest
|
|
14 {
|
|
15 [Log]
|
|
16 public abstract class TestClass
|
|
17 {
|
|
18 [Counter]
|
|
19 public virtual void Test()
|
|
20 {
|
|
21 }
|
|
22
|
|
23 [Counter]
|
|
24 public virtual void LongTest()
|
|
25 {
|
|
26 Thread.Sleep(100);
|
|
27 }
|
|
28 }
|
|
29
|
|
30 [Test]
|
|
31 public void Test()
|
|
32 {
|
|
33 TestClass t = (TestClass)TypeAccessor.CreateInstance(typeof(TestClass));
|
|
34
|
|
35 for (int i = 0; i < 10; i++)
|
|
36 t.Test();
|
|
37
|
|
38 MethodCallCounter counter = CounterAspect.GetCounter(typeof(TestClass).GetMethod("Test"));
|
|
39
|
|
40 Assert.AreEqual(10, counter.TotalCount);
|
|
41
|
|
42 Console.WriteLine(counter.TotalTime);
|
|
43
|
|
44 new Thread(new ThreadStart(t.LongTest)).Start();
|
|
45 Thread.Sleep(20);
|
|
46
|
|
47 lock (CounterAspect.Counters.SyncRoot) foreach (MethodCallCounter c in CounterAspect.Counters)
|
|
48 {
|
|
49 Console.WriteLine("{0}.{1,-10} | {2,2} | {3,2} | {4}",
|
|
50 c.MethodInfo.DeclaringType.Name,
|
|
51 c.MethodInfo.Name,
|
|
52 c.TotalCount,
|
|
53 c.CurrentCalls.Count,
|
|
54 c.TotalTime);
|
|
55
|
|
56 lock (c.CurrentCalls.SyncRoot) for (int i = 0; i < c.CurrentCalls.Count; i++)
|
|
57 {
|
|
58 InterceptCallInfo ci = (InterceptCallInfo)c.CurrentCalls[i];
|
|
59 IPrincipal pr = ci.CurrentPrincipal;
|
|
60
|
|
61 Console.WriteLine("{0,15} | {1}",
|
|
62 pr == null? "***" : pr.Identity.Name,
|
|
63 DateTime.Now - ci.BeginCallTime);
|
|
64 }
|
|
65 }
|
|
66 }
|
|
67
|
|
68 public abstract class TestClass2
|
|
69 {
|
|
70 [Counter]
|
|
71 public virtual void Test()
|
|
72 {
|
|
73 }
|
|
74 }
|
|
75
|
|
76 [Test]
|
|
77 public void Test2()
|
|
78 {
|
|
79 // custom create counter delegate returns null
|
|
80 CounterAspect.CreateCounter = mi => null;
|
|
81
|
|
82 var t = (TestClass2)TypeAccessor.CreateInstance(typeof(TestClass2));
|
|
83
|
|
84 // interceptor should fallback to default counter implementation
|
|
85 t.Test();
|
|
86 }
|
|
87
|
|
88 }
|
|
89 }
|