2013-05-23 07:12:01 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using FluentAssertions;
|
2013-05-01 03:11:00 +02:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Test.CacheTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class CachedFixture
|
|
|
|
|
{
|
|
|
|
|
private Cached<string> _cachedString = new Cached<string>();
|
|
|
|
|
private Worker _worker;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
|
|
|
|
_cachedString = new Cached<string>();
|
|
|
|
|
_worker = new Worker();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_call_function_once()
|
|
|
|
|
{
|
|
|
|
|
_cachedString.Get("Test", _worker.GetString);
|
|
|
|
|
_cachedString.Get("Test", _worker.GetString);
|
|
|
|
|
|
|
|
|
|
_worker.HitCount.Should().Be(1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void multiple_calls_should_return_same_result()
|
|
|
|
|
{
|
|
|
|
|
var first = _cachedString.Get("Test", _worker.GetString);
|
|
|
|
|
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");
|
|
|
|
|
}
|
2013-05-23 07:12:01 +02:00
|
|
|
|
|
|
|
|
|
[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");
|
|
|
|
|
}
|
2013-05-01 03:11:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Worker
|
|
|
|
|
{
|
|
|
|
|
public int HitCount { get; private set; }
|
|
|
|
|
|
|
|
|
|
public string GetString()
|
|
|
|
|
{
|
|
|
|
|
HitCount++;
|
|
|
|
|
return "Hit count is " + HitCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|