Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.apache.geode.redis.internal.executor.string;

import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand Down Expand Up @@ -94,6 +95,14 @@ public void testDecrBy() {
assertThat(ex).isNotNull();
}

@Test
public void decrbyMoreThanMaxLongThrowsArithmeticException() {
jedis.set("somekey", "1");
assertThatThrownBy(
() -> jedis.sendCommand(Protocol.Command.DECRBY, "somekey", "9223372036854775809"))
.hasMessageContaining(ERROR_NOT_INTEGER);
}

private String randString() {
return Long.toHexString(Double.doubleToLongBits(Math.random()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.apache.geode.redis.internal.executor.string;

import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;
import static org.apache.geode.redis.internal.RedisConstants.ERROR_OVERFLOW;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand All @@ -22,10 +24,8 @@
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisDataException;

import org.apache.geode.redis.ConcurrentLoopingThreads;
import org.apache.geode.redis.internal.RedisConstants;
import org.apache.geode.test.awaitility.GeodeAwaitility;
import org.apache.geode.test.dunit.rules.RedisPortSupplier;

Expand Down Expand Up @@ -87,25 +87,17 @@ public void testIncr_whenOverflow_shouldReturnError() {
String max64BitIntegerValue = "9223372036854775807";
jedis.set(key, max64BitIntegerValue);

try {
jedis.incr(key);
} catch (JedisDataException e) {
assertThat(e.getMessage()).contains(RedisConstants.ERROR_OVERFLOW);
}
assertThatThrownBy(() -> jedis.incr(key)).hasMessageContaining(ERROR_OVERFLOW);
assertThat(jedis.get(key)).isEqualTo(max64BitIntegerValue);
}

@Test
public void testIncr_whenWrongType_shouldReturnError() {
String key = "key";
String nonIntegerValue = "I am not a number! I am a free man!";
assertThat(jedis.set(key, nonIntegerValue)).isEqualTo("OK");

try {
jedis.incr(key);
} catch (JedisDataException e) {
assertThat(e.getMessage()).contains(RedisConstants.ERROR_NOT_INTEGER);
}
assertThat(jedis.set(key, nonIntegerValue)).isEqualTo("OK");
assertThatThrownBy(() -> jedis.incr(key)).hasMessageContaining(ERROR_NOT_INTEGER);
assertThat(jedis.get(key)).isEqualTo(nonIntegerValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public void testStrlen_requestWrongType_shouldReturnError() {
.hasMessageContaining(RedisConstants.ERROR_WRONG_TYPE);
}

@Test
public void testStrlen_withEmptyByte() {
byte[] key = new byte[] {0};
jedis.set(key, new byte[] {});

assertThat(jedis.strlen(key)).isEqualTo(0);
}

@Test
public void testStrlen_withBinaryData() {
byte[] zero = new byte[] {0};
Expand All @@ -91,4 +99,22 @@ public void testStrlen_withBinaryData() {
assertThat(jedis.strlen(zero)).isEqualTo(1);
}

@Test
public void testStrlen_withIntData() {
byte[] key = new byte[] {0};
byte[] value = new byte[] {1, 0, 0};
jedis.set(key, value);

assertThat(jedis.strlen(key)).isEqualTo(value.length);
}

@Test
public void testStrlen_withFloatData() {
byte[] key = new byte[] {0};
byte[] value = new byte[] {'0', '.', '9'};
jedis.set(key, value);

assertThat(jedis.strlen(key)).isEqualTo(value.length);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.apache.geode.redis.internal.data;



import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
Expand Down Expand Up @@ -46,6 +44,14 @@ public RedisString(ByteArrayWrapper value) {
// for serialization
public RedisString() {}

public ByteArrayWrapper get() {
return new ByteArrayWrapper(value.toBytes());
}

public void set(ByteArrayWrapper value) {
valueSet(value);
}

public int append(ByteArrayWrapper appendValue,
Region<ByteArrayWrapper, RedisData> region,
ByteArrayWrapper key) {
Expand All @@ -55,14 +61,6 @@ public int append(ByteArrayWrapper appendValue,
return value.length();
}

public ByteArrayWrapper get() {
return new ByteArrayWrapper(value.toBytes());
}

public void set(ByteArrayWrapper value) {
valueSet(value);
}

public long incr(Region<ByteArrayWrapper, RedisData> region, ByteArrayWrapper key)
throws NumberFormatException, ArithmeticException {
long longValue = parseValueAsLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package org.apache.geode.redis.internal.executor.string;


import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER;

import java.util.List;

import org.apache.geode.redis.internal.data.ByteArrayWrapper;
Expand All @@ -25,9 +27,6 @@

public class DecrByExecutor extends StringExecutor {

private static final String ERROR_DECREMENT_NOT_USABLE =
"The decrementation on this key must be numeric";

private static final int DECREMENT_INDEX = 2;

@Override
Expand All @@ -42,7 +41,7 @@ public RedisResponse executeCommand(Command command, ExecutionHandlerContext con
try {
decrement = Long.parseLong(decrString);
} catch (NumberFormatException e) {
return RedisResponse.error(ERROR_DECREMENT_NOT_USABLE);
return RedisResponse.error(ERROR_NOT_INTEGER);
}

long value = getRedisStringCommands(context).decrby(key, decrement);
Expand Down
Loading