File tree Expand file tree Collapse file tree 3 files changed +24
-3
lines changed
test/java/com/amazon/aoc/helpers Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Original file line number Diff line number Diff line change 55import java .util .List ;
66
77public final class SortUtils {
8+ private static final int MAX_RESURSIVE_DEPTH = 10 ;
89
910 /**
1011 * Given a list of entities, which are X-Ray segments or subsegments, recursively sort each of
@@ -13,13 +14,18 @@ public final class SortUtils {
1314 * @param entities - list of X-Ray entities to sort recursively. Modified in place.
1415 */
1516 public static void recursiveEntitySort (List <Entity > entities ) {
16- if (entities == null || entities .size () == 0 ) {
17+ recursiveEntitySort (entities , 0 );
18+ }
19+
20+ private static void recursiveEntitySort (List <Entity > entities , int depth ) {
21+ if (entities == null || entities .size () == 0 || depth >= MAX_RESURSIVE_DEPTH ) {
1722 return ;
1823 }
24+ int currDepth = depth + 1 ;
1925
2026 for (Entity entity : entities ) {
21- if (entity .getSubsegments () != null && entity .getSubsegments ().size () > 1 ) {
22- recursiveEntitySort (entity .getSubsegments ());
27+ if (entity .getSubsegments () != null && ! entity .getSubsegments ().isEmpty () ) {
28+ recursiveEntitySort (entity .getSubsegments (), currDepth );
2329 }
2430 }
2531
Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ public class Entity {
3737 private boolean inProgress ;
3838 @ JsonInclude (JsonInclude .Include .NON_DEFAULT )
3939 private boolean inferred ;
40+ @ JsonInclude (JsonInclude .Include .NON_DEFAULT )
41+ private boolean stubbed ;
4042
4143 private String namespace ;
4244
Original file line number Diff line number Diff line change @@ -68,6 +68,19 @@ public void testNestedEntitySort() {
6868 assertThat (bottomEntities ).containsSequence (generated .get (6 ), generated .get (7 ));
6969 }
7070
71+ @ Test
72+ public void testInfiniteLoop () {
73+ Entity current = new Entity ();
74+ List <Entity > entityList = new ArrayList <>();
75+ entityList .add (current );
76+ current .setSubsegments (entityList ); // set up an infinite children loop
77+
78+ SortUtils .recursiveEntitySort (entityList );
79+
80+ // Not really testing anything, just making sure we don't infinite loop
81+ assertThat (entityList ).hasSize (1 );
82+ }
83+
7184 private List <Entity > generateEntities (int n ) {
7285 List <Entity > ret = new ArrayList <>();
7386
You can’t perform that action at this time.
0 commit comments