Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elide some asyncs #224

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private async Task ExecutorAsync()
public async Task WithTimeout()
{
await RunUtils.RunAsyncActionAdvancedAsync(
async _ => await ExecutorAsync(),
_ => ExecutorAsync(),
TimeSpan.FromSeconds(2),
false,
true
Expand All @@ -45,7 +45,7 @@ await RunUtils.RunAsyncActionAdvancedAsync(
public async Task WithTimeout2()
{
await RunUtils.RunAsyncActionAdvancedAsync(
async _ => await ExecutorAsync(),
_ => ExecutorAsync(),
TimeSpan.FromSeconds(2),
true,
true
Expand All @@ -56,7 +56,7 @@ await RunUtils.RunAsyncActionAdvancedAsync(
public async Task WithoutTimeout()
{
await RunUtils.RunAsyncActionAdvancedAsync(
async _ => await ExecutorAsync(),
_ => ExecutorAsync(),
Timeout.InfiniteTimeSpan,
true,
true
Expand Down
4 changes: 2 additions & 2 deletions src/ZiggyCreatures.FusionCache.Chaos/ChaosMemoryLocker.cs
Copy link
Collaborator

@jodydonetti jodydonetti Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since MaybeChaos() can throw (that's the whole point of it), I'm wondering about any difference in behaviour introduced by the await elision with regard to exceptions (see here).

Any thoughts?

Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public ChaosMemoryLocker(IFusionCacheMemoryLocker innerMemoryLocker, ILogger<Cha
}

/// <inheritdoc/>
public async ValueTask<object?> AcquireLockAsync(string cacheName, string cacheInstanceId, string operationId, string key, TimeSpan timeout, ILogger? logger, CancellationToken token)
public ValueTask<object?> AcquireLockAsync(string cacheName, string cacheInstanceId, string operationId, string key, TimeSpan timeout, ILogger? logger, CancellationToken token)
{
MaybeChaos(token);
return await _innerMemoryLocker.AcquireLockAsync(cacheName, cacheInstanceId, operationId, key, timeout, logger, token);
return _innerMemoryLocker.AcquireLockAsync(cacheName, cacheInstanceId, operationId, key, timeout, logger, token);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public async ValueTask<bool> SetEntryAsync<TValue>(string operationId, string ke
try
{
timeout ??= options.GetAppropriateDistributedCacheTimeout(hasFallbackValue);
data = await RunUtils.RunAsyncFuncWithTimeoutAsync<byte[]?>(
async ct => await _cache.GetAsync(MaybeProcessCacheKey(key), ct).ConfigureAwait(false),
data = await RunUtils.RunAsyncFuncWithTimeoutAsync(
ct => _cache.GetAsync(MaybeProcessCacheKey(key), ct),
timeout.Value,
true,
token: token
Expand Down Expand Up @@ -255,15 +255,15 @@ public async ValueTask<bool> SetEntryAsync<TValue>(string operationId, string ke
return (null, false);
}

public async ValueTask<bool> RemoveEntryAsync(string operationId, string key, FusionCacheEntryOptions options, bool isBackground, CancellationToken token)
public ValueTask<bool> RemoveEntryAsync(string operationId, string key, FusionCacheEntryOptions options, bool isBackground, CancellationToken token)
{
if (IsCurrentlyUsable(operationId, key) == false)
return false;
return new ValueTask<bool>(false);

// ACTIVITY
using var activity = Activities.SourceDistributedLevel.StartActivityWithCommonTags(Activities.Names.DistributedRemove, _options.CacheName, _options.InstanceId!, key, operationId, CacheLevelKind.Distributed);

return await ExecuteOperationAsync(
return ExecuteOperationAsync(
operationId,
key,
async ct =>
Expand All @@ -276,6 +276,6 @@ public async ValueTask<bool> RemoveEntryAsync(string operationId, string key, Fu
"removing entry from distributed" + isBackground.ToString(" (background)"),
options,
token
).ConfigureAwait(false);
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -969,22 +969,22 @@ static async Task<int> FakeGetAsync(FusionCacheFactoryExecutionContext<int> ctx,
using var cache = new FusionCache(CreateFusionCacheOptions()).SetupDistributedCache(distributedCache, TestsUtils.GetSerializer(serializerType));

// TOT REQ + 1 / FULL RESP + 1
var v1 = await cache.GetOrSetAsync<int>(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v1 = await cache.GetOrSetAsync<int>(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

// CACHED -> NO INCR
var v2 = await cache.GetOrSetAsync<int>(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v2 = await cache.GetOrSetAsync<int>(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

// LET THE CACHE EXPIRE
await Task.Delay(duration.PlusALittleBit());

// TOT REQ + 1 / COND REQ + 1 / NOT MOD RESP + 1
var v3 = await cache.GetOrSetAsync<int>(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v3 = await cache.GetOrSetAsync<int>(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

// LET THE CACHE EXPIRE
await Task.Delay(duration.PlusALittleBit());

// TOT REQ + 1 / COND REQ + 1 / NOT MOD RESP + 1
var v4 = await cache.GetOrSetAsync<int>(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v4 = await cache.GetOrSetAsync<int>(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

// SET VALUE -> CHANGE LAST MODIFIED
endpoint.SetValue(42);
Expand All @@ -993,7 +993,7 @@ static async Task<int> FakeGetAsync(FusionCacheFactoryExecutionContext<int> ctx,
await Task.Delay(duration.PlusALittleBit());

// TOT REQ + 1 / COND REQ + 1 / FULL RESP + 1
var v5 = await cache.GetOrSetAsync<int>(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v5 = await cache.GetOrSetAsync<int>(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

Assert.Equal(4, endpoint.TotalRequestsCount);
Assert.Equal(3, endpoint.ConditionalRequestsCount);
Expand Down
10 changes: 5 additions & 5 deletions tests/ZiggyCreatures.FusionCache.Tests/MemoryLevelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,22 +1007,22 @@ static async Task<int> FakeGetAsync(FusionCacheFactoryExecutionContext<int> ctx,

using var cache = new FusionCache(new FusionCacheOptions());
// TOT REQ + 1 / FULL RESP + 1
var v1 = await cache.GetOrSetAsync<int>("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v1 = await cache.GetOrSetAsync<int>("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

// CACHED -> NO INCR
var v2 = await cache.GetOrSetAsync<int>("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v2 = await cache.GetOrSetAsync<int>("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

// LET THE CACHE EXPIRE
await Task.Delay(duration.PlusALittleBit());

// TOT REQ + 1 / COND REQ + 1 / NOT MOD RESP + 1
var v3 = await cache.GetOrSetAsync<int>("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v3 = await cache.GetOrSetAsync<int>("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

// LET THE CACHE EXPIRE
await Task.Delay(duration.PlusALittleBit());

// TOT REQ + 1 / COND REQ + 1 / NOT MOD RESP + 1
var v4 = await cache.GetOrSetAsync<int>("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v4 = await cache.GetOrSetAsync<int>("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

// SET VALUE -> CHANGE LAST MODIFIED
endpoint.SetValue(42);
Expand All @@ -1031,7 +1031,7 @@ static async Task<int> FakeGetAsync(FusionCacheFactoryExecutionContext<int> ctx,
await Task.Delay(duration.PlusALittleBit());

// TOT REQ + 1 / COND REQ + 1 / FULL RESP + 1
var v5 = await cache.GetOrSetAsync<int>("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));
var v5 = await cache.GetOrSetAsync<int>("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true));

Assert.Equal(4, endpoint.TotalRequestsCount);
Assert.Equal(3, endpoint.ConditionalRequestsCount);
Expand Down
Loading