Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

MemoryCacheClient changed to use UTC DateTime internally #334

Merged
merged 2 commits into from Nov 17, 2012

Conversation

Projects
None yet
2 participants
Contributor

softwx commented Nov 17, 2012

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);
    }
}

mythz added a commit that referenced this pull request Nov 17, 2012

Merge pull request #334 from softwx/master
MemoryCacheClient changed to use UTC DateTime internally

@mythz mythz merged commit f42c68a into ServiceStack:master Nov 17, 2012

Owner

mythz commented Nov 17, 2012

brilliant! thx.

For the memcached integration test, they're kept in the https://github.com/ServiceStack/ServiceStack.Contrib/ project. I've added it in this commit ServiceStackV3/ServiceStack.Contrib@b811d7c thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment