Skip to content

Commit

Permalink
Fix for throws KeyNotFoundException while Get(key) (#100)
Browse files Browse the repository at this point in the history
* Fix for throws KeyNotFoundException while Get(key)

Fix for #51
Add test to show operates correctly

* Fix warnings and merge from main
  • Loading branch information
ChrisPulman authored Jul 30, 2022
1 parent 5962eec commit b706fa9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591;1701;1702;1705;VSX1000</NoWarn>
<NoWarn>$(NoWarn);1591;1701;1702;1705;VSX1000;IDE0190</NoWarn>
<Platform>AnyCPU</Platform>
<IsTestProject>$(MSBuildProjectName.Contains('Tests'))</IsTestProject>
<DebugType>embedded</DebugType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class SettingsCacheTests
/// <summary>
/// Test1s this instance.
/// </summary>
/// <param name="overrideDatabaseName">Name of the override database.</param>
[Fact]
public async void TestCreateAndInsert()
{
Expand Down
5 changes: 1 addition & 4 deletions src/ReactiveMarbles.CacheDatabase.Settings/Core/AppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ static AppInfo()
/// Overrides the settings cache path.
/// </summary>
/// <param name="path">The path.</param>
public static void OverrideSettingsCachePath(string path)
{
SettingsCachePath = path;
}
public static void OverrideSettingsCachePath(string path) => SettingsCachePath = path;

/// <summary>
/// Deletes the settings store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ public void Dispose()
/// <returns>
/// A task that represents the asynchronous dispose operation.
/// </returns>
public ValueTask DisposeAsync() => _blobCache.DisposeAsync();
public ValueTask DisposeAsync()
{
var result = _blobCache.DisposeAsync();
GC.SuppressFinalize(this);
return result;
}

/// <summary>
/// Gets the value for the specified key, or, if the value doesn't exist, saves the <paramref name="defaultValue" /> and returns it.
Expand Down
41 changes: 4 additions & 37 deletions src/ReactiveMarbles.CacheDatabase.Sqlite3/SqliteBlobCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,43 +171,10 @@ public IObservable<Unit> Flush(Type type)
}

var time = DateTimeOffset.UtcNow;
return _initialized
.SelectMany(async _ =>
{
try
{
var value = await Connection.GetAsync<CacheEntry>(key).ConfigureAwait(false);

if (value is null)
{
throw new KeyNotFoundException($"Key {key} could not be found.");
}

if (value.TypeName is not null)
{
throw new KeyNotFoundException($"Key {key} is associated with an object.");
}

if (value.Id is null)
{
throw new KeyNotFoundException($"Key {key} is id is null.");
}

if (value.ExpiresAt <= time)
{
throw new KeyNotFoundException($"Key {key} has expired.");
}

return value;
}
catch (Exception)
{
throw new KeyNotFoundException($"Key {key} could not be found.");
}
})
.Select(x => x.Value)
.Where(x => x is not null)
.Select(x => x!);
return _initialized.SelectMany((_, _, _) => Connection.Table<CacheEntry>().FirstAsync(x => x.Id != null && x.ExpiresAt > time && x.Id == key))
.Catch<CacheEntry, InvalidOperationException>(ex => Observable.Throw<CacheEntry>(new KeyNotFoundException(ex.Message)))
.Where(x => x?.Value is not null)
.Select(x => x.Value!);
}

/// <inheritdoc/>
Expand Down
19 changes: 19 additions & 0 deletions src/ReactiveMarbles.CacheDatabase.Tests/BlobCacheTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,25 @@ public async Task VacuumPurgeEntriesThatAreExpired()
}
}

/// <summary>
/// Tests the issue.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestIssueAsync()
{
using (Utility.WithEmptyDirectory(out var path))
await using (var fixture = CreateBlobCache(path))
{
var cacheKey = "cacheKey";
var someObject = new string[] { "someObject" };
await fixture.InsertObject(cacheKey, someObject.AsEnumerable(), null);
Assert.NotNull(await fixture.GetObject<IEnumerable<string>>(cacheKey));
Assert.NotNull(await fixture.Get(cacheKey, typeof(IEnumerable<string>)));
Assert.NotNull(await fixture.Get(cacheKey));
}
}

/// <summary>
/// Gets the <see cref="IBlobCache"/> we want to do the tests against.
/// </summary>
Expand Down

0 comments on commit b706fa9

Please sign in to comment.