OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
CacheTests.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using NUnit.Framework;
30 using OpenMetaverse;
31 using OpenSim.Tests.Common;
32 
33 namespace OpenSim.Framework.Tests
34 {
35  [TestFixture]
36  public class CacheTests : OpenSimTestCase
37  {
38  private Cache cache;
39  private UUID cacheItemUUID;
40  [SetUp]
41  public void Build()
42  {
43  cache = new Cache();
44  cache = new Cache(CacheMedium.Memory,CacheStrategy.Aggressive,CacheFlags.AllowUpdate);
45  cacheItemUUID = UUID.Random();
46  MemoryCacheItem cachedItem = new MemoryCacheItem(cacheItemUUID.ToString(),DateTime.Now + TimeSpan.FromDays(1));
47  byte[] foo = new byte[1];
48  foo[0] = 255;
49  cachedItem.Store(foo);
50  cache.Store(cacheItemUUID.ToString(), cachedItem);
51  }
52  [Test]
53  public void TestRetreive()
54  {
55  CacheItemBase citem = (CacheItemBase)cache.Get(cacheItemUUID.ToString());
56  byte[] data = (byte[]) citem.Retrieve();
57  Assert.That(data.Length == 1, "Cached Item should have one byte element");
58  Assert.That(data[0] == 255, "Cached Item element should be 255");
59  }
60 
61  [Test]
62  public void TestNotInCache()
63  {
64  UUID randomNotIn = UUID.Random();
65  while (randomNotIn == cacheItemUUID)
66  {
67  randomNotIn = UUID.Random();
68  }
69  object citem = cache.Get(randomNotIn.ToString());
70  Assert.That(citem == null, "Item should not be in Cache");
71  }
72 
73 
74  [Test]
75  public void ExpireItemManually()
76  {
77  UUID ImmediateExpiryUUID = UUID.Random();
78  MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), TimeSpan.FromDays(1));
79  byte[] foo = new byte[1];
80  foo[0] = 1;
81  cachedItem.Store(foo);
82  cache.Store(cacheItemUUID.ToString(), cachedItem);
83  cache.Invalidate(cacheItemUUID.ToString());
84  cache.Get(cacheItemUUID.ToString());
85  object citem = cache.Get(cacheItemUUID.ToString());
86  Assert.That(citem == null, "Item should not be in Cache because we manually invalidated it");
87  }
88 
89  [Test]
90  public void ClearCacheTest()
91  {
92  UUID ImmediateExpiryUUID = UUID.Random();
93  MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), DateTime.Now - TimeSpan.FromDays(1));
94  byte[] foo = new byte[1];
95  foo[0] = 1;
96  cachedItem.Store(foo);
97  cache.Store(cacheItemUUID.ToString(), cachedItem);
98  cache.Clear();
99 
100  object citem = cache.Get(cacheItemUUID.ToString());
101  Assert.That(citem == null, "Item should not be in Cache because we manually invalidated it");
102  }
103 
104  [Test]
105  public void CacheItemMundane()
106  {
107  UUID Random1 = UUID.Random();
108  UUID Random2 = UUID.Random();
109  byte[] data = new byte[0];
110  CacheItemBase cb1 = new CacheItemBase(Random1.ToString(), DateTime.Now + TimeSpan.FromDays(1));
111  CacheItemBase cb2 = new CacheItemBase(Random2.ToString(), DateTime.Now + TimeSpan.FromDays(1));
112  CacheItemBase cb3 = new CacheItemBase(Random1.ToString(), DateTime.Now + TimeSpan.FromDays(1));
113 
114  cb1.Store(data);
115 
116  Assert.That(cb1.Equals(cb3), "cb1 should equal cb3, their uuids are the same");
117  Assert.That(!cb2.Equals(cb1), "cb2 should not equal cb1, their uuids are NOT the same");
118  Assert.That(cb1.IsLocked() == false, "CacheItemBase default is false");
119  Assert.That(cb1.Retrieve() == null, "Virtual Retrieve method should return null");
120 
121 
122  }
123 
124  }
125 }
virtual Object Retrieve()
Definition: Cache.cs:80