Skip to content

Commit

Permalink
add binary version integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jun 25, 2024
1 parent 02ede1f commit b5ee3ec
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
21 changes: 20 additions & 1 deletion java/integTest/src/test/java/glide/SharedCommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public void hsetnx(BaseClient client) {
String key2 = UUID.randomUUID().toString();
String field = UUID.randomUUID().toString();

assertTrue(client.hsetnx(gs(key1), gs(field), gs("value")).get());
assertTrue(client.hsetnx(key1, field, "value").get());
assertFalse(client.hsetnx(key1, field, "newValue").get());
assertEquals("value", client.hget(key1, field).get());

Expand All @@ -755,6 +755,25 @@ public void hsetnx(BaseClient client) {
assertTrue(executionException.getCause() instanceof RequestException);
}

@SneakyThrows
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("getClients")
public void hsetnx_binary(BaseClient client) {
GlideString key1 = gs(UUID.randomUUID().toString());
GlideString key2 = gs(UUID.randomUUID().toString());
GlideString field = gs(UUID.randomUUID().toString());

assertTrue(client.hsetnx(key1, field, gs("value")).get());
assertFalse(client.hsetnx(key1, field, gs("newValue")).get());
assertEquals("value", client.hget(key1.toString(), field.toString()).get());

// Key exists, but it is not a hash
assertEquals(OK, client.set(key2, gs("value")).get());
ExecutionException executionException =
assertThrows(ExecutionException.class, () -> client.hsetnx(key2, field, gs("value")).get());
assertTrue(executionException.getCause() instanceof RequestException);
}

@SneakyThrows
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("getClients")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import glide.api.models.exceptions.RequestException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -264,7 +263,6 @@ public void watch() {
String foobarString = "foobar";
String helloString = "hello";
String[] keys = new String[] {key1, key2, key3};
GlideString[] keys_gs = Arrays.stream(keys).map(GlideString::gs).toArray(GlideString[]::new);
Transaction setFoobarTransaction = new Transaction();
Transaction setHelloTransaction = new Transaction();
String[] expectedExecResponse = new String[] {OK, OK, OK};
Expand All @@ -281,7 +279,63 @@ public void watch() {

// Transaction executes command successfully with a read command on the watch key before
// transaction is executed.
assertEquals(OK, client.watch(keys_gs).get());
assertEquals(OK, client.watch(keys).get());
assertEquals(helloString, client.get(key2).get());
assertArrayEquals(expectedExecResponse, client.exec(setFoobarTransaction).get());
assertEquals(foobarString, client.get(key1).get()); // Sanity check
assertEquals(foobarString, client.get(key2).get());
assertEquals(foobarString, client.get(key3).get());

// Transaction executes command successfully with unmodified watched keys
assertEquals(OK, client.watch(keys).get());
assertArrayEquals(expectedExecResponse, client.exec(setFoobarTransaction).get());
assertEquals(foobarString, client.get(key1).get()); // Sanity check
assertEquals(foobarString, client.get(key2).get());
assertEquals(foobarString, client.get(key3).get());

// Transaction executes command successfully with a modified watched key but is not in the
// transaction.
assertEquals(OK, client.watch(new String[] {key4}).get());
setHelloTransaction.set(key1, helloString).set(key2, helloString).set(key3, helloString);
assertArrayEquals(expectedExecResponse, client.exec(setHelloTransaction).get());
assertEquals(helloString, client.get(key1).get()); // Sanity check
assertEquals(helloString, client.get(key2).get());
assertEquals(helloString, client.get(key3).get());

// WATCH can not have an empty String array parameter
ExecutionException executionException =
assertThrows(ExecutionException.class, () -> client.watch(new String[] {}).get());
assertInstanceOf(RequestException.class, executionException.getCause());
}

@Test
@SneakyThrows
public void watch_binary() {
String key1 = "{key}-1" + UUID.randomUUID();
String key2 = "{key}-2" + UUID.randomUUID();
String key3 = "{key}-3" + UUID.randomUUID();
String key4 = "{key}-4" + UUID.randomUUID();
String foobarString = "foobar";
String helloString = "hello";
GlideString[] keys =
new GlideString[] {GlideString.gs(key1), GlideString.gs(key2), GlideString.gs(key3)};
Transaction setFoobarTransaction = new Transaction();
Transaction setHelloTransaction = new Transaction();
String[] expectedExecResponse = new String[] {OK, OK, OK};

// Returns null when a watched key is modified before it is executed in a transaction command.
// Transaction commands are not performed.
assertEquals(OK, client.watch(keys).get());
assertEquals(OK, client.set(key2, helloString).get());
setFoobarTransaction.set(key1, foobarString).set(key2, foobarString).set(key3, foobarString);
assertEquals(null, client.exec(setFoobarTransaction).get());
assertEquals(null, client.get(key1).get()); // Sanity check
assertEquals(helloString, client.get(key2).get());
assertEquals(null, client.get(key3).get());

// Transaction executes command successfully with a read command on the watch key before
// transaction is executed.
assertEquals(OK, client.watch(keys).get());
assertEquals(helloString, client.get(key2).get());
assertArrayEquals(expectedExecResponse, client.exec(setFoobarTransaction).get());
assertEquals(foobarString, client.get(key1).get()); // Sanity check
Expand Down

0 comments on commit b5ee3ec

Please sign in to comment.