Skip to content

Commit

Permalink
Fix FetchFunctionShouldDebounceConcurrentRequests test (#193)
Browse files Browse the repository at this point in the history
* Fix FetchFunctionShouldDebounceConcurrentRequests test

* Update BlobCacheTestsBase.cs
  • Loading branch information
ChrisPulman authored Nov 10, 2023
1 parent 87fb473 commit 947c2d0
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 95 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;IDE0190</NoWarn>
<NoWarn>$(NoWarn);1591;1701;1702;1705;VSX1000;IDE0190;IDE1006;IDE0071</NoWarn>
<Platform>AnyCPU</Platform>
<IsTestProject>$(MSBuildProjectName.Contains('Tests'))</IsTestProject>
<DebugType>embedded</DebugType>
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveMarbles.CacheDatabase.Core/LoginExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static IObservable<Unit> SaveLogin(this ISecureBlobCache blobCache, strin
/// <param name="blobCache">The blob cache where to get the data.</param>
/// <param name="host">The host associated with the data.</param>
/// <returns>A Future result representing the user/password Tuple.</returns>
public static IObservable<LoginInfo> GetLoginAsync(this ISecureBlobCache blobCache, string host = "default") =>
public static IObservable<LoginInfo> GetLogin(this ISecureBlobCache blobCache, string host = "default") =>
blobCache.GetObject<(string, string)>("login:" + host).Select(x => new LoginInfo(x));

/// <summary>
Expand Down
22 changes: 8 additions & 14 deletions src/ReactiveMarbles.CacheDatabase.Core/LoginInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ namespace ReactiveMarbles.CacheDatabase.Core
/// <summary>
/// Stored login information for a user.
/// </summary>
public class LoginInfo
/// <remarks>
/// Initializes a new instance of the <see cref="LoginInfo"/> class.
/// </remarks>
/// <param name="username">The username for the entry.</param>
/// <param name="password">The password for the user.</param>
public class LoginInfo(string username, string password)
{
/// <summary>
/// Initializes a new instance of the <see cref="LoginInfo"/> class.
/// </summary>
/// <param name="username">The username for the entry.</param>
/// <param name="password">The password for the user.</param>
public LoginInfo(string username, string password)
{
UserName = username;
Password = password;
}

/// <summary>
/// Initializes a new instance of the <see cref="LoginInfo"/> class.
/// </summary>
Expand All @@ -32,11 +26,11 @@ internal LoginInfo((string UserName, string Password) usernameAndLogin)
/// <summary>
/// Gets the username.
/// </summary>
public string UserName { get; }
public string UserName { get; } = username;

/// <summary>
/// Gets the password.
/// </summary>
public string Password { get; }
public string Password { get; } = password;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,6 @@ blobCache is null
Func<T, bool>? cacheValidationPredicate = null) =>
blobCache.GetAndFetchLatest(key, () => fetchFunc().ToObservable(), fetchPredicate, absoluteExpiration, shouldInvalidateOnError, cacheValidationPredicate);

internal static string GetTypePrefixedKey(string key, Type type) => type.FullName + "___" + key;
internal static string GetTypePrefixedKey(this string key, Type type) => type.FullName + "___" + key;
}
}
208 changes: 130 additions & 78 deletions src/ReactiveMarbles.CacheDatabase.Tests/BlobCacheTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using ReactiveMarbles.CacheDatabase.SystemTextJson;
using ReactiveMarbles.CacheDatabase.Tests.Helpers;
using ReactiveMarbles.CacheDatabase.Tests.Mocks;
using ReactiveUI;
using ReactiveUI.Testing;
using SQLite;
using Xunit;
Expand Down Expand Up @@ -296,92 +297,143 @@ public async Task FetchFunctionShouldBeCalledOnceForGetOrFetchObject()
/// <summary>
/// Makes sure the fetch function debounces current requests.
/// </summary>
[Fact(Skip = "TestScheduler tests aren't gonna work with new SQLite")]
public void FetchFunctionShouldDebounceConcurrentRequests() =>
new TestScheduler().With(sched =>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact] // (Skip = "TestScheduler tests aren't gonna work with new SQLite")]
public async Task FetchFunctionShouldDebounceConcurrentRequestsAsync()
{
using var testSequencer = new TestSequencer();
var sched = new TestScheduler();
using (Utility.WithEmptyDirectory(out var path))
{
using (Utility.WithEmptyDirectory(out var path))
var callCount = 0;
var fetcher = new Func<IObservable<int>>(() =>
{
var callCount = 0;
var fetcher = new Func<IObservable<int>>(() =>
callCount++;
return Observable.Return(42)
.Delay(TimeSpan.FromMilliseconds(1000), ImmediateScheduler.Instance);
});

var fixture = CreateBlobCache(path);
sched.Start();

var result1 = 0;
var result2 = 0;
var result3 = 0;
var result4 = 0;
var result5 = 0;
fixture.GetOrFetchObject("foo", fetcher)
.ObserveOn(ImmediateScheduler.Instance)
.Subscribe(async x =>
{
result1++;
if (result2 == 1 && result3 == 1)
{
callCount++;
return Observable.Return(42).Delay(TimeSpan.FromMilliseconds(1000), sched);
});
await testSequencer.AdvancePhaseAsync("Result 1");
}
});

var fixture = CreateBlobCache(path);
try
Assert.Equal(0, result1);

sched.AdvanceToMs(250);

// Nobody's returned yet, cache is empty, we should have called the fetcher
// once to get a result
fixture.GetOrFetchObject("foo", fetcher)
.ObserveOn(ImmediateScheduler.Instance)
.Subscribe(async x =>
{
result2++;
if (result1 == 1 && result3 == 1)
{
fixture.GetOrFetchObject("foo", fetcher).ToObservableChangeSet(ImmediateScheduler.Instance).Bind(out var result1).Subscribe();

Assert.Equal(0, result1.Count);

sched.AdvanceToMs(250);

// Nobody's returned yet, cache is empty, we should have called the fetcher
// once to get a result
fixture.GetOrFetchObject("foo", fetcher).ToObservableChangeSet(ImmediateScheduler.Instance).Bind(out var result2).Subscribe();
Assert.Equal(0, result1.Count);
Assert.Equal(0, result2.Count);
Assert.Equal(1, callCount);

sched.AdvanceToMs(750);

// Same as above, result1-3 are all listening to the same fetch
fixture.GetOrFetchObject("foo", fetcher).ToObservableChangeSet(ImmediateScheduler.Instance).Bind(out var result3).Subscribe();
Assert.Equal(0, result1.Count);
Assert.Equal(0, result2.Count);
Assert.Equal(0, result3.Count);
Assert.Equal(1, callCount);

// Fetch returned, all three collections should have an item
sched.AdvanceToMs(1250);
Assert.Equal(1, result1.Count);
Assert.Equal(1, result2.Count);
Assert.Equal(1, result3.Count);
Assert.Equal(1, callCount);

// Making a new call, but the cache has an item, this shouldn't result
// in a fetcher call either
fixture.GetOrFetchObject("foo", fetcher).ToObservableChangeSet(ImmediateScheduler.Instance).Bind(out var result4).Subscribe();
sched.AdvanceToMs(2500);
Assert.Equal(1, result1.Count);
Assert.Equal(1, result2.Count);
Assert.Equal(1, result3.Count);
Assert.Equal(1, result4.Count);
Assert.Equal(1, callCount);

// Making a new call, but with a new key - this *does* result in a fetcher
// call. Result1-4 shouldn't get any new items, and at t=3000, we haven't
// returned from the call made at t=2500 yet
fixture.GetOrFetchObject("bar", fetcher).ToObservableChangeSet(ImmediateScheduler.Instance).Bind(out var result5).Subscribe();
sched.AdvanceToMs(3000);
Assert.Equal(1, result1.Count);
Assert.Equal(1, result2.Count);
Assert.Equal(1, result3.Count);
Assert.Equal(1, result4.Count);
Assert.Equal(0, result5.Count);
Assert.Equal(2, callCount);

// Everything is done, we should have one item in result5 now
sched.AdvanceToMs(4000);
Assert.Equal(1, result1.Count);
Assert.Equal(1, result2.Count);
Assert.Equal(1, result3.Count);
Assert.Equal(1, result4.Count);
Assert.Equal(1, result5.Count);
Assert.Equal(2, callCount);
await testSequencer.AdvancePhaseAsync("Result 2");
}
finally
});

Assert.Equal(0, result1);
Assert.Equal(0, result2);
Assert.Equal(0, callCount);

sched.AdvanceToMs(750);

// Same as above, result1-3 are all listening to the same fetch
fixture.GetOrFetchObject("foo", fetcher)
.ObserveOn(ImmediateScheduler.Instance)
.Subscribe(async x =>
{
result3++;
if (result1 == 1 && result2 == 1)
{
// Since we're in TestScheduler, we can't use the normal
// using statement, we need to kick off the async dispose,
// then start the scheduler to let it run
fixture.Dispose();
sched.Start();
await testSequencer.AdvancePhaseAsync("Result 3");
}
}
});
});

