Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make constraint cache thread safe (#17146)
* Make constraint cache thread safe Fixes: #17101 This changes the constraint cache to use ConcurrentDictionary. This code is invoked in a multithreaded way in Blazor server resulting in internal failures in dictionary. Since this is a threading issue there's no good way to unit test it, but I noticed we're missing tests in general for this class, so I added a few for the caching behavior. * PR feedback
- Loading branch information
Showing
2 changed files
with
48 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
36 changes: 36 additions & 0 deletions
36
src/Components/Components/test/Routing/RouteConstraintTest.cs
This file contains 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,36 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Components.Routing | ||
{ | ||
public class RouteConstraintTest | ||
{ | ||
[Fact] | ||
public void Parse_CreatesDifferentConstraints_ForDifferentKinds() | ||
{ | ||
// Arrange | ||
var original = RouteConstraint.Parse("ignore", "ignore", "int"); | ||
|
||
// Act | ||
var another = RouteConstraint.Parse("ignore", "ignore", "guid"); | ||
|
||
// Assert | ||
Assert.NotSame(original, another); | ||
} | ||
|
||
[Fact] | ||
public void Parse_CachesCreatedConstraint_ForSameKind() | ||
{ | ||
// Arrange | ||
var original = RouteConstraint.Parse("ignore", "ignore", "int"); | ||
|
||
// Act | ||
var another = RouteConstraint.Parse("ignore", "ignore", "int"); | ||
|
||
// Assert | ||
Assert.Same(original, another); | ||
} | ||
} | ||
} |