|
| 1 | +using System; |
| 2 | +using NUnit.Framework; |
| 3 | + |
| 4 | +namespace NHibernate.Cache { |
| 5 | + |
| 6 | + [TestFixture] |
| 7 | + public class ICacheTest { |
| 8 | + |
| 9 | + [Test] |
| 10 | + public void TestSimpleCache() { |
| 11 | + DoTestCache( new SimpleCache() ); |
| 12 | + } |
| 13 | + |
| 14 | + public void DoTestCache(ICache cache) { |
| 15 | + long longBefore = Timestamper.Next(); |
| 16 | + |
| 17 | + System.Threading.Thread.Sleep(15); |
| 18 | + |
| 19 | + long before = Timestamper.Next(); |
| 20 | + |
| 21 | + System.Threading.Thread.Sleep(15); |
| 22 | + |
| 23 | + ICacheConcurrencyStrategy ccs = new ReadWriteCache(cache); |
| 24 | + |
| 25 | + // cache something |
| 26 | + |
| 27 | + Assertion.Assert( ccs.Put("foo", "foo", before) ); |
| 28 | + |
| 29 | + System.Threading.Thread.Sleep(15); |
| 30 | + |
| 31 | + long after = Timestamper.Next(); |
| 32 | + |
| 33 | + Assertion.AssertNull( ccs.Get("foo", longBefore) ); |
| 34 | + Assertion.AssertEquals( "foo", ccs.Get("foo", after) ); |
| 35 | + Assertion.Assert( !ccs.Put("foo", "foo", before) ); |
| 36 | + |
| 37 | + // update it; |
| 38 | + |
| 39 | + ccs.Lock("foo"); |
| 40 | + |
| 41 | + Assertion.AssertNull( ccs.Get("foo", after) ); |
| 42 | + Assertion.AssertNull( ccs.Get("foo", longBefore) ); |
| 43 | + Assertion.Assert( !ccs.Put("foo", "foo", before) ); |
| 44 | + |
| 45 | + System.Threading.Thread.Sleep(15); |
| 46 | + |
| 47 | + long whileLocked = Timestamper.Next(); |
| 48 | + |
| 49 | + Assertion.Assert( !ccs.Put("foo", "foo", whileLocked) ); |
| 50 | + |
| 51 | + System.Threading.Thread.Sleep(15); |
| 52 | + |
| 53 | + ccs.Release("foo"); |
| 54 | + |
| 55 | + Assertion.AssertNull( ccs.Get("foo", after) ); |
| 56 | + Assertion.AssertNull( ccs.Get("foo", longBefore) ); |
| 57 | + Assertion.Assert( !ccs.Put("foo", "bar", whileLocked) ); |
| 58 | + Assertion.Assert( !ccs.Put("foo", "bar", after) ); |
| 59 | + |
| 60 | + System.Threading.Thread.Sleep(15); |
| 61 | + |
| 62 | + long longAfter = Timestamper.Next(); |
| 63 | + |
| 64 | + Assertion.Assert( ccs.Put("foo", "baz", longAfter) ); |
| 65 | + Assertion.AssertNull( ccs.Get("foo", after) ); |
| 66 | + Assertion.AssertNull( ccs.Get("foo", whileLocked) ); |
| 67 | + |
| 68 | + System.Threading.Thread.Sleep(15); |
| 69 | + |
| 70 | + long longLongAfter = Timestamper.Next(); |
| 71 | + |
| 72 | + Assertion.AssertEquals("baz", ccs.Get("foo", longLongAfter) ); |
| 73 | + |
| 74 | + // update it again, with multiple locks |
| 75 | + |
| 76 | + ccs.Lock("foo"); |
| 77 | + ccs.Lock("foo"); |
| 78 | + |
| 79 | + Assertion.AssertNull( ccs.Get("foo", longLongAfter) ); |
| 80 | + |
| 81 | + System.Threading.Thread.Sleep(15); |
| 82 | + |
| 83 | + whileLocked = Timestamper.Next(); |
| 84 | + |
| 85 | + Assertion.Assert( !ccs.Put("foo", "foo", whileLocked) ); |
| 86 | + |
| 87 | + System.Threading.Thread.Sleep(15); |
| 88 | + |
| 89 | + ccs.Release("foo"); |
| 90 | + |
| 91 | + System.Threading.Thread.Sleep(15); |
| 92 | + |
| 93 | + long betweenReleases = Timestamper.Next(); |
| 94 | + |
| 95 | + Assertion.Assert( !ccs.Put("foo", "bar", betweenReleases) ); |
| 96 | + Assertion.AssertNull( ccs.Get("foo", betweenReleases) ); |
| 97 | + |
| 98 | + System.Threading.Thread.Sleep(15); |
| 99 | + |
| 100 | + ccs.Release("foo"); |
| 101 | + |
| 102 | + Assertion.Assert( !ccs.Put("foo", "bar", whileLocked) ); |
| 103 | + |
| 104 | + System.Threading.Thread.Sleep(15); |
| 105 | + |
| 106 | + longAfter = Timestamper.Next(); |
| 107 | + |
| 108 | + Assertion.Assert( ccs.Put("foo", "baz", longAfter) ); |
| 109 | + Assertion.AssertNull( ccs.Get("foo", whileLocked) ); |
| 110 | + |
| 111 | + System.Threading.Thread.Sleep(15); |
| 112 | + |
| 113 | + longLongAfter = Timestamper.Next(); |
| 114 | + |
| 115 | + Assertion.AssertEquals("baz", ccs.Get("foo", longLongAfter) ); |
| 116 | + |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments