You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by Todd Nine <to...@spidertracks.co.nz> on 2010/08/26 06:06:18 UTC

Integration/Unit testing with Zookeeper

Hi all,
  I've build my leader election watcher and I'm attempting to
integration test it.  I'm having a lot of issues in regards to old
watchers still running.  Here are my test cases (in order of execution).


1. Single watcher is first to join ZK, becomes the leader.


2. watcher1 joins, watcher 2 joins.  watcher1 is leader


3. watcher1 joins, watcher 2 joins.  watcher 1 is leader.  Kill watcher
1, watcher 2 becomes leader


The issue is that I have 3 test methods in my test class.  Each one
seems to still have watchers running from previous text case executions.
As a result, my test cases aren't atomic.  I've tried adding the
zk.close() call to the end of each of my tests.  However I'm still not
getting my ephemeral nodes expiring.  I also perform sleeps with my
timeouts in my tests to make sure that ZK times out the connection to
old clients.  Any ideas what I can do to clean up integration testing
this?



public class ElectionWatcherTest extends ZooKeeperTest {

	
	/**
	 * Simple test case that checks we're the leader when it's the first
Node
	 * 
	 * @throws Exception
	 */
	@Test
	public void firstNode() throws Exception {

		ILeaderWatcher leaderWatcher = mock(ILeaderWatcher.class);

		ElectionWatcher watcher = new ElectionWatcher();
		watcher.setConnectionFactory(connectionFactory);
		watcher.setLeaderWatcher(leaderWatcher);

		// now start the object for election process
		watcher.initialize();

		// wait 2 times as long as the timeout for election to happen
		Thread.sleep(timeout * 2);

		watcher.shutdown();
		
		// verify that the leaderWatcher was called as the leader
		verify(leaderWatcher).setAsLeader();
		
		

	}

	/**
	 * Simple test case that checks we're the leader when it's the first
node and a second node joins
	 * 
	 * @throws Exception
	 */
	@Test
	public void secondNode() throws Exception {

		ILeaderWatcher leaderWatcher1 = mock(ILeaderWatcher.class);

		ElectionWatcher watcher1 = new ElectionWatcher();
		watcher1.setConnectionFactory(connectionFactory);
		watcher1.setLeaderWatcher(leaderWatcher1);

		ILeaderWatcher leaderWatcher2 = mock(ILeaderWatcher.class);

		ElectionWatcher watcher2 = new ElectionWatcher();
		watcher2.setConnectionFactory(connectionFactory);
		watcher2.setLeaderWatcher(leaderWatcher2);

		// now start the object for election process
		watcher1.initialize();

		// wait 2 times as long as the timeout for election to happen
		Thread.sleep(timeout*2);
		
		//now fire up our second watcher
		watcher2.initialize();
		
		Thread.sleep(timeout*2);
		
		watcher1.shutdown();
		watcher2.shutdown();

		// verify that the leaderWatcher was called as the leader
		verify(leaderWatcher1).setAsLeader();
		
		//verify the second watcher wasn't set as leader
		verify(leaderWatcher2, never()).setAsLeader();

	}
}


Re: Integration/Unit testing with Zookeeper

Posted by Patrick Hunt <ph...@apache.org>.
I don't think we have enough context to help you out with this. Where are
you creating/closing your ZooKeeper clients?

I'd suggest that you turn on DEBUG level logging in your test and look at
the output. Verify that you are actually closing the zookeeper client as you
expect.

Patrick

On Wed, Aug 25, 2010 at 9:06 PM, Todd Nine <to...@spidertracks.co.nz> wrote:

> Hi all,
>  I've build my leader election watcher and I'm attempting to
> integration test it.  I'm having a lot of issues in regards to old
> watchers still running.  Here are my test cases (in order of execution).
>
>  1. Single watcher is first to join ZK, becomes the leader.
>  2. watcher1 joins, watcher 2 joins.  watcher1 is leader
>  3. watcher1 joins, watcher 2 joins.  watcher 1 is leader.  Kill watcher 1,
> watcher 2 becomes leader
>
> The issue is that I have 3 test methods in my test class.  Each one
> seems to still have watchers running from previous text case executions.
> As a result, my test cases aren't atomic.  I've tried adding the
> zk.close() call to the end of each of my tests.  However I'm still not
> getting my ephemeral nodes expiring.  I also perform sleeps with my
> timeouts in my tests to make sure that ZK times out the connection to
> old clients.  Any ideas what I can do to clean up integration testing
> this?
>
> public class ElectionWatcherTest extends ZooKeeperTest {
>        /**
>         * Simple test case that checks we're the leader when it's the first
> Node
>         *
>         * @throws Exception
>         */
>        @Test
>        public void firstNode() throws Exception {
>
>                ILeaderWatcher leaderWatcher = mock(ILeaderWatcher.class);
>
>                ElectionWatcher watcher = new ElectionWatcher();
>                watcher.setConnectionFactory(connectionFactory);
>                watcher.setLeaderWatcher(leaderWatcher);
>
>                // now start the object for election process
>                watcher.initialize();
>
>                // wait 2 times as long as the timeout for election to
> happen
>                Thread.sleep(timeout * 2);
>
>                watcher.shutdown();
>
>                // verify that the leaderWatcher was called as the leader
>                verify(leaderWatcher).setAsLeader();
>          }
>
>        /**
>         * Simple test case that checks we're the leader when it's the first
> node and a second node joins
>         *
>         * @throws Exception
>         */
>        @Test
>        public void secondNode() throws Exception {
>
>                ILeaderWatcher leaderWatcher1 = mock(ILeaderWatcher.class);
>
>                ElectionWatcher watcher1 = new ElectionWatcher();
>                watcher1.setConnectionFactory(connectionFactory);
>                watcher1.setLeaderWatcher(leaderWatcher1);
>
>                ILeaderWatcher leaderWatcher2 = mock(ILeaderWatcher.class);
>
>                ElectionWatcher watcher2 = new ElectionWatcher();
>                watcher2.setConnectionFactory(connectionFactory);
>                watcher2.setLeaderWatcher(leaderWatcher2);
>
>                // now start the object for election process
>                watcher1.initialize();
>
>                // wait 2 times as long as the timeout for election to
> happen
>                Thread.sleep(timeout*2);
>
>                //now fire up our second watcher
>                watcher2.initialize();
>
>                Thread.sleep(timeout*2);
>
>                watcher1.shutdown();
>                watcher2.shutdown();
>
>                // verify that the leaderWatcher was called as the leader
>                verify(leaderWatcher1).setAsLeader();
>
>                //verify the second watcher wasn't set as leader
>                verify(leaderWatcher2, never()).setAsLeader();
>
>        }
> }
>
>