You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I am using the latest LazyCache master branch and am having trouble with setting AbsoluteExpirationRelativeToNow.
Calling GetOrAddAsync with a long running task does not properly set AbsoluteExpiration. For the unit test added in #125 the AbsoluteExpirationRelativeToNow is set at the beginning of the task, so is likely to succeed. However, if addItemFactory is a longer running task (e.g. calling database or API) and then the AbsoluteExpirationRelativeToNow is set, the system will not set AbsoluteExpiration.
To Reproduce
[Fact]publicvoidGetOrAdd(){varkey="test";varexpected=_cache.GetOrAdd(key, entry =>{Thread.Sleep(300);entry.AbsoluteExpirationRelativeToNow=TimeSpan.FromSeconds(5);return"Value";});varactual=_cache.Get<string>(key);Assert.NotNull(actual);Assert.Equal(expected,actual);Thread.Sleep(5500);// Will return null here, since GetOrAdd runs addItemFactory synchronously.varvalue=_cache.Get<string>(key);Assert.Null(value);}[Fact]publicasyncTaskGetOrAddAsync(){varkey="test";varexpected=await_cache.GetOrAddAsync(key,async entry =>{awaitTask.Delay(300);entry.AbsoluteExpirationRelativeToNow=TimeSpan.FromSeconds(5);return"Value";});varactual=_cache.Get<string>(key);Assert.NotNull(actual);Assert.Equal(expected,actual);Thread.Sleep(5500);// Will return "Value" here, since GetOrAddAsync runs addItemFactory asynchronously and the task does not immediately set AbsoluteExpirationRelativeToNow.varvalue=_cache.Get<string>(key);Assert.Null(value);}
Expected behavior
SetAbsoluteExpirationFromRelative should be called after addItemFactory is evaluated in GetOrAddAsync.
The text was updated successfully, but these errors were encountered:
…ExpirationRelativeToNow (#160)
* fix: await addItemFactory before calling SetAbsoluteExpirationFromRelative
allows AbsoluteExpiration to be set properly when factories are long-running and a relative expiration is specified
fixes#148
* test: verify that AbsoluteExpiration is set properly from a relative expiration when a factory is long-running
* test: improve unit test names
Describe the bug
I am using the latest LazyCache master branch and am having trouble with setting AbsoluteExpirationRelativeToNow.
Calling GetOrAddAsync with a long running task does not properly set AbsoluteExpiration. For the unit test added in #125 the AbsoluteExpirationRelativeToNow is set at the beginning of the task, so is likely to succeed. However, if addItemFactory is a longer running task (e.g. calling database or API) and then the AbsoluteExpirationRelativeToNow is set, the system will not set AbsoluteExpiration.
To Reproduce
Expected behavior
SetAbsoluteExpirationFromRelative should be called after addItemFactory is evaluated in GetOrAddAsync.
The text was updated successfully, but these errors were encountered: