Patrick Hunt <ph...@apache.org> writes:
> See the quorum tests for examples of how to do this, in particular
> take a look at org.apache.zookeeper.test.QuorumBase which is how we
> start/stop quorum servers in some of our tests. Our tests can run in
> "fork once" mode in junit. Meaning that all the tests
> (starting/stopping servers frequently) run in the same jvm, your
> embedded case.
a good reason to want to embed ZooKeeper is for unit tests. I had a
look at the tests that came with ZooKeeper and I am not overly fond of
having to embed a ZooKeeper ensemble by fiddling with the implementation
classes. also, I think it is a bad idea to base a project's test on
extending test base-classes. this was okay in JUnit 3, but if you are
used to JUnit 4 it is a bit ugly.
when I run embedded servers in unit tests I allocate both temporary
directories and unique ports as part of the test setup. for the most(*)
part I use the @Rule TemporaryFolder for creating temporary directories
that are cleaned up when the test is done. ports are allocated by a
utility I've written that reduces the chance of grabbing ports that are
already in use.
it would be beneficial if it was possible to configure ZooKeeperServer
instances and have them work together in an ensemble (within a JVM)
without having to resort to hacking the implementation classes or
extending test base-classes (yech!).
(*) for versions of JUnit I've found in Maven Central
@Rule TemporaryFolder doesn't work properly with @BeforeClass, so it
isn't possible to use @Rule TemporaryFolder to manage temporary folders
that are needed by broken server classes (some servers have lots of
badly designed singletons that requires you to run tests in separate
threads). In these cases I use a somewhat inferior test utility that
basically does the same as TemporaryFolder.