Skip to content

Commit

Permalink
Fix incorrect reporting of fetched from cache in async.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCiliaVincenti committed Jan 1, 2024
1 parent 929a96a commit d9beccd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
9 changes: 5 additions & 4 deletions OpenWeatherMap.Cache.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task TestConcurrency()
for (int i = 1; i <= tries; i++)
{
tasks = Enumerable.Range(0, concurrency)
.Select(async i =>
.Select(async (i) =>
{
var location = new Models.Location(48.6371, -122.1237);
var readings = openWeatherMapCache.GetReadings(location);
Expand Down Expand Up @@ -74,8 +74,8 @@ public async Task TestConcurrency()
public async Task TestConcurrencyAsync()
{
int apiCachePeriod = 1_000;
int concurrency = 100;
int tries = 5;
int concurrency = 2;
int tries = 1;

var openWeatherMapCache = new OpenWeatherMapCache(_apiKey, apiCachePeriod);
int totalFromCache = 0;
Expand All @@ -87,7 +87,7 @@ public async Task TestConcurrencyAsync()
for (int i = 1; i <= tries; i++)
{
tasks = Enumerable.Range(0, concurrency)
.Select(async i =>
.Select(async (i) =>
{
var location = new Models.Location(48.6371, -122.1237);
var readings = await openWeatherMapCache.GetReadingsAsync(location);
Expand All @@ -103,6 +103,7 @@ public async Task TestConcurrencyAsync()
{
Interlocked.Increment(ref totalFromAPI);
}
await Task.Delay(0);
}).ToList().AsParallel();
await Task.WhenAll(tasks);

Expand Down
2 changes: 1 addition & 1 deletion OpenWeatherMap.Cache.Tests/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"apiKey": "[API Key]"
}
}
10 changes: 5 additions & 5 deletions OpenWeatherMap.Cache/OpenWeatherMap.Cache.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
<PackageProjectUrl>https://github.com/MarkCiliaVincenti/OpenWeatherMap.Cache</PackageProjectUrl>
<Copyright>MIT</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>1.9.14</Version>
<Version>1.10.0</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageReleaseNotes>Dependency update.</PackageReleaseNotes>
<PackageReleaseNotes>Fix incorrect reporting of fetched from cache in async.</PackageReleaseNotes>
<Description>An asynchronous .NET Standard 2.0 library that allows you to fetch &amp; cache current weather readings from the OpenWeather API, with built-in resiliency that can extend the cache lifetime in case the API is unreachable.</Description>
<Copyright2023 Mark Cilia Vincenti</Copyright>
<Copyright2024 Mark Cilia Vincenti</Copyright>
<PackageTags>OpenWeather,OpenWeatherMap,cache,weather,API,.NET Standard,netstandard</PackageTags>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<AssemblyVersion>1.9.14.0</AssemblyVersion>
<FileVersion>1.9.14.0</FileVersion>
<AssemblyVersion>1.10.0.0</AssemblyVersion>
<FileVersion>1.10.0.0</FileVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IsPackable>true</IsPackable>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
Expand Down
3 changes: 2 additions & 1 deletion OpenWeatherMap.Cache/OpenWeatherMapCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OpenWeatherMap.Cache.Constants;
using OpenWeatherMap.Cache.Models;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -290,7 +291,7 @@ public async ValueTask<Readings> GetReadingsAsync<T>(T locationQuery, Cancellati

try
{
var apiWeatherResult = await GetApiWeatherResultFromLocationQueryAsync(locationQuery, cancellationToken).ConfigureAwait(false);
var apiWeatherResult = await GetApiWeatherResultFromLocationQueryAsync(locationQuery, cancellationToken).ConfigureAwait(true);
var newValue = new Readings(apiWeatherResult)
{
ApiRequestMade = true
Expand Down

0 comments on commit d9beccd

Please sign in to comment.