@@ -923,6 +923,48 @@ public void BuildDfaTree_WithPolicies_AndBranches_BothPoliciesSkipped()
923
923
Assert . Null ( a . PolicyEdges ) ;
924
924
}
925
925
926
+ // Verifies that we sort the endpoints before calling into policies.
927
+ //
928
+ // The builder uses a different sort order when building the tree, vs when building the policy nodes. Policy
929
+ // nodes should see an "absolute" order.
930
+ [ Fact ]
931
+ public void BuildDfaTree_WithPolicies_SortedAccordingToScore ( )
932
+ {
933
+ // Arrange
934
+ //
935
+ // These cases where chosen where the absolute order incontrolled explicitly by setting .Order, but
936
+ // the precedence of segments is different, so these will be sorted into different orders when building
937
+ // the tree.
938
+ var policies = new MatcherPolicy [ ]
939
+ {
940
+ new TestMetadata1MatcherPolicy ( ) ,
941
+ new TestMetadata2MatcherPolicy ( ) ,
942
+ } ;
943
+
944
+ var builder = CreateDfaMatcherBuilder ( policies ) ;
945
+
946
+ ( ( TestMetadata1MatcherPolicy ) policies [ 0 ] ) . OnGetEdges = VerifyOrder ;
947
+ ( ( TestMetadata2MatcherPolicy ) policies [ 1 ] ) . OnGetEdges = VerifyOrder ;
948
+
949
+ var endpoint1 = CreateEndpoint ( "/a/{**b}" , order : - 1 , metadata : new object [ ] { new TestMetadata1 ( 0 ) , new TestMetadata2 ( true ) , } ) ;
950
+ builder . AddEndpoint ( endpoint1 ) ;
951
+
952
+ var endpoint2 = CreateEndpoint ( "/a/{b}/{**c}" , order : 0 , metadata : new object [ ] { new TestMetadata1 ( 1 ) , new TestMetadata2 ( true ) , } ) ;
953
+ builder . AddEndpoint ( endpoint2 ) ;
954
+
955
+ var endpoint3 = CreateEndpoint ( "/a/b/{c}" , order : 1 , metadata : new object [ ] { new TestMetadata1 ( 1 ) , new TestMetadata2 ( false ) , } ) ;
956
+ builder . AddEndpoint ( endpoint3 ) ;
957
+
958
+ // Act & Assert
959
+ _ = builder . BuildDfaTree ( ) ;
960
+
961
+ void VerifyOrder ( IReadOnlyList < Endpoint > endpoints )
962
+ {
963
+ // The list should already be in sorted order, every time build is called.
964
+ Assert . Equal ( endpoints , endpoints . OrderBy ( e => e , builder . Comparer ) ) ;
965
+ }
966
+ }
967
+
926
968
[ Fact ]
927
969
public void BuildDfaTree_RequiredValues ( )
928
970
{
@@ -1501,6 +1543,8 @@ private class TestMetadata1MatcherPolicy : MatcherPolicy, IEndpointComparerPolic
1501
1543
1502
1544
public IComparer < Endpoint > Comparer => EndpointMetadataComparer < TestMetadata1 > . Default ;
1503
1545
1546
+ public Action < IReadOnlyList < Endpoint > > OnGetEdges { get ; set ; }
1547
+
1504
1548
public bool AppliesToEndpoints ( IReadOnlyList < Endpoint > endpoints )
1505
1549
{
1506
1550
return endpoints . Any ( e => e . Metadata . GetMetadata < TestMetadata1 > ( ) != null ) ;
@@ -1513,6 +1557,7 @@ public PolicyJumpTable BuildJumpTable(int exitDestination, IReadOnlyList<PolicyJ
1513
1557
1514
1558
public IReadOnlyList < PolicyNodeEdge > GetEdges ( IReadOnlyList < Endpoint > endpoints )
1515
1559
{
1560
+ OnGetEdges ? . Invoke ( endpoints ) ;
1516
1561
return endpoints
1517
1562
. GroupBy ( e => e . Metadata . GetMetadata < TestMetadata1 > ( ) . State )
1518
1563
. Select ( g => new PolicyNodeEdge ( g . Key , g . ToArray ( ) ) )
@@ -1540,6 +1585,9 @@ private class TestMetadata2MatcherPolicy : MatcherPolicy, IEndpointComparerPolic
1540
1585
1541
1586
public IComparer < Endpoint > Comparer => EndpointMetadataComparer < TestMetadata2 > . Default ;
1542
1587
1588
+ public Action < IReadOnlyList < Endpoint > > OnGetEdges { get ; set ; }
1589
+
1590
+
1543
1591
public bool AppliesToEndpoints ( IReadOnlyList < Endpoint > endpoints )
1544
1592
{
1545
1593
return endpoints . Any ( e => e . Metadata . GetMetadata < TestMetadata2 > ( ) != null ) ;
@@ -1552,6 +1600,7 @@ public PolicyJumpTable BuildJumpTable(int exitDestination, IReadOnlyList<PolicyJ
1552
1600
1553
1601
public IReadOnlyList < PolicyNodeEdge > GetEdges ( IReadOnlyList < Endpoint > endpoints )
1554
1602
{
1603
+ OnGetEdges ? . Invoke ( endpoints ) ;
1555
1604
return endpoints
1556
1605
. GroupBy ( e => e . Metadata . GetMetadata < TestMetadata2 > ( ) . State )
1557
1606
. Select ( g => new PolicyNodeEdge ( g . Key , g . ToArray ( ) ) )
0 commit comments