ServiceStack/ServiceStack — pull request #334 by softwx (2012-11-17 17:19:49+00:00)
Title: MemoryCacheClient changed to use UTC DateTime internally

The public methods that take DateTimes will accept either Utc Kind or Local Kind DateTimes, and will only convert to universal time if the given DateTime is not already Utc.

Also wrote the following unit tests, but not sure which project should hold them, or whether you even want to include them since they use Thread.Sleep with small times to test expiration handling.

[TestFixture]
public class MemoryCacheClientTests
{
MemoryCacheClient cache;

[TestFixtureSetUp]
public void TestFixtureSetUp()
{
cache = new MemoryCacheClient();
}

[TestFixtureTearDown]
public void TestFixtureTearDown()
{
cache.Dispose();
cache = null;
}

[Test]
public void Get_before_Add_local_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Add(key, value, DateTime.Now.AddMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Add_local_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Add(key, value, DateTime.Now.AddMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}

[Test]
public void Get_before_Add_utc_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Add(key, value, DateTime.UtcNow.AddMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Add_utc_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Add(key, value, DateTime.UtcNow.AddMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}

[Test]
public void Get_before_Add_TimeSpan_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Add(key, value, TimeSpan.FromMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Add_TimeSpan_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Add(key, value, TimeSpan.FromMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}

[Test]
public void Get_before_Set_local_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value, DateTime.Now.AddMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Set_local_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value, DateTime.Now.AddMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}

[Test]
public void Get_before_Set_utc_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value, DateTime.UtcNow.AddMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Set_utc_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value, DateTime.UtcNow.AddMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}

[Test]
public void Get_before_Set_TimeSpan_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value, TimeSpan.FromMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Set_TimeSpan_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value, TimeSpan.FromMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}

[Test]
public void Get_before_Replace_local_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value);
cache.Replace(key, value, DateTime.Now.AddMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Replace_local_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value);
cache.Replace(key, value, DateTime.Now.AddMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}

[Test]
public void Get_before_Replace_utc_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value);
cache.Replace(key, value, DateTime.UtcNow.AddMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Replace_utc_DateTime_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value);
cache.Replace(key, value, DateTime.UtcNow.AddMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}

[Test]
public void Get_before_Replace_TimeSpan_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value);
cache.Replace(key, value, TimeSpan.FromMilliseconds(200));
var retVal = cache.Get(key);
Assert.That(retVal, Is.SameAs(value));
}

[Test]
public void Get_after_Replace_TimeSpan_expires()
{
cache.FlushAll();
string key = "a";
string value = "aValue";
cache.Set(key, value);
cache.Replace(key, value, TimeSpan.FromMilliseconds(200));
System.Threading.Thread.Sleep(250);
var retVal = cache.Get(key);
Assert.That(retVal, Is.Null);
}
}