Skip to content

Commit

Permalink
try get nullable (#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jan 14, 2023
1 parent ab957f2 commit 7cf4b58
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 111 deletions.
4 changes: 2 additions & 2 deletions src/Polly.Benchmarks/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public MemoryCacheProvider(IMemoryCache memoryCache) =>
return (cacheHit, value);
}

public void Put(string key, object value, Ttl ttl)
public void Put(string key, object? value, Ttl ttl)
{
TimeSpan remaining = DateTimeOffset.MaxValue - DateTimeOffset.UtcNow;
var options = new MemoryCacheEntryOptions();
Expand All @@ -86,7 +86,7 @@ public void Put(string key, object value, Ttl ttl)
public Task<(bool, object?)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) =>
Task.FromResult(TryGet(key));

public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
public Task PutAsync(string key, object? value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
Put(key, value, ttl);
return Task.CompletedTask;
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Specs/Bulkhead/BulkheadAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQu
Policy.BulkheadAsync(maxParallelization, maxQueuingActions);

protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action) =>
action.ExecuteOnBulkheadAsync((AsyncBulkheadPolicy)bulkhead);
action.ExecuteOnBulkheadAsync((AsyncBulkheadPolicy) bulkhead);

#endregion
}
10 changes: 9 additions & 1 deletion src/Polly.Specs/Bulkhead/BulkheadScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ public BulkheadScenario(int maxParallelization, int maxQueuingActions, int total
}

public object[] ToTheoryData() =>
new object[] {_maxParallelization, _maxQueuingActions, _totalTestLoad, _cancelQueuing, _cancelExecuting, _scenario };
new object[]
{
_maxParallelization,
_maxQueuingActions,
_totalTestLoad,
_cancelQueuing,
_cancelExecuting,
_scenario
};
}
2 changes: 1 addition & 1 deletion src/Polly.Specs/Bulkhead/BulkheadTResultAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected override IBulkheadPolicy GetBulkhead(int maxParallelization, int maxQu
Policy.BulkheadAsync<ResultPrimitive>(maxParallelization, maxQueuingActions);

protected override Task ExecuteOnBulkhead(IBulkheadPolicy bulkhead, TraceableAction action) =>
action.ExecuteOnBulkheadAsync<ResultPrimitive>((AsyncBulkheadPolicy<ResultPrimitive>)bulkhead);
action.ExecuteOnBulkheadAsync<ResultPrimitive>((AsyncBulkheadPolicy<ResultPrimitive>) bulkhead);

#endregion
}
44 changes: 22 additions & 22 deletions src/Polly.Specs/Caching/SerializingCacheProviderAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public void Single_generic_constructor_should_throw_on_no_wrapped_cache_provider
{
StubSerializer<object, StubSerialized> stubObjectSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => new StubSerialized(o),
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);

Action configure = () => new AsyncSerializingCacheProvider<StubSerialized>(null!, stubObjectSerializer);
Expand Down Expand Up @@ -42,7 +42,7 @@ public async Task Single_generic_SerializingCacheProvider_should_serialize_on_pu
bool serializeInvoked = false;
StubSerializer<object, StubSerialized> stubSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => { serializeInvoked = true; return new StubSerialized(o); },
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);
StubCacheProvider stubCacheProvider = new StubCacheProvider();
object objectToCache = new object();
Expand All @@ -66,7 +66,7 @@ public async Task Single_generic_SerializingCacheProvider_should_serialize_on_pu
bool serializeInvoked = false;
StubSerializer<object, StubSerialized> stubSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => { serializeInvoked = true; return new StubSerialized(o); },
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);
StubCacheProvider stubCacheProvider = new StubCacheProvider();
object? objectToCache = null;
Expand All @@ -90,7 +90,7 @@ public async Task Single_generic_SerializingCacheProvider_should_deserialize_on_
bool deserializeInvoked = false;
StubSerializer<object, StubSerialized> stubSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => new StubSerialized(o),
deserialize: s => { deserializeInvoked = true; return s.Original; }
deserialize: s => { deserializeInvoked = true; return s?.Original ?? default; }
);

