added TTL to cached objects

This commit is contained in:
Keivan Beigi 2013-07-23 17:35:35 -07:00
commit 121f3b973d
11 changed files with 160 additions and 141 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Threading;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Cache;
@ -28,8 +29,6 @@ namespace NzbDrone.Common.Test.CacheTests
}
[Test]
public void multiple_calls_should_return_same_result()
{
@ -37,43 +36,16 @@ namespace NzbDrone.Common.Test.CacheTests
var second = _cachedString.Get("Test", _worker.GetString);
first.Should().Be(second);
}
[Test]
public void should_remove_value_from_set()
{
_cachedString.Get("Test", _worker.GetString);
_cachedString.Remove("Test");
_cachedString.Get("Test", _worker.GetString);
_worker.HitCount.Should().Be(2);
}
[Test]
public void remove_none_existing_should_break_things()
{
_cachedString.Remove("Test");
}
[Test]
public void get_without_callback_should_throw_on_invalid_key()
{
Assert.Throws<KeyNotFoundException>(() => _cachedString.Get("InvalidKey"));
}
[Test]
public void should_be_able_to_update_key()
{
_cachedString.Set("Key", "Old");
_cachedString.Set("Key", "New");
_cachedString.Get("Key").Should().Be("New");
_cachedString.Find("Key").Should().Be("New");
}
[Test]
@ -85,14 +57,34 @@ namespace NzbDrone.Common.Test.CacheTests
for (int i = 0; i < 10; i++)
{
_cachedString.Get("key", () =>
{
hitCount++;
return null;
});
{
hitCount++;
return null;
});
}
hitCount.Should().Be(1);
}
[Test]
public void should_honor_ttl()
{
int hitCount = 0;
_cachedString = new Cached<string>();
for (int i = 0; i < 100; i++)
{
_cachedString.Get("key", () =>
{
hitCount++;
return null;
}, TimeSpan.FromMilliseconds(200));
Thread.Sleep(10);
}
hitCount.Should().BeInRange(4, 6);
}
}
public class Worker

View file

@ -7,7 +7,7 @@ using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test.CacheTests
{
[TestFixture]
public class CachedManagerFixture:TestBase<ICacheManger>
public class CachedManagerFixture : TestBase<ICacheManger>
{
[Test]
public void should_return_proper_type_of_cache()
@ -17,7 +17,6 @@ namespace NzbDrone.Common.Test.CacheTests
result.Should().BeOfType<Cached<DateTime>>();
}
[Test]
public void multiple_calls_should_get_the_same_cache()
{
@ -26,9 +25,5 @@ namespace NzbDrone.Common.Test.CacheTests
result1.Should().BeSameAs(result2);
}
}
}