Hi,

The test project on the ProjectSuggestions page, to create tests for the
examples, sounded like a nice way to get started with ZooKeeper, I've
created one queue test and one lock test so far.

I'd like to make a good range of tests, a number of basic sanity ones like
the queue test, and some more involved ones like the lock test.  The lock
test does polling right now, another test would also test watching.  I think
it's useful to test both.

Any comments on this would be great, I noticed the bug about the test case
runtime so I'm not sure what that means for more tests, thanks,

Steven
Index: src/java/test/org/apache/zookeeper/test/ExampleTest.java
===================================================================
--- src/java/test/org/apache/zookeeper/test/ExampleTest.java	(revision 0)
+++ src/java/test/org/apache/zookeeper/test/ExampleTest.java	(revision 0)
@@ -0,0 +1,149 @@
+package org.apache.zookeeper.test;
+
+import java.util.Calendar;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.junit.Test;
+
+public class ExampleTest extends ClientBase {
+	protected static final Logger LOG = Logger.getLogger(ExampleTest.class);
+
+	@Override
+	protected void tearDown() throws Exception {
+		super.tearDown();
+		LOG.info("FINISHED " + getName());
+	}
+
+	static class CountdownTimer {
+		long startTime;
+		long waitTime;
+
+		CountdownTimer(long millis) {
+			waitTime = millis;
+			startTime = Calendar.getInstance().getTimeInMillis();
+		}
+
+		boolean waiting() {
+			return (Calendar.getInstance().getTimeInMillis() - startTime) < waitTime;
+		}
+	}
+
+	@Test
+	public void testQueue() throws Exception {
+		ZooKeeper zkProducer = null;
+		ZooKeeper zkConsumer = null;
+		String queue_handle = "/queue";
+		try {
+
+			zkProducer = createClient();
+			zkConsumer = createClient();
+
+			zkProducer.create(queue_handle, new byte[0], Ids.OPEN_ACL_UNSAFE,
+					CreateMode.PERSISTENT);
+			zkProducer.create(queue_handle + "/element", "0".getBytes(),
+					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
+			zkProducer.create(queue_handle + "/element", "1".getBytes(),
+					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
+			List<String> children = null;
+			CountdownTimer waitForQueueElements = new CountdownTimer(1000);
+			while (waitForQueueElements.waiting()
+					&& ((children = zkConsumer.getChildren(queue_handle, true))
+							.size() != 2)) {
+			}
+			assertEquals(children.size(), 2);
+			String child1 = children.get(0);
+			String child2 = children.get(1);
+			int compareResult = child1.compareTo(child2);
+			assertNotSame(compareResult, 0);
+			if (compareResult < 0) {
+			} else {
+				String temp = child1;
+				child1 = child2;
+				child2 = temp;
+			}
+			String child1data = new String(zkConsumer.getData(queue_handle
+					+ "/" + child1, false, null));
+			String child2data = new String(zkConsumer.getData(queue_handle
+					+ "/" + child2, false, null));
+			assertEquals(child1data, "0");
+			assertEquals(child2data, "1");
+		} finally {
+			if (zkProducer != null) {
+				zkProducer.close();
+			}
+			if (zkConsumer != null) {
+				zkConsumer.close();
+			}
+		}
+
+	}
+
+	@Test
+	public void testLock() throws Exception {
+		final int num_contenders = 10;
+		final ZooKeeper zookeepers[] = new ZooKeeper[num_contenders];
+		final String contenderLockNodes[] = new String[num_contenders];
+		final String lock_handle = "/lock";
+		final String lock_element = "/node-";
+		final int lock_prefix_length = lock_handle.length() + lock_element.length();
+		try{
+			for(int i=0; i < zookeepers.length; i++){
+				zookeepers[i] = createClient();
+			}
+
+			zookeepers[0].create(lock_handle, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+
+			//Get lock nodes
+			for(int i=0; i< zookeepers.length; i++){
+				contenderLockNodes[i] = zookeepers[i].create(lock_handle+lock_element, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
+			}
+
+			boolean contenderSucceeded[] = new boolean[num_contenders];
+			//Poll the locks in a round robin
+			CountdownTimer lockAcquire = new CountdownTimer(1000*num_contenders);
+			
+			boolean waiting = true;
+			while(lockAcquire.waiting() && waiting){
+				waiting = false;
+				for(int contenderId=0; contenderId < num_contenders; contenderId++){
+					if(contenderSucceeded[contenderId]) break;
+					ZooKeeper zooKeeper = zookeepers[contenderId];
+					String lockNode = contenderLockNodes[contenderId];
+					long lockId = Long.parseLong(lockNode.substring(lock_prefix_length));
+
+					List<String> children = zooKeeper.getChildren(lock_handle, false);
+					
+					long coveredId = -1;
+					for(String child : children){
+						long childId = Long.parseLong(child.substring(lock_prefix_length));
+						if((coveredId < childId) && (childId < lockId)){
+							coveredId = childId;
+						}
+					}		
+					if(coveredId == -1){
+						contenderSucceeded[contenderId] = true;
+						//Release lock
+						zooKeeper.delete(lockNode, -1);
+					} else{
+						waiting = true;
+					}
+				}
+			}
+
+			for(int i=0; i< contenderSucceeded.length; i++){
+				assertTrue(contenderSucceeded[i]);
+			}
+		}finally{
+			for(ZooKeeper zooKeeper : zookeepers){
+				if(zooKeeper !=null){
+					zooKeeper.close();
+				}
+			}
+		}
+	}
+}
package org.apache.zookeeper.test;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
import org.junit.Test;

public class ExampleTest extends ClientBase {
	protected static final Logger LOG = Logger.getLogger(ExampleTest.class);

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		LOG.info("FINISHED " + getName());
	}

	static class CountdownTimer {
		long startTime;
		long waitTime;

		CountdownTimer(long millis) {
			waitTime = millis;
			startTime = Calendar.getInstance().getTimeInMillis();
		}

		boolean waiting() {
			return (Calendar.getInstance().getTimeInMillis() - startTime) < waitTime;
		}
	}

	@Test
	public void testQueue() throws Exception {
		ZooKeeper zkProducer = null;
		ZooKeeper zkConsumer = null;
		String queue_handle = "/queue";
		try {

			zkProducer = createClient();
			zkConsumer = createClient();

			zkProducer.create(queue_handle, new byte[0], Ids.OPEN_ACL_UNSAFE,
					CreateMode.PERSISTENT);
			zkProducer.create(queue_handle + "/element", "0".getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
			zkProducer.create(queue_handle + "/element", "1".getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
			List<String> children = null;
			CountdownTimer waitForQueueElements = new CountdownTimer(1000);
			while (waitForQueueElements.waiting()
					&& ((children = zkConsumer.getChildren(queue_handle, true))
							.size() != 2)) {
			}
			assertEquals(children.size(), 2);
			String child1 = children.get(0);
			String child2 = children.get(1);
			int compareResult = child1.compareTo(child2);
			assertNotSame(compareResult, 0);
			if (compareResult < 0) {
			} else {
				String temp = child1;
				child1 = child2;
				child2 = temp;
			}
			String child1data = new String(zkConsumer.getData(queue_handle
					+ "/" + child1, false, null));
			String child2data = new String(zkConsumer.getData(queue_handle
					+ "/" + child2, false, null));
			assertEquals(child1data, "0");
			assertEquals(child2data, "1");
		} finally {
			if (zkProducer != null) {
				zkProducer.close();
			}
			if (zkConsumer != null) {
				zkConsumer.close();
			}
		}

	}

	@Test
	public void testLock() throws Exception {
		final int num_contenders = 10;
		final ZooKeeper zookeepers[] = new ZooKeeper[num_contenders];
		final String contenderLockNodes[] = new String[num_contenders];
		final String lock_handle = "/lock";
		final String lock_element = "/node-";
		final int lock_prefix_length = lock_handle.length() + lock_element.length();
		try{
			for(int i=0; i < zookeepers.length; i++){
				zookeepers[i] = createClient();
			}

			zookeepers[0].create(lock_handle, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

			//Get lock nodes
			for(int i=0; i< zookeepers.length; i++){
				contenderLockNodes[i] = zookeepers[i].create(lock_handle+lock_element, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
			}

			boolean contenderSucceeded[] = new boolean[num_contenders];
			//Poll the locks in a round robin
			CountdownTimer lockAcquire = new CountdownTimer(1000*num_contenders);
			
			boolean waiting = true;
			while(lockAcquire.waiting() && waiting){
				waiting = false;
				for(int contenderId=0; contenderId < num_contenders; contenderId++){
					if(contenderSucceeded[contenderId]) break;
					ZooKeeper zooKeeper = zookeepers[contenderId];
					String lockNode = contenderLockNodes[contenderId];
					long lockId = Long.parseLong(lockNode.substring(lock_prefix_length));

					List<String> children = zooKeeper.getChildren(lock_handle, false);
					
					long coveredId = -1;
					for(String child : children){
						long childId = Long.parseLong(child.substring(lock_prefix_length));
						if((coveredId < childId) && (childId < lockId)){
							coveredId = childId;
						}
					}		
					if(coveredId == -1){
						contenderSucceeded[contenderId] = true;
						//Release lock
						zooKeeper.delete(lockNode, -1);
					} else{
						waiting = true;
					}
				}
			}

			for(int i=0; i< contenderSucceeded.length; i++){
				assertTrue(contenderSucceeded[i]);
			}
		}finally{
			for(ZooKeeper zooKeeper : zookeepers){
				if(zooKeeper !=null){
					zooKeeper.close();
				}
			}
		}
	}
}

Reply via email to