Mercurial > pub > bltoolkit
comparison HowTo/Aspects/CacheAspect.cs @ 0:f990fcb411a9
Копия текущей версии из github
| author | cin |
|---|---|
| date | Thu, 27 Mar 2014 21:46:09 +0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f990fcb411a9 |
|---|---|
| 1 using System; | |
| 2 using System.Reflection; | |
| 3 | |
| 4 using NUnit.Framework; | |
| 5 | |
| 6 using BLToolkit.Aspects; | |
| 7 using BLToolkit.Reflection; | |
| 8 | |
| 9 namespace HowTo.Aspects | |
| 10 { | |
| 11 public /*[a]*/abstract/*[/a]*/ class TestClass | |
| 12 { | |
| 13 public static int Value; | |
| 14 | |
| 15 // This is a method we will cache. Cached return value depends on input parameters. | |
| 16 // We will change the 'Value' field outside of the class and see how it affects the result. | |
| 17 // | |
| 18 [/*[a]*/Cache/*[/a]*/(MaxCacheTime=500, IsWeak=false)] | |
| 19 public /*[a]*/virtual/*[/a]*/ int CachedMethod(int p1, int p2) | |
| 20 { | |
| 21 return Value; | |
| 22 } | |
| 23 | |
| 24 public static TestClass CreateInstance() | |
| 25 { | |
| 26 // Use TypeAccessor to create an instance of an abstract class. | |
| 27 // | |
| 28 return /*[a]*/TypeAccessor/*[/a]*/<TestClass>.CreateInstance(); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 [TestFixture] | |
| 33 public class CacheAspectTest | |
| 34 { | |
| 35 [Test] | |
| 36 public void Test1() | |
| 37 { | |
| 38 TestClass tc = TestClass.CreateInstance(); | |
| 39 | |
| 40 DateTime begin = DateTime.Now; | |
| 41 | |
| 42 // Initial setup for the test static variable. | |
| 43 // | |
| 44 TestClass.Value = 777; | |
| 45 | |
| 46 while (tc.CachedMethod(2, 2) == 777) | |
| 47 { | |
| 48 // This change will not affect the Test method return value for 500 ms. | |
| 49 // | |
| 50 TestClass.Value++; | |
| 51 } | |
| 52 | |
| 53 double totalMilliseconds = (DateTime.Now - begin).TotalMilliseconds; | |
| 54 | |
| 55 Assert.GreaterOrEqual(totalMilliseconds, 500); | |
| 56 } | |
| 57 | |
| 58 [Test] | |
| 59 public void Test2() | |
| 60 { | |
| 61 TestClass tc = TestClass.CreateInstance(); | |
| 62 | |
| 63 // Return value depends on parameter values. | |
| 64 // | |
| 65 TestClass.Value = /*[a]*/1/*[/a]*/; Assert.AreEqual(/*[a]*/1/*[/a]*/, tc.CachedMethod(1, 1)); | |
| 66 TestClass.Value = /*[a]*/2/*[/a]*/; Assert.AreEqual(/*[a]*/1/*[/a]*/, tc.CachedMethod(1, 1)); // no change | |
| 67 TestClass.Value = /*[a]*/3/*[/a]*/; Assert.AreEqual(/*[a]*/3/*[/a]*/, tc.CachedMethod(2, 1)); | |
| 68 | |
| 69 // However we can clear cache manually. | |
| 70 // For particular method: | |
| 71 // | |
| 72 CacheAspect.ClearCache(typeof(TestClass), "CachedMethod", typeof(int), typeof(int)); | |
| 73 TestClass.Value = /*[a]*/4/*[/a]*/; Assert.AreEqual(/*[a]*/4/*[/a]*/, tc.CachedMethod(2, 1)); | |
| 74 | |
| 75 // By MethodInfo: | |
| 76 // | |
| 77 MethodInfo methodInfo = tc.GetType().GetMethod("CachedMethod", new Type[] { typeof(int), typeof(int) }); | |
| 78 CacheAspect.ClearCache(methodInfo); | |
| 79 TestClass.Value = /*[a]*/5/*[/a]*/; Assert.AreEqual(/*[a]*/5/*[/a]*/, tc.CachedMethod(2, 1)); | |
| 80 | |
| 81 // For the all cached methods. | |
| 82 // | |
| 83 CacheAspect.ClearCache(); | |
| 84 TestClass.Value = /*[a]*/6/*[/a]*/; Assert.AreEqual(/*[a]*/6/*[/a]*/, tc.CachedMethod(2, 1)); | |
| 85 } | |
| 86 } | |
| 87 } |
