-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
2,336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
using System; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading; | ||
using Dapper; | ||
using Hangfire.SQLite.Tests.Utils; | ||
using Xunit; | ||
|
||
namespace Hangfire.SQLite.Tests | ||
{ | ||
public class ExpirationManagerFacts | ||
{ | ||
private readonly CancellationToken _token; | ||
|
||
public ExpirationManagerFacts() | ||
{ | ||
var cts = new CancellationTokenSource(); | ||
_token = cts.Token; | ||
} | ||
|
||
[Fact] | ||
public void Ctor_ThrowsAnException_WhenStorageIsNull() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => new ExpirationManager(null)); | ||
} | ||
|
||
[Fact, CleanDatabase] | ||
public void Execute_RemovesOutdatedRecords() | ||
{ | ||
using (var connection = CreateConnection()) | ||
{ | ||
var entryId = CreateExpirationEntry(connection, DateTime.UtcNow.AddMonths(-1)); | ||
var manager = CreateManager(connection); | ||
|
||
manager.Execute(_token); | ||
|
||
Assert.True(IsEntryExpired(connection, entryId)); | ||
} | ||
} | ||
|
||
[Fact, CleanDatabase] | ||
public void Execute_DoesNotRemoveEntries_WithNoExpirationTimeSet() | ||
{ | ||
using (var connection = CreateConnection()) | ||
{ | ||
var entryId = CreateExpirationEntry(connection, null); | ||
var manager = CreateManager(connection); | ||
|
||
manager.Execute(_token); | ||
|
||
Assert.False(IsEntryExpired(connection, entryId)); | ||
} | ||
} | ||
|
||
[Fact, CleanDatabase] | ||
public void Execute_DoesNotRemoveEntries_WithFreshExpirationTime() | ||
{ | ||
using (var connection = CreateConnection()) | ||
{ | ||
var entryId = CreateExpirationEntry(connection, DateTime.Now.AddMonths(1)); | ||
var manager = CreateManager(connection); | ||
|
||
manager.Execute(_token); | ||
|
||
Assert.False(IsEntryExpired(connection, entryId)); | ||
} | ||
} | ||
|
||
[Fact, CleanDatabase] | ||
public void Execute_Processes_AggregatedCounterTable() | ||
{ | ||
using (var connection = CreateConnection()) | ||
{ | ||
// Arrange | ||
const string createSql = @" | ||
insert into [HangFire.AggregatedCounter] ([Key], [Value], ExpireAt) | ||
values ('key', 1, @expireAt)"; | ||
connection.Execute(createSql, new { expireAt = DateTime.UtcNow.AddMonths(-1) }); | ||
|
||
var manager = CreateManager(connection); | ||
|
||
// Act | ||
manager.Execute(_token); | ||
|
||
// Assert | ||
Assert.Equal(0, connection.Query<int>(@"select count(*) from [HangFire.Counter]").Single()); | ||
} | ||
} | ||
|
||
[Fact, CleanDatabase] | ||
public void Execute_Processes_JobTable() | ||
{ | ||
using (var connection = CreateConnection()) | ||
{ | ||
// Arrange | ||
const string createSql = @" | ||
insert into [HangFire.Job] (InvocationData, Arguments, CreatedAt, ExpireAt) | ||
values ('', '', datetime('now', 'utc'), @expireAt)"; | ||
connection.Execute(createSql, new { expireAt = DateTime.UtcNow.AddMonths(-1) }); | ||
|
||
var manager = CreateManager(connection); | ||
|
||
// Act | ||
manager.Execute(_token); | ||
|
||
// Assert | ||
Assert.Equal(0, connection.Query<int>(@"select count(*) from [HangFire.Job]").Single()); | ||
} | ||
} | ||
|
||
[Fact, CleanDatabase] | ||
public void Execute_Processes_ListTable() | ||
{ | ||
using (var connection = CreateConnection()) | ||
{ | ||
// Arrange | ||
const string createSql = @" | ||
insert into [HangFire.List] ([Key], ExpireAt) | ||
values ('key', @expireAt)"; | ||
connection.Execute(createSql, new { expireAt = DateTime.UtcNow.AddMonths(-1) }); | ||
|
||
var manager = CreateManager(connection); | ||
|
||
// Act | ||
manager.Execute(_token); | ||
|
||
// Assert | ||
Assert.Equal(0, connection.Query<int>(@"select count(*) from [HangFire.List]").Single()); | ||
} | ||
} | ||
|
||
[Fact, CleanDatabase] | ||
public void Execute_Processes_SetTable() | ||
{ | ||
using (var connection = CreateConnection()) | ||
{ | ||
// Arrange | ||
const string createSql = @" | ||
insert into [HangFire.Set] ([Key], [Score], [Value], ExpireAt) | ||
values ('key', 0, '', @expireAt)"; | ||
connection.Execute(createSql, new { expireAt = DateTime.UtcNow.AddMonths(-1) }); | ||
|
||
var manager = CreateManager(connection); | ||
|
||
// Act | ||
manager.Execute(_token); | ||
|
||
// Assert | ||
Assert.Equal(0, connection.Query<int>(@"select count(*) from [HangFire.Set]").Single()); | ||
} | ||
} | ||
|
||
[Fact, CleanDatabase] | ||
public void Execute_Processes_HashTable() | ||
{ | ||
using (var connection = CreateConnection()) | ||
{ | ||
// Arrange | ||
const string createSql = @" | ||
insert into [HangFire.Hash] ([Key], [Field], [Value], ExpireAt) | ||
values ('key', 'field', '', @expireAt)"; | ||
connection.Execute(createSql, new { expireAt = DateTime.UtcNow.AddMonths(-1) }); | ||
|
||
var manager = CreateManager(connection); | ||
|
||
// Act | ||
manager.Execute(_token); | ||
|
||
// Assert | ||
Assert.Equal(0, connection.Query<int>(@"select count(*) from [HangFire.Hash]").Single()); | ||
} | ||
} | ||
|
||
private static int CreateExpirationEntry(IDbConnection connection, DateTime? expireAt) | ||
{ | ||
const string insertSql = @" | ||
insert into [HangFire.AggregatedCounter] ([Key], [Value], [ExpireAt]) | ||
values ('key', 1, @expireAt); | ||
select last_insert_rowid() as Id;"; | ||
|
||
var id = connection.Query(insertSql, new { @expireAt = expireAt }).Single(); | ||
var recordId = (int) id.Id; | ||
return recordId; | ||
} | ||
|
||
private static bool IsEntryExpired(IDbConnection connection, int entryId) | ||
{ | ||
var count = connection.Query<int>( | ||
"select count(*) from [HangFire.AggregatedCounter] where Id = @id", new { id = entryId }).Single(); | ||
return count == 0; | ||
} | ||
|
||
private IDbConnection CreateConnection() | ||
{ | ||
return ConnectionUtils.CreateConnection(); | ||
} | ||
|
||
private ExpirationManager CreateManager(IDbConnection connection) | ||
{ | ||
var storage = new SQLiteStorage(connection); | ||
return new ExpirationManager(storage, TimeSpan.Zero); | ||
} | ||
} | ||
} |
Oops, something went wrong.