var stubCacheProvider = new StubCacheProvider();
Expand All @@ -113,7 +113,7 @@ public async Task Single_generic_SerializingCacheProvider_should_not_deserialize
bool deserializeInvoked = false;
StubSerializer<object, StubSerialized> stubSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => new StubSerialized(o),
deserialize: s => { deserializeInvoked = true; return s.Original; }
deserialize: s => { deserializeInvoked = true; return s?.Original ?? default; }
);
var stubCacheProvider = new StubCacheProvider();
string key = "some key";
Expand All @@ -134,7 +134,7 @@ public async Task Single_generic_SerializingCacheProvider_from_extension_syntax_
bool serializeInvoked = false;
StubSerializer<object, StubSerialized> stubSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => { serializeInvoked = true; return new StubSerialized(o); },
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);
StubCacheProvider stubCacheProvider = new StubCacheProvider();
object objectToCache = new object();
Expand All @@ -158,7 +158,7 @@ public async Task Single_generic_SerializingCacheProvider_from_extension_syntax_
bool serializeInvoked = false;
StubSerializer<object, StubSerialized> stubSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => { serializeInvoked = true; return new StubSerialized(o); },
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);
StubCacheProvider stubCacheProvider = new StubCacheProvider();
object? objectToCache = null;
Expand All @@ -182,7 +182,7 @@ public async Task Single_generic_SerializingCacheProvider_from_extension_syntax_
bool deserializeInvoked = false;
StubSerializer<object, StubSerialized> stubSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => new StubSerialized(o),
deserialize: s => { deserializeInvoked = true; return s.Original; }
deserialize: s => { deserializeInvoked = true; return s?.Original ?? default; }
);
var stubCacheProvider = new StubCacheProvider();
object objectToCache = new object();
Expand All @@ -204,7 +204,7 @@ public async Task Single_generic_SerializingCacheProvider_from_extension_syntax_
bool deserializeInvoked = false;
StubSerializer<object, StubSerialized> stubSerializer = new StubSerializer<object, StubSerialized>(
serialize: o => new StubSerialized(o),
deserialize: s => { deserializeInvoked = true; return s.Original; }
deserialize: s => { deserializeInvoked = true; return s?.Original ?? default; }
);
var stubCacheProvider = new StubCacheProvider();
string key = "some key";
Expand All @@ -228,7 +228,7 @@ public void Double_generic_constructor_should_throw_on_no_wrapped_cache_provider
{
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => new StubSerialized<ResultPrimitive>(o),
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);

Action configure = () => new AsyncSerializingCacheProvider<ResultPrimitive, StubSerialized<ResultPrimitive>>(null!, stubTResultSerializer);
Expand Down Expand Up @@ -261,7 +261,7 @@ public async Task Double_generic_SerializingCacheProvider_should_serialize_on_pu
bool serializeInvoked = false;
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => { serializeInvoked = true; return new StubSerialized<ResultPrimitive>(o); },
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);
var stubCacheProvider = new StubCacheProvider();
ResultPrimitive objectToCache = ResultPrimitive.Good;
Expand All @@ -285,7 +285,7 @@ public async Task Double_generic_SerializingCacheProvider_should_serialize_on_pu
bool serializeInvoked = false;
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => { serializeInvoked = true; return new StubSerialized<ResultPrimitive>(o); },
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);
StubCacheProvider stubCacheProvider = new StubCacheProvider();
ResultPrimitive objectToCache = default;
Expand All @@ -309,7 +309,7 @@ public async Task Double_generic_SerializingCacheProvider_should_deserialize_on_
bool deserializeInvoked = false;
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => new StubSerialized<ResultPrimitive>(o),
deserialize: s => { deserializeInvoked = true; return s.Original; }
deserialize: s => { deserializeInvoked = true; return s?.Original ?? default; }
);
var stubCacheProvider = new StubCacheProvider();
ResultPrimitive objectToCache = ResultPrimitive.Good;
Expand All @@ -318,7 +318,7 @@ public async Task Double_generic_SerializingCacheProvider_should_deserialize_on_
AsyncSerializingCacheProvider<ResultPrimitive, StubSerialized<ResultPrimitive>> serializingCacheProvider = new AsyncSerializingCacheProvider<ResultPrimitive, StubSerialized<ResultPrimitive>>(stubCacheProvider.AsyncFor<StubSerialized<ResultPrimitive>>(), stubTResultSerializer);

await stubCacheProvider.PutAsync(key, new StubSerialized<ResultPrimitive>(objectToCache), new Ttl(TimeSpan.FromMinutes(1)), CancellationToken.None, false);
(bool cacheHit, object fromCache) = await serializingCacheProvider.TryGetAsync(key, CancellationToken.None, false);
(bool cacheHit, object? fromCache) = await serializingCacheProvider.TryGetAsync(key, CancellationToken.None, false);

cacheHit.Should().BeTrue();
deserializeInvoked.Should().BeTrue();
Expand All @@ -331,15 +331,15 @@ public async Task Double_generic_SerializingCacheProvider_should_not_deserialize
bool deserializeInvoked = false;
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => new StubSerialized<ResultPrimitive>(o),
deserialize: s => { deserializeInvoked = true; return s.Original; }
deserialize: s => { deserializeInvoked = true; return s?.Original ?? default; }
);
var stubCacheProvider = new StubCacheProvider();
string key = "some key";

