Skip to content

Commit

Permalink
tests: add tests for the many ways to SetAbsoluteExpiration (2 FAILS …
Browse files Browse the repository at this point in the history
…in timespan tests)
  • Loading branch information
alastairtree committed Sep 19, 2020
1 parent 66e66cd commit 8e5e506
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,154 @@ public void GetOrAddWithOffsetWillAddAndReturnCached()
Assert.AreEqual(2001, expectedSecond.Year);
}

[Test]
public void GetOrAddWithAbsoluteOffsetExpiryAsDateTimeOffsetDoesExpireItems()
{
var millisecondsCacheDuration = 100;
var validResult = sut.GetOrAdd(
TestKey,
() => new ComplexTestObject(),
DateTimeOffset.Now.AddMilliseconds(millisecondsCacheDuration)
);
// pass expiry time with a delay
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 50));
var expiredResult = sut.Get<ComplexTestObject>(TestKey);

Assert.That(validResult, Is.Not.Null);
Assert.That(expiredResult, Is.Null);
}

[Test]
public void GetOrAddWithAbsoluteOffsetExpiryAsTimeSpanDoesExpireItems()
{
var millisecondsCacheDuration = 100;
var validResult = sut.GetOrAdd(
TestKey,
() => new ComplexTestObject(),
TimeSpan.FromMilliseconds(millisecondsCacheDuration)
);
// pass expiry time with a delay
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 50));
var expiredResult = sut.Get<ComplexTestObject>(TestKey);

Assert.That(validResult, Is.Not.Null);
Assert.That(expiredResult, Is.Null);
}

[Test]
public void GetOrAddWithAbsoluteOffsetExpiryInTheDelegateDoesExpireItems()
{
var millisecondsCacheDuration = 100;
var validResult = sut.GetOrAdd(
TestKey,
entry =>
{
entry.SetAbsoluteExpiration(DateTimeOffset.Now.AddMilliseconds(millisecondsCacheDuration));
return new ComplexTestObject();
}
);
// pass expiry time with a delay
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 50));
var expiredResult = sut.Get<ComplexTestObject>(TestKey);

Assert.That(validResult, Is.Not.Null);
Assert.That(expiredResult, Is.Null);
}

[Test]
public void GetOrAddWithAbsoluteOffsetExpiryInTheDelegateUsingTimeSpanDoesExpireItems()
{
var millisecondsCacheDuration = 100;
var validResult = sut.GetOrAdd(
TestKey,
entry =>
{
entry.SetAbsoluteExpiration(TimeSpan.FromMilliseconds(millisecondsCacheDuration));
return new ComplexTestObject();
}
);
// pass expiry time with a delay
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 50));
var expiredResult = sut.Get<ComplexTestObject>(TestKey);

Assert.That(validResult, Is.Not.Null);
Assert.That(expiredResult, Is.Null);
}

[Test]
public async Task GetOrAddAsyncWithAbsoluteOffsetExpiryAsDateTimeOffsetDoesExpireItems()
{
var millisecondsCacheDuration = 100;
var validResult = await sut.GetOrAddAsync(
TestKey,
() => Task.FromResult(new ComplexTestObject()),
DateTimeOffset.Now.AddMilliseconds(millisecondsCacheDuration)
);
// pass expiry time with a delay
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 50));
var expiredResult = sut.Get<ComplexTestObject>(TestKey);

Assert.That(validResult, Is.Not.Null);
Assert.That(expiredResult, Is.Null);
}

[Test]
public async Task GetOrAddAsyncWithAbsoluteOffsetExpiryAsTimeSpanDoesExpireItems()
{
var millisecondsCacheDuration = 100;
var validResult = await sut.GetOrAddAsync(
TestKey,
() => Task.FromResult(new ComplexTestObject()),
TimeSpan.FromMilliseconds(millisecondsCacheDuration)
);
// pass expiry time with a delay
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 50));
var expiredResult = sut.Get<ComplexTestObject>(TestKey);

Assert.That(validResult, Is.Not.Null);
Assert.That(expiredResult, Is.Null);
}

[Test]
public async Task GetOrAddAsyncWithAbsoluteOffsetExpiryInTheDelegateDoesExpireItems()
{
var millisecondsCacheDuration = 100;
var validResult = await sut.GetOrAddAsync(
TestKey,
entry =>
{
entry.SetAbsoluteExpiration(DateTimeOffset.Now.AddMilliseconds(millisecondsCacheDuration));
return Task.FromResult(new ComplexTestObject());
}
);
// pass expiry time with a delay
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 50));
var expiredResult = sut.Get<ComplexTestObject>(TestKey);

Assert.That(validResult, Is.Not.Null);
Assert.That(expiredResult, Is.Null);
}

[Test]
public async Task GetOrAddAsyncWithAbsoluteOffsetExpiryInTheDelegateUsingTimeSpanDoesExpireItems()
{
var millisecondsCacheDuration = 100;
var validResult = await sut.GetOrAddAsync(
TestKey,
entry =>
{
entry.SetAbsoluteExpiration(TimeSpan.FromMilliseconds(millisecondsCacheDuration));
return Task.FromResult(new ComplexTestObject());
}
);
// pass expiry time with a delay
Thread.Sleep(TimeSpan.FromMilliseconds(millisecondsCacheDuration + 50));
var expiredResult = sut.Get<ComplexTestObject>(TestKey);

Assert.That(validResult, Is.Not.Null);
Assert.That(expiredResult, Is.Null);
}

[Test]
public void GetOrAddWithPolicyAndThenGetObjectReturnsCorrectType()
{
Expand Down

0 comments on commit 8e5e506

Please sign in to comment.