-
Notifications
You must be signed in to change notification settings - Fork 106
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
Guarantee initial order for decorators to sort #1154
Merged
Merged
Changes from all commits
Commits
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 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 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
129 changes: 129 additions & 0 deletions
129
core/src/test/java/io/dekorate/utils/TopologicalSortTest.java
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,129 @@ | ||
package io.dekorate.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.dekorate.LoggerFactory; | ||
import io.dekorate.kubernetes.decorator.Decorator; | ||
import io.dekorate.logger.NoopLogger; | ||
|
||
public class TopologicalSortTest { | ||
|
||
private List<Decorator> sortedDecorators; | ||
|
||
@BeforeAll | ||
public static void initLogger() { | ||
LoggerFactory.setLogger(new NoopLogger()); | ||
} | ||
|
||
@BeforeEach | ||
public void setup() { | ||
sortedDecorators = null; | ||
} | ||
|
||
@Test | ||
public void shouldGroupDecoratorsWithSameTypeAndShouldPreserveOrder() { | ||
FooDecorator a1 = new FooDecorator(); | ||
DummyDecorator b = new DummyDecorator().withAfter(FooDecorator.class); | ||
FooDecorator a2 = new FooDecorator(); | ||
|
||
whenSort(b, a1, a2); | ||
thenDecoratorsAre(a1, a2, b); | ||
} | ||
|
||
@Test | ||
public void shouldThrowCycleException() { | ||
FooDecorator foo = new FooDecorator(); | ||
DummyDecorator bar = new BarDecorator().withAfter(FooDecorator.class); | ||
DummyDecorator special = new SpecialFooDecorator().withAfter(FooDecorator.class, BarDecorator.class); | ||
|
||
// it should fail because: | ||
// bar -> foo | ||
// special (it's also a foo) -> bar | ||
assertThrows(RuntimeException.class, () -> whenSort(foo, bar, special)); | ||
} | ||
|
||
@Test | ||
public void shouldAvoidCycleErrorWhenDecoratorHasSameParent() { | ||
FooDecorator foo = new FooDecorator(); | ||
DummyDecorator bar = new BarDecorator().withBefore(SpecialFooDecorator.class).withAfter(FooDecorator.class); | ||
DummyDecorator special = new SpecialFooDecorator().withAfter(FooDecorator.class, BarDecorator.class); | ||
|
||
whenSort(foo, bar, special); | ||
thenDecoratorsAre(foo, bar, special); | ||
} | ||
|
||
private void whenSort(Decorator decorator, Decorator... decorators) { | ||
List<Decorator> unsortedDecorators = new ArrayList<>(); | ||
unsortedDecorators.add(decorator); | ||
if (decorators != null) { | ||
for (Decorator rest : decorators) { | ||
unsortedDecorators.add(rest); | ||
} | ||
} | ||
|
||
sortedDecorators = TopologicalSort.sortDecorators(unsortedDecorators); | ||
} | ||
|
||
private void thenDecoratorsAre(Decorator... expectedDecorators) { | ||
for (int position = 0; position < expectedDecorators.length; position++) { | ||
Decorator expected = expectedDecorators[position]; | ||
assertEquals(sortedDecorators.get(position), expected, "Unexpected order in decorators: " + sortedDecorators); | ||
} | ||
} | ||
|
||
static class FooDecorator extends DummyDecorator { | ||
|
||
} | ||
|
||
static class SpecialFooDecorator extends FooDecorator { | ||
|
||
} | ||
|
||
static class BarDecorator extends DummyDecorator { | ||
|
||
} | ||
|
||
static class DummyDecorator extends Decorator { | ||
|
||
private Class[] before; | ||
private Class[] after; | ||
|
||
public DummyDecorator withAfter(Class... classes) { | ||
after = classes; | ||
return this; | ||
} | ||
|
||
public DummyDecorator withBefore(Class... classes) { | ||
before = classes; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void visit(Object o) { | ||
|
||
} | ||
|
||
@Override | ||
public int compareTo(Object o) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public Class<? extends Decorator>[] before() { | ||
return before; | ||
} | ||
|
||
@Override | ||
public Class<? extends Decorator>[] after() { | ||
return after; | ||
} | ||
} | ||
} |
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.
It's not clear to me what you mean by initial order.
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.
The idea is to get a determinist order if we always get the same input. For example, if our input is a set of three decorators with no dependencies on each other, the final result order of these three decorators might vary in different executions.
The idea I had to address this situation is to introduce an initial order based on the decorator class name instead of the decorator order insertion. Therefore, and following the above example, the three decorators will be ordered by the decorator name and hence the final result order will now always be the same.
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.
The order of decorators should not be important to us as long as they are topologically sorted. Even if it's possible the get different results that both satisfy the constraints, the end result (the generated json/yaml files) should be deterministic. If the end result is not deterministic, then we probably have not properly expressed those constraints and we should focus on that.
TDLR: I don't mind having the decorators in a predictable order however, we shouldn't have to, are we sure we are dealing with the issue at the right level?