package net.spy.memcached;

import java.util.concurrent.Callable;

import net.spy.memcached.transcoders.LongTranscoder;
import net.spy.test.SyncThread;

/**
 * Test the CAS mutator.
 */
public class CASMutatorTest extends ClientBaseCase {

	private CASMutation<Long> mutation;
	private CASMutator<Long> mutator;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		mutation=new CASMutation<Long>() {
			public Long getNewValue(Long current) {
				return current+1;
			}
		};
		mutator=new CASMutator<Long>(client, new LongTranscoder(), 50);
	}

	public void testConcurrentCAS() throws Throwable {
		int num=SyncThread.getDistinctResultCount(20, new Callable<Long>(){
			public Long call() throws Exception {
				return mutator.cas("test.cas.concurrent", 0L, 0, mutation);
			}});
		assertEquals(20, num);
	}

	public void testIncorrectTypeInCAS() throws Throwable {
		// Stick something for this CAS in the cache.
		client.set("x", 0, "not a long");
		try {
			Long rv=mutator.cas("x", 1L, 0, mutation);
			fail("Expected RuntimeException on invalid type mutation, got "
				+ rv);
		} catch(RuntimeException e) {
			assertEquals("Couldn't get a CAS in 50 attempts", e.getMessage());
		}
	}

	public void testCASValueToString() {
		CASValue<String> c=new CASValue<String>(717L, "hi");
		assertEquals("{CasValue 717/hi}", c.toString());
	}
}

@@ -555,24 +555,179 @@
 ), 50);%0A%09%7D%0A%0A
+%09public void testDefaultConstructor() %7B%0A%09%09// Just validate that this doesn't throw an exception.%0A%09%09new CASMutator%3CLong%3E(client, new LongTranscoder());%0A%09%7D%0A%0A
 %09public void

Make sure all of the CASMutator constructors are at least run once.