-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix FastLazy race condition and waiting thread hanging (#6336) #6707
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
488056f
Fix FastLazy race condition and waiting thread hanging (#6336).
F0b0s b2bdec5
Move properties to methods.
F0b0s b036470
Fix verified file.
F0b0s e0bc2e8
Merge branch 'dev' into fast_lazy_race_condition
Aaronontheweb 9b10ee6
Merge branch 'dev' into fast_lazy_race_condition
F0b0s f16f1e3
Fix verified file.
F0b0s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="FastLazySpecs.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Util; | ||
using Xunit; | ||
|
||
namespace Akka.Tests.Util; | ||
|
||
public class FastLazySpecs | ||
{ | ||
[Fact] | ||
public void FastLazy_should_indicate_no_value_has_been_produced() | ||
{ | ||
var fal = new FastLazy<int>(() => 2); | ||
Assert.False(fal.IsValueCreated()); | ||
} | ||
|
||
[Fact] | ||
public void FastLazy_should_produce_value() | ||
{ | ||
var fal = new FastLazy<int>(() => 2); | ||
var value = fal.Value; | ||
Assert.Equal(2, value); | ||
Assert.True(fal.IsValueCreated()); | ||
} | ||
|
||
[Fact] | ||
public void FastLazy_must_be_threadsafe() | ||
{ | ||
for (var c = 0; c < 100000; c++) // try this 100000 times | ||
{ | ||
var values = new ConcurrentBag<int>(); | ||
var fal = new FastLazy<int>(() => new Random().Next(1, Int32.MaxValue)); | ||
var result = Parallel.For(0, 1000, i => values.Add(fal.Value)); // 1000 concurrent operations | ||
SpinWait.SpinUntil(() => result.IsCompleted); | ||
var value = values.First(); | ||
Assert.NotEqual(0, value); | ||
Assert.True(values.All(x => x.Equals(value))); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void FastLazy_only_single_value_creation_attempt() | ||
{ | ||
int attempts = 0; | ||
Func<int> slowValueFactory = () => | ||
{ | ||
Interlocked.Increment(ref attempts); | ||
Thread.Sleep(100); | ||
return new Random().Next(1, Int32.MaxValue); | ||
}; | ||
|
||
var values = new ConcurrentBag<int>(); | ||
var fal = new FastLazy<int>(slowValueFactory); | ||
var result = Parallel.For(0, 1000, i => values.Add(fal.Value)); // 1000 concurrent operations | ||
SpinWait.SpinUntil(() => result.IsCompleted); | ||
var value = values.First(); | ||
Assert.NotEqual(0, value); | ||
Assert.True(values.All(x => x.Equals(value))); | ||
Assert.Equal(1000, values.Count); | ||
Assert.Equal(1, attempts); | ||
} | ||
|
||
[Fact] | ||
public void FastLazy_must_be_threadsafe_AnyRef() | ||
{ | ||
for (var c = 0; c < 100000; c++) // try this 100000 times | ||
{ | ||
var values = new ConcurrentBag<string>(); | ||
var fal = new FastLazy<string>(() => Guid.NewGuid().ToString()); | ||
var result = Parallel.For(0, 1000, i => values.Add(fal.Value)); // 1000 concurrent operations | ||
SpinWait.SpinUntil(() => result.IsCompleted); | ||
var value = values.First(); | ||
Assert.NotNull(value); | ||
Assert.True(values.All(x => x.Equals(value))); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void FastLazy_only_single_value_creation_attempt_AnyRef() | ||
{ | ||
int attempts = 0; | ||
Func<string> slowValueFactory = () => | ||
{ | ||
Interlocked.Increment(ref attempts); | ||
Thread.Sleep(100); | ||
return Guid.NewGuid().ToString(); | ||
}; | ||
|
||
var values = new ConcurrentBag<string>(); | ||
var fal = new FastLazy<string>(slowValueFactory); | ||
var result = Parallel.For(0, 1000, i => values.Add(fal.Value)); // 1000 concurrent operations | ||
SpinWait.SpinUntil(() => result.IsCompleted); | ||
var value = values.First(); | ||
Assert.NotNull(value); | ||
Assert.True(values.All(x => x.Equals(value))); | ||
Assert.Equal(1000, values.Count); | ||
Assert.Equal(1, attempts); | ||
} | ||
|
||
[Fact] | ||
public void FastLazy_AllThreads_ShouldThrowException_WhenFactoryThrowsException() | ||
{ | ||
var lazy = new FastLazy<string>(() => throw new Exception("Factory exception")); | ||
var result = Parallel.For(0, 10, i => { Assert.Throws<Exception>(() => _ = lazy.Value); }); | ||
|
||
Assert.True(result.IsCompleted); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to method because with
[MethodImpl(MethodImplOptions.AggressiveInlining)]
it shows slightly better performance