package net.spy.memcached;

import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Collection;

/**
 * Test ketama node location.
 */
public class KetamaNodeLocatorTest extends AbstractNodeLocationCase {

	@Override
	protected void setupNodes(int n) {
		super.setupNodes(n);

		for(int i=0; i<nodeMocks.length; i++) {
			nodeMocks[i].expects(once())
				.method("getSocketAddress")
				.will(returnValue(InetSocketAddress.createUnresolved(
						"127.0.0.1", 10000 + i)));
		}

		locator=new KetamaNodeLocator(Arrays.asList(nodes),
			HashAlgorithm.KETAMA_HASH);
	}

	public void testAll() throws Exception {
		setupNodes(4);

		Collection<MemcachedNode> all = locator.getAll();
		assertEquals(4, all.size());
		for(int i=0; i<4; i++) {
			assertTrue(all.contains(nodes[i]));
		}
	}

	public void testLookups() {
		setupNodes(4);
		assertSame(nodes[0], locator.getPrimary("dustin"));
		assertSame(nodes[2], locator.getPrimary("noelani"));
		assertSame(nodes[1], locator.getPrimary("some other key"));
	}

	public void testClusterResizing() {
		setupNodes(4);
		assertSame(nodes[0], locator.getPrimary("dustin"));
		assertSame(nodes[2], locator.getPrimary("noelani"));
		assertSame(nodes[1], locator.getPrimary("some other key"));

		setupNodes(5);
		assertSame(nodes[0], locator.getPrimary("dustin"));
		assertSame(nodes[2], locator.getPrimary("noelani"));
		assertSame(nodes[1], locator.getPrimary("some other key"));
	}

	public void testSequence1() {
		setupNodes(4);
		assertSequence("dustin", 0, 1, 1, 3);
	}

	public void testSequence2() {
		setupNodes(4);
		assertSequence("noelani", 2, 0, 0, 0);
	}
}

@@ -1012,32 +1012,517 @@
 her key%22));%0A%09%7D%0A%0A
+%09public void testContinuumWrapping() %7B%0A%09%09/*%0A%09%09// This is the method by which I found something that would wrap%0A%09%09String key=%22a%22;%0A%09%09// maximum key found in the ketama continuum%0A%09%09while(HashAlgorithm.KETAMA_HASH.hash(key) %3C= 4290126876L) %7B%0A%09%09%09key=PwGen.getPass(8);%0A%09%09%7D%0A%09%09System.out.println(%22Found a key past the end of the continuum:  %22%0A%09%09%09+ key);%0A%09%09*/%0A%0A%09%09setupNodes(4);%0A%09%09assertSame(nodes%5B3%5D, locator.getPrimary(%227QHNPFVC%22));%0A%09%09assertSame(nodes%5B3%5D, locator.getPrimary(%22N6H4245M%22));%0A%09%7D%0A%0A
 %09public void tes

Test ketama continuum wrapping.