stubCacheProvider.TryGet(key).Item1.Should().BeFalse();

AsyncSerializingCacheProvider<ResultPrimitive, StubSerialized<ResultPrimitive>> serializingCacheProvider = new AsyncSerializingCacheProvider<ResultPrimitive, StubSerialized<ResultPrimitive>>(stubCacheProvider.AsyncFor<StubSerialized<ResultPrimitive>>(), stubTResultSerializer);
(bool cacheHit, ResultPrimitive fromCache) = await serializingCacheProvider.TryGetAsync(key, CancellationToken.None, false);
(bool cacheHit, ResultPrimitive? fromCache) = await serializingCacheProvider.TryGetAsync(key, CancellationToken.None, false);

cacheHit.Should().BeFalse();
deserializeInvoked.Should().BeFalse();
Expand All @@ -352,7 +352,7 @@ public async Task Double_generic_SerializingCacheProvider_from_extension_syntax_
bool serializeInvoked = false;
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => { serializeInvoked = true; return new StubSerialized<ResultPrimitive>(o); },
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);
var stubCacheProvider = new StubCacheProvider();
ResultPrimitive objectToCache = ResultPrimitive.Good;
Expand All @@ -376,7 +376,7 @@ public async Task Double_generic_SerializingCacheProvider_from_extension_syntax_
bool serializeInvoked = false;
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => { serializeInvoked = true; return new StubSerialized<ResultPrimitive>(o); },
deserialize: s => s.Original
deserialize: s => s?.Original ?? default
);
StubCacheProvider stubCacheProvider = new StubCacheProvider();
ResultPrimitive objectToCache = default;
Expand All @@ -401,7 +401,7 @@ public async Task Double_generic_SerializingCacheProvider_from_extension_syntax_
bool deserializeInvoked = false;
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => new StubSerialized<ResultPrimitive>(o),
deserialize: s => { deserializeInvoked = true; return s.Original; }
deserialize: s => { deserializeInvoked = true; return s?.Original ?? default; }
);
var stubCacheProvider = new StubCacheProvider();
ResultPrimitive objectToCache = ResultPrimitive.Good;
Expand All @@ -411,7 +411,7 @@ public async Task Double_generic_SerializingCacheProvider_from_extension_syntax_
stubCacheProvider.AsyncFor<StubSerialized<ResultPrimitive>>().WithSerializer(stubTResultSerializer);

await stubCacheProvider.PutAsync(key, new StubSerialized<ResultPrimitive>(objectToCache), new Ttl(TimeSpan.FromMinutes(1)), CancellationToken.None, false);
(bool cacheHit, ResultPrimitive fromCache) = await serializingCacheProvider.TryGetAsync(key, CancellationToken.None, false);
(bool cacheHit, ResultPrimitive? fromCache) = await serializingCacheProvider.TryGetAsync(key, CancellationToken.None, false);

cacheHit.Should().BeTrue();
deserializeInvoked.Should().BeTrue();
Expand All @@ -424,7 +424,7 @@ public async Task Double_generic_SerializingCacheProvider_from_extension_syntax_
bool deserializeInvoked = false;
StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>> stubTResultSerializer = new StubSerializer<ResultPrimitive, StubSerialized<ResultPrimitive>>(
serialize: o => new StubSerialized<ResultPrimitive>(o),
deserialize: s => { deserializeInvoked = true; return s.Original; }
deserialize: s => { deserializeInvoked = true; return s?.Original ?? default; }
);
var stubCacheProvider = new StubCacheProvider();
string key = "some key";
Expand All @@ -433,7 +433,7 @@ public async Task Double_generic_SerializingCacheProvider_from_extension_syntax_

AsyncSerializingCacheProvider<ResultPrimitive, StubSerialized<ResultPrimitive>> serializingCacheProvider =
stubCacheProvider.AsyncFor<StubSerialized<ResultPrimitive>>().WithSerializer(stubTResultSerializer);
(bool cacheHit, ResultPrimitive fromCache) = await serializingCacheProvider.TryGetAsync(key, CancellationToken.None, false);
(bool cacheHit, ResultPrimitive? fromCache) = await serializingCacheProvider.TryGetAsync(key, CancellationToken.None, false);

cacheHit.Should().BeFalse();
deserializeInvoked.Should().BeFalse();
Expand Down
Loading

0 comments on commit 7cf4b58

Please sign in to comment.