Skip to content

Commit

Permalink
FIX: some issues with priority not being propagated to components
Browse files Browse the repository at this point in the history
causing issues with components ordering
  • Loading branch information
xhanin committed Oct 10, 2014
1 parent bea3aad commit da0a80d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public <T> MachineEngine<T> getEngine(Name<T> name) {
if (!canBuild(name)) {
throw new IllegalArgumentException("unsuported name " + name);
}
return (MachineEngine<T>) new StdMachineEngine<RestxConfig>((Name<RestxConfig>) name, BoundlessComponentBox.FACTORY) {
return (MachineEngine<T>) new StdMachineEngine<RestxConfig>((Name<RestxConfig>) name, priority(), BoundlessComponentBox.FACTORY) {

private final Factory.Query<ConfigSupplier> configSupplierQuery = Factory.Query.byClass(ConfigSupplier.class);
private final Factory.Query<String> stringsQuery = Factory.Query.byClass(String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public AlternativesFactoryMachine(int priority,
final ImmutableMap<T, ? extends FactoryMachine> alternatives,
ComponentBox.BoxFactory boxFactory) {
super(priority, new StdMachineEngine<FactoryMachine>(
Name.of(FactoryMachine.class, name.getName() + "Alternatives"), boxFactory) {
Name.of(FactoryMachine.class, name.getName() + "Alternatives"), priority, boxFactory) {
private Factory.Query<T> query = Factory.Query.byName(name);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SingletonFactoryMachine<C> extends SingleNameFactoryMachine<C> {
private final NamedComponent<C> component;

public SingletonFactoryMachine(int priority, final NamedComponent<C> component) {
super(priority, new NoDepsMachineEngine<C>(component.getName(), BoundlessComponentBox.FACTORY) {
super(priority, new NoDepsMachineEngine<C>(component.getName(), priority, BoundlessComponentBox.FACTORY) {
@Override
public C doNewComponent(SatisfiedBOM satisfiedBOM) {
return component.getComponent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public boolean canBuild(Name<?> name) {
@Override
@SuppressWarnings("unchecked")
public <T> MachineEngine<T> getEngine(final Name<T> name) {
return new NoDepsMachineEngine<T>(name, BoundlessComponentBox.FACTORY) {
return new NoDepsMachineEngine<T>(name, priority(), BoundlessComponentBox.FACTORY) {
@Override
protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
// in case the system property has been nullified, we return an empty string as value, rather than null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class {{machine}} extends SingleNameFactoryMachine<FactoryMachine> {
if (satisfiedBOM.getOne(query).isPresent()
&& satisfiedBOM.getOne(query).get().getComponent().equals("{{whenValue}}")) {
return new SingleNameFactoryMachine<{{alternativeToComponentType}}>({{priority}},
new StdMachineEngine<{{alternativeToComponentType}}>(NAME, 0, BoundlessComponentBox.FACTORY) {
new StdMachineEngine<{{alternativeToComponentType}}>(NAME, {{priority}}, BoundlessComponentBox.FACTORY) {
{{queriesDeclarations}}

@Override
Expand Down
25 changes: 25 additions & 0 deletions restx-factory/src/test/java/restx/factory/FactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ public void should_start_auto_startable() throws Exception {
assertThat(lifecycleComponent.started).isTrue();
}

@Test
public void should_start_auto_startable_in_order() throws Exception {
final LifecycleComponent lifecycleComponent1 = new LifecycleComponent();
final boolean[] l2Started = new boolean[1];
final boolean[] wasL1Started = new boolean[1];
Factory factory = Factory.builder()
.addMachine(new SingletonFactoryMachine<>(10, NamedComponent.of(
AutoStartable.class, "t0", new AutoStartable() {
@Override
public void start() {
wasL1Started[0] = lifecycleComponent1.started;
l2Started[0] = true;
}
}
)))
.addMachine(new SingletonFactoryMachine<>(0, NamedComponent.of(
AutoStartable.class, "t1", lifecycleComponent1)))
.build();
factory.start();

assertThat(lifecycleComponent1.started).isTrue();
assertThat(l2Started[0]).isTrue();
assertThat(wasL1Started[0]).isTrue();
}

@Test
public void should_prepare_auto_preparable() throws Exception {
LifecycleComponent lifecycleComponent = new LifecycleComponent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean canBuild(Name<?> name) {

@Override
public <T> MachineEngine<T> getEngine(final Name<T> name) {
return new StdMachineEngine<T>(name, BoundlessComponentBox.FACTORY) {
return new StdMachineEngine<T>(name, priority(), BoundlessComponentBox.FACTORY) {
@Override
@SuppressWarnings("unchecked")
protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
Expand Down

0 comments on commit da0a80d

Please sign in to comment.