Assert.Equal(0, result1);
Assert.Equal(0, result2);
Assert.Equal(0, result3);
Assert.Equal(0, callCount);

// Fetch returned, all three collections should have an item
sched.AdvanceToMs(1250);
await testSequencer.AdvancePhaseAsync("Result 1-3");
Assert.Equal(1, result1);
Assert.Equal(1, result2);
Assert.Equal(1, result3);
Assert.Equal(3, callCount);

// Making a new call, but the cache has an item, this shouldn't result
// in a fetcher call either
fixture.GetOrFetchObject("foo", fetcher)
.ObserveOn(ImmediateScheduler.Instance)
.Subscribe(async x =>
{
result4++;
await testSequencer.AdvancePhaseAsync("Result 4");
});

sched.AdvanceToMs(2500);
Assert.Equal(1, result1);
Assert.Equal(1, result2);
Assert.Equal(1, result3);
await testSequencer.AdvancePhaseAsync("Result 4");
Assert.Equal(1, result4);
Assert.Equal(4, callCount);

// Making a new call, but with a new key - this *does* result in a fetcher
// call. Result1-4 shouldn't get any new items, and at t=3000, we haven't
// returned from the call made at t=2500 yet
fixture.GetOrFetchObject("bar", fetcher) // .ToObservableChangeSet(ImmediateScheduler.Instance).Bind(out var result5).Subscribe();
.ObserveOn(ImmediateScheduler.Instance)
.Subscribe(async x =>
{
result5++;
await testSequencer.AdvancePhaseAsync("Result 5");
});

sched.AdvanceToMs(3000);
Assert.Equal(1, result1);
Assert.Equal(1, result2);
Assert.Equal(1, result3);
Assert.Equal(1, result4);
Assert.Equal(0, result5);
Assert.Equal(4, callCount);

// Everything is done, we should have one item in result5 now
sched.AdvanceToMs(4000);
Assert.Equal(1, result1);
Assert.Equal(1, result2);
Assert.Equal(1, result3);
Assert.Equal(1, result4);
await testSequencer.AdvancePhaseAsync("Result 5");
Assert.Equal(1, result5);
Assert.Equal(5, callCount);

// Since we're in TestScheduler, we can't use the normal
// using statement, we need to kick off the async dispose,
// then start the scheduler to let it run
fixture.Dispose();
testSequencer.Dispose();
}
}

/// <summary>
/// Makes sure that the fetch function propogates thrown exceptions.
Expand Down

0 comments on commit 947c2d0

Please sign in to comment.