Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added prevExist flag to CAS #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/justinsb/etcd/EtcdClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ public EtcdResult cas(String key, String prevValue, String value) throws EtcdCli
return set0(key, data, new int[] { 200, 412 }, 101);
}

/**
* Sets a key to a new value, depending on the previous existence of the key
*/
public EtcdResult cas(String key, Boolean prevExist, String value) throws EtcdClientException {
List<BasicNameValuePair> data = Lists.newArrayList();
data.add(new BasicNameValuePair("value", value));
data.add(new BasicNameValuePair("prevExist", prevExist.toString()));

return set0(key, data, new int[] { 200, 201, 412 }, 105);
}

/**
* Watches the given subtree
*/
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/justinsb/etcd/SmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,32 @@ public void testCAS() throws Exception {
Assert.assertEquals("world", result.node.value);
}

@Test
public void testCASPrevValue() throws Exception {
String key = prefix + "/cas";

EtcdResult result;

result = this.client.set(key, "hello");
result = this.client.get(key);
Assert.assertEquals("hello", result.node.value);

result = this.client.cas(key, false, "world");
Assert.assertEquals(true, result.isError());

result = this.client.get(key);
Assert.assertEquals("hello", result.node.value);

result = this.client.delete(key);
Assert.assertEquals(false, result.isError());

result = this.client.cas(key, false, "world");
Assert.assertEquals(false, result.isError());

result = this.client.get(key);
Assert.assertEquals("world", result.node.value);
}

@Test
public void testWatchPrefix() throws Exception {
String key = prefix + "/watch";
Expand Down