Skip to content

Implement dependsOn for cross-container dependencies - #1404

Merged
bsideup merged 7 commits into
masterfrom
dependsOn
Jul 23, 2019
Merged

bsideup merged 7 commits into
masterfrom
dependsOn

Conversation

@bsideup

@bsideup bsideup commented Apr 15, 2019

Copy link
Copy Markdown
Member

No description provided.

@bsideup
bsideup requested a review from a team April 15, 2019 09:05
@aguibert

aguibert commented May 8, 2019

Copy link
Copy Markdown
Contributor

@bsideup instead of depending on containers with the actual Starable instances, could we perhaps introduce some sort of naming mechanism for containers, and then we could simply do dependsOn(String)? This would be nice because we wouldn't have to swap from field-based declarations to a static init block.

An example of what I had in mind:

public class MyTest {

  @Container
  public static GenericContainer a = new GenericContainer(...).withID("a");

  @Container
  public static GenericContainer b = new GenericContainer(...).dependsOn("a");

}

WDYT?

@bsideup

bsideup commented May 8, 2019 •

Copy link
Copy Markdown
Member Author

@aguibert I have a really bad feeling about it.
References give strong guarantees on what user refers, while strings can be anything, they are global (imagine somebody defining some generic name like "kafka" in a big code base?) and in general are less safe.

This would be nice because we wouldn't have to swap from field-based declarations to a static init block.

dependsOn does not rely on the static blocks or anything and works with fields/rules too:

public class MyTest {

  @Container
  public static GenericContainer a = new GenericContainer(...);

  @Container
  public static GenericContainer b = new GenericContainer(...).dependsOn(a);

}

@aguibert

aguibert commented May 8, 2019

Copy link
Copy Markdown
Contributor

ah, that code example you posted makes sense. I forgot that was an option because I'm used to not being able to reference other container fields like this:

public class MyTest {

  @Container
  public static GenericContainer a = new GenericContainer(...);

  @Container
  public static GenericContainer b = new GenericContainer(...)
                  .withEnv("A_PORT", a.getFirstMappedPort()); // blows up because we call `getFirstMappedPort()` too early -- before `a` is started and has its port assigned.

}

@bsideup
bsideup marked this pull request as ready for review July 19, 2019 13:36
@bsideup
bsideup requested review from kiview and rnorth as code owners July 19, 2019 13:36
@bsideup bsideup added this to the next milestone Jul 21, 2019

@rnorth rnorth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I have some minor comments but nothing big; feel free to merge when you're satisfied.

}

VisibleAssertions.assertEquals("Started once", 1, startable.getStartInvocationCount().intValue());
VisibleAssertions.assertEquals("Does not trigger .stop()", 0, startable.getStopInvocationCount().intValue());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there's a good reason, but why do we not propagate a stop() call through the dependencies?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because there is no guarantee that the dependencies will be a part of the same "group".

We may introduce deepStop in future

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a deepStop would be useful for when you know these are the semantics you want. I end up having to manually create one in several projects. Thoughts on adding an API?

Btw, are both stop and start required to be idempotent? I think the current implementation of deepStart depends on this.

}

@Test
public void shouldStartTransitiveDependencies() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just eyeballing the code, it looks like we're safe from circular dependencies, and also 'diamond' dependencies should be OK too.

Do you think it might be good to have tests for these scenarios in case a regression is introduced later?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circular dependencies won't work (we have "hard depends on")

I will add a test for the diamond case 👍

@rnorth rnorth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@bsideup
bsideup merged commit 1d686a1 into master Jul 23, 2019
@delete-merged-branch
delete-merged-branch Bot deleted the dependsOn branch July 23, 2019 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants