Skip to content

Commit

Permalink
Fix flaky SqliteCacheTests.BasicTests
Browse files Browse the repository at this point in the history
This test wasn't safe to test in parallel, due to the hard-coded
database name/path.

In addition to using `[TestCleanup]` to clear the database from disk
after each test is run, we're also using an instance-specific
`Configuration.CachePath` that's created on class init, so it's now safe
to run the same test but for different frameworks in parallel (each gets
its own path).
  • Loading branch information
mqudsi committed Nov 2, 2021
1 parent 33d8553 commit 09e34c1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
18 changes: 9 additions & 9 deletions SqliteCache.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ namespace NeoSmart.Caching.Sqlite.Tests
public class SqliteCacheTests
{
public Encoding DefaultEncoding = new UTF8Encoding(false);
SqliteCacheOptions Configuration => new SqliteCacheOptions()
private readonly SqliteCacheOptions Configuration = new SqliteCacheOptions()
{
MemoryOnly = false,
CachePath = "test.db",
CachePath = $"test-{Guid.NewGuid()}.db",
};

public SqliteCacheTests()
[TestCleanup]
public void DeleteTestDb()
{
System.IO.File.Delete(Configuration.CachePath);
}

private SqliteCache CreateDefault()
private SqliteCache CreateDefault(bool persistent = true)
{
var logger = new TestLogger<SqliteCache>();
var cacheDb = new SqliteCache(Configuration, logger);
var cacheDb = new SqliteCache(Configuration with { MemoryOnly = !persistent }, logger);

return cacheDb;
}

[TestMethod]
public async Task BasicTests()
{
System.IO.File.Delete(Configuration.CachePath);

using (var cache = CreateDefault())
using (var cache = CreateDefault(true))
{
var bytes = cache.Get("hello");
Assert.IsNull(bytes);
Expand All @@ -50,7 +50,7 @@ public async Task BasicTests()
}

// Check persistence
using (var cache = CreateDefault())
using (var cache = CreateDefault(true))
{
var bytes = await cache.GetAsync("hello");
CollectionAssert.AreEqual(bytes, DefaultEncoding.GetBytes("hello"));
Expand Down
20 changes: 9 additions & 11 deletions SqliteCache.Tests/BulkInsertTests.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ namespace NeoSmart.Caching.Sqlite.Tests
public class BulkInsertTests
{
public Encoding DefaultEncoding = new UTF8Encoding(false);
SqliteCacheOptions Configuration => new SqliteCacheOptions()
private readonly SqliteCacheOptions Configuration = new SqliteCacheOptions()
{
MemoryOnly = false,
CachePath = "bulktest.db",
CachePath = $"bulktest-{Guid.NewGuid()}.db",
};

public BulkInsertTests()
[TestCleanup]
public void DeleteTestDb()
{
System.IO.File.Delete(Configuration.CachePath);
}

private SqliteCache CreateDefault()
private SqliteCache CreateDefault(bool persistent = false)
{
var logger = new TestLogger<SqliteCache>();
var cacheDb = new SqliteCache(Configuration, logger);
var cacheDb = new SqliteCache(Configuration with { MemoryOnly = !persistent }, logger);

return cacheDb;
}

[TestMethod]
public async Task BasicBulkTests()
{
System.IO.File.Delete(Configuration.CachePath);

using (var cache = CreateDefault())
using (var cache = CreateDefault(true))
{
var item1 = cache.Get("firstItem");
Assert.IsNull(item1);
Expand Down Expand Up @@ -63,7 +63,7 @@ public async Task BasicBulkTests()
}

// Check persistence
using (var cache = CreateDefault())
using (var cache = CreateDefault(true))
{
var bytes = await cache.GetAsync("firstItem");
Assert.IsNotNull(bytes);
Expand All @@ -78,8 +78,6 @@ public async Task BasicBulkTests()
[TestMethod]
public async Task MultipleBulkCalls()
{
System.IO.File.Delete(Configuration.CachePath);

using (var cache = CreateDefault())
{
var item1 = cache.Get("firstItem");
Expand Down
2 changes: 1 addition & 1 deletion SqliteCache/SqliteCacheOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NeoSmart.Caching.Sqlite
{
public class SqliteCacheOptions : IOptions<SqliteCacheOptions>
public record SqliteCacheOptions : IOptions<SqliteCacheOptions>
{
SqliteCacheOptions IOptions<SqliteCacheOptions>.Value => this;

Expand Down

0 comments on commit 09e34c1

Please sign in to comment.