DATAREDIS-525 - Add support for ReactiveRedisConnection & ReactiveRedisClusterConnection.

We introduced reactor based reactive connection support via ReactiveRedisConnection and ReactiveRedisClusterConnection.

A reactive connection can be obtained via the ConnectionFactory. This is currently only possible when using the Lettuce Redis driver.

Original pull request: #229.
This commit is contained in:
Christoph Strobl
2016-06-29 21:11:17 +02:00
committed by Mark Paluch
parent f6411b3811
commit d257ae1074
72 changed files with 14189 additions and 36 deletions

View File

@@ -35,6 +35,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.hamcrest.core.IsInstanceOf;
import org.junit.AfterClass;
import org.junit.AssumptionViolatedException;
import org.junit.Before;
@@ -254,7 +255,7 @@ public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
cache.put(key, value);
RedisCache redisCache = (RedisCache) cache;
assertThat(redisCache.get(key, value.getClass()), instanceOf(value.getClass()));
assertThat(redisCache.get(key, value.getClass()), IsInstanceOf.<Object>instanceOf(value.getClass()));
}
/**

View File

@@ -55,7 +55,7 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional(transactionManager = "transactionManager")
public class TransactionalRedisCacheManagerWithCommitUnitTests {
@SuppressWarnings("rawtypes")//
@SuppressWarnings("rawtypes") //
protected @Autowired RedisTemplate redisTemplate;
protected @Autowired FooService transactionalService;

View File

@@ -51,7 +51,7 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional(transactionManager = "transactionManager")
public class TransactionalRedisCacheManagerWithRollbackUnitTests {
@SuppressWarnings("rawtypes")//
@SuppressWarnings("rawtypes") //
protected @Autowired RedisTemplate redisTemplate;
protected @Autowired FooService transactionalService;

View File

@@ -105,8 +105,9 @@ public abstract class AbstractTransactionalTestBase {
RedisConnection connection = factory.getConnection();
for (String key : KEYS) {
Assert.assertThat("Values for " + key + " should " + (valuesShouldHaveBeenPersisted ? "" : "NOT ")
+ "have been found.", connection.exists(key.getBytes()), Is.is(valuesShouldHaveBeenPersisted));
Assert.assertThat(
"Values for " + key + " should " + (valuesShouldHaveBeenPersisted ? "" : "NOT ") + "have been found.",
connection.exists(key.getBytes()), Is.is(valuesShouldHaveBeenPersisted));
}
connection.close();
}

View File

@@ -32,6 +32,7 @@ import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.repository.util.QueryExecutionConverters;
import com.lambdaworks.redis.RedisException;
import com.lambdaworks.redis.api.async.RedisAsyncCommands;
@@ -325,4 +326,20 @@ public class LettuceConnectionFactoryTests {
connection.close();
}
}
/**
* @see DATAREDIS-525
*/
@Test
public void factoryShouldReturnReactiveConnectionWhenCorrectly() {
LettuceConnectionFactory factory = new LettuceConnectionFactory();
factory.afterPropertiesSet();
ConnectionFactoryTracker.add(factory);
assertThat(factory.getReactiveConnection()
.execute(cmd -> QueryExecutionConverters.RxJava1ObservableToMonoConverter.INSTANCE.convert(cmd.ping()))
.blockFirst(), is("PONG"));
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.is;
import static org.junit.Assume.assumeThat;
import com.lambdaworks.redis.cluster.api.sync.RedisClusterCommands;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.springframework.data.redis.test.util.LettuceRedisClusterClientProvider;
/**
* @author Christoph Strobl
*/
public abstract class LettuceReactiveClusterCommandsTestsBase {
public static @ClassRule LettuceRedisClusterClientProvider clientProvider = LettuceRedisClusterClientProvider.local();
RedisClusterCommands<String, String> nativeCommands;
LettuceReactiveRedisClusterConnection connection;
@Before
public void before() {
assumeThat(clientProvider.test(), is(true));
nativeCommands = clientProvider.getClient().connect().sync();
connection = new LettuceReactiveRedisClusterConnection(clientProvider.getClient());
}
@After
public void tearDown() {
if(nativeCommands != null) {
nativeCommands.flushall();
nativeCommands.close();
}
if(connection != null) {
connection.close();
}
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import static org.springframework.data.redis.connection.lettuce.LettuceReactiveCommandsTestsBase.*;
import java.util.Arrays;
import org.junit.Test;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveClusterHyperLogLogCommandsTests extends LettuceReactiveClusterCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void pfCountWithMultipleKeysShouldReturnCorrectlyWhenKeysMapToSameSlot() {
nativeCommands.pfadd(SAME_SLOT_KEY_1, new String[] { VALUE_1, VALUE_2 });
nativeCommands.pfadd(SAME_SLOT_KEY_2, new String[] { VALUE_2, VALUE_3 });
assertThat(connection.hyperLogLogCommands().pfCount(Arrays.asList(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER))
.block(), is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void pfMergeShouldWorkCorrectlyWhenKeysMapToSameSlot() {
nativeCommands.pfadd(SAME_SLOT_KEY_1, new String[] { VALUE_1, VALUE_2 });
nativeCommands.pfadd(SAME_SLOT_KEY_2, new String[] { VALUE_2, VALUE_3 });
assertThat(
connection.hyperLogLogCommands()
.pfMerge(SAME_SLOT_KEY_3_BBUFFER, Arrays.asList(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER)).block(),
is(true));
assertThat(nativeCommands.pfcount(new String[] { SAME_SLOT_KEY_3 }), is(3L));
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.collection.IsCollectionWithSize.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*;
import static org.springframework.data.redis.connection.RedisClusterNode.*;
import static org.springframework.data.redis.connection.lettuce.LettuceReactiveCommandsTestsBase.*;
import java.nio.ByteBuffer;
import java.util.List;
import org.junit.Test;
import org.springframework.data.redis.connection.RedisClusterNode;
import reactor.core.publisher.Mono;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveClusterKeyCommandsTests extends LettuceReactiveClusterCommandsTestsBase {
static final RedisClusterNode NODE_1 = newRedisClusterNode().listeningAt("127.0.0.1", 7379).build();
/**
* @see DATAREDIS-525
*/
@Test
public void keysShouldReturnOnlyKeysFromSelectedNode() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
List<ByteBuffer> result = connection.keyCommands().keys(NODE_1, ByteBuffer.wrap("*".getBytes())).block();
assertThat(result, hasSize(1));
assertThat(result, contains(KEY_1_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void randomkeyShouldReturnOnlyKeysFromSelectedNode() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
Mono<ByteBuffer> randomkey = connection.keyCommands().randomKey(NODE_1);
for (int i = 0; i < 10; i++) {
assertThat(randomkey.block(), is(equalTo(KEY_1_BBUFFER)));
}
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*;
import static org.springframework.data.redis.connection.lettuce.LettuceReactiveCommandsTestsBase.*;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.data.redis.connection.ReactiveListCommands;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveClusterListCommandsTests extends LettuceReactiveClusterCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void bRPopLPushShouldWorkCorrectlyWhenAllKeysMapToSameSlot() {
nativeCommands.rpush(SAME_SLOT_KEY_1, VALUE_1, VALUE_2, VALUE_3);
nativeCommands.rpush(SAME_SLOT_KEY_2, VALUE_1);
ByteBuffer result = connection.listCommands()
.bRPopLPush(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER, Duration.ofSeconds(1)).block();
assertThat(result, is(equalTo(VALUE_3_BBUFFER)));
assertThat(nativeCommands.llen(SAME_SLOT_KEY_2), is(2L));
assertThat(nativeCommands.lindex(SAME_SLOT_KEY_2, 0), is(equalTo(VALUE_3)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void blPopShouldReturnFirstAvailableWhenAllKeysMapToTheSameSlot() {
nativeCommands.rpush(SAME_SLOT_KEY_1, VALUE_1, VALUE_2, VALUE_3);
ReactiveListCommands.PopResult result = connection.listCommands()
.blPop(Arrays.asList(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER), Duration.ofSeconds(1L)).block();
assertThat(result.getKey(), is(equalTo(SAME_SLOT_KEY_1_BBUFFER)));
assertThat(result.getValue(), is(equalTo(VALUE_1_BBUFFER)));
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*;
import static org.springframework.data.redis.connection.lettuce.LettuceReactiveCommandsTestsBase.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.data.redis.connection.RedisStringCommands;
/**
* @author Christoph Strobl
* @since 2.0
*/
public class LettuceReactiveClusterStringCommandsTests extends LettuceReactiveClusterCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void mSetNXShouldAddMultipleKeyValueParisWhenMappedToSameSlot() {
Map<ByteBuffer, ByteBuffer> map = new LinkedHashMap<>();
map.put(SAME_SLOT_KEY_1_BBUFFER, VALUE_1_BBUFFER);
map.put(SAME_SLOT_KEY_2_BBUFFER, VALUE_2_BBUFFER);
connection.stringCommands().mSetNX(map).block();
assertThat(nativeCommands.get(SAME_SLOT_KEY_1), is(equalTo(VALUE_1)));
assertThat(nativeCommands.get(SAME_SLOT_KEY_2), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void mSetNXShouldNotAddMultipleKeyValueParisWhenAlreadyExitAndMapToSameSlot() {
nativeCommands.set(SAME_SLOT_KEY_2, VALUE_2);
Map<ByteBuffer, ByteBuffer> map = new LinkedHashMap<>();
map.put(SAME_SLOT_KEY_1_BBUFFER, VALUE_1_BBUFFER);
map.put(SAME_SLOT_KEY_2_BBUFFER, VALUE_2_BBUFFER);
assertThat(connection.stringCommands().mSetNX(map).block(), is(false));
assertThat(nativeCommands.exists(SAME_SLOT_KEY_1), is(false));
assertThat(nativeCommands.get(SAME_SLOT_KEY_2), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void bitOpAndShouldWorkAsExpectedWhenKeysMapToSameSlot() {
nativeCommands.set(SAME_SLOT_KEY_1, VALUE_1);
nativeCommands.set(SAME_SLOT_KEY_2, VALUE_2);
assertThat(connection.stringCommands().bitOp(Arrays.asList(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER),
RedisStringCommands.BitOperation.AND, SAME_SLOT_KEY_3_BBUFFER).block(), is(7L));
assertThat(nativeCommands.get(SAME_SLOT_KEY_3), is(equalTo("value-0")));
}
/**
* @see DATAREDIS-525
*/
@Test
public void bitOpOrShouldWorkAsExpectedWhenKeysMapToSameSlot() {
nativeCommands.set(SAME_SLOT_KEY_1, VALUE_1);
nativeCommands.set(SAME_SLOT_KEY_2, VALUE_2);
assertThat(connection.stringCommands().bitOp(Arrays.asList(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER),
RedisStringCommands.BitOperation.OR, SAME_SLOT_KEY_3_BBUFFER).block(), is(7L));
assertThat(nativeCommands.get(SAME_SLOT_KEY_3), is(equalTo(VALUE_3)));
}
/**
* @see DATAREDIS-525
*/
@Test(expected = IllegalArgumentException.class)
public void bitNotShouldThrowExceptionWhenMoreThanOnSourceKeyAndKeysMapToSameSlot() {
connection.stringCommands().bitOp(Arrays.asList(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER),
RedisStringCommands.BitOperation.NOT, SAME_SLOT_KEY_3_BBUFFER).block();
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import static org.springframework.data.redis.connection.lettuce.LettuceReactiveCommandsTestsBase.*;
import java.util.Arrays;
import org.junit.Test;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveClusterZSetCommandsTests extends LettuceReactiveClusterCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void zUnionStoreShouldWorkWhenAllKeysMapToSameSlot() {
nativeCommands.zadd(SAME_SLOT_KEY_1, 1D, VALUE_1);
nativeCommands.zadd(SAME_SLOT_KEY_1, 2D, VALUE_2);
nativeCommands.zadd(SAME_SLOT_KEY_2, 1D, VALUE_1);
nativeCommands.zadd(SAME_SLOT_KEY_2, 2D, VALUE_2);
nativeCommands.zadd(SAME_SLOT_KEY_2, 3D, VALUE_3);
assertThat(connection.zSetCommands().zUnionStore(SAME_SLOT_KEY_3_BBUFFER,
Arrays.asList(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER), Arrays.asList(2D, 3D)).block(), is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zInterStoreShouldWorkCorrectlyWhenKeysMapToSameSlot() {
nativeCommands.zadd(SAME_SLOT_KEY_1, 1D, VALUE_1);
nativeCommands.zadd(SAME_SLOT_KEY_1, 2D, VALUE_2);
nativeCommands.zadd(SAME_SLOT_KEY_2, 1D, VALUE_1);
nativeCommands.zadd(SAME_SLOT_KEY_2, 2D, VALUE_2);
nativeCommands.zadd(SAME_SLOT_KEY_2, 3D, VALUE_3);
assertThat(connection.zSetCommands().zInterStore(SAME_SLOT_KEY_3_BBUFFER,
Arrays.asList(SAME_SLOT_KEY_1_BBUFFER, SAME_SLOT_KEY_2_BBUFFER), Arrays.asList(2D, 3D)).block(), is(2L));
}
}

View File

@@ -0,0 +1,129 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.is;
import static org.junit.Assume.assumeThat;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import com.lambdaworks.redis.AbstractRedisClient;
import com.lambdaworks.redis.cluster.RedisClusterClient;
import com.lambdaworks.redis.cluster.api.sync.RedisClusterCommands;
import org.hamcrest.core.Is;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.data.redis.test.util.LettuceRedisClientProvider;
import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.api.sync.RedisCommands;
import org.springframework.data.redis.test.util.LettuceRedisClusterClientProvider;
/**
* @author Christoph Strobl
*/
@RunWith(Parameterized.class)
public class LettuceReactiveCommandsTestsBase {
static final String KEY_1 = "key-1";
static final String KEY_2 = "key-2";
static final String KEY_3 = "key-3";
static final String SAME_SLOT_KEY_1 = "{key}-1";
static final String SAME_SLOT_KEY_2 = "{key}-2";
static final String SAME_SLOT_KEY_3 = "{key}-3";
static final String VALUE_1 = "value-1";
static final String VALUE_2 = "value-2";
static final String VALUE_3 = "value-3";
static final byte[] SAME_SLOT_KEY_1_BYTES = SAME_SLOT_KEY_1.getBytes(Charset.forName("UTF-8"));
static final byte[] SAME_SLOT_KEY_2_BYTES = SAME_SLOT_KEY_2.getBytes(Charset.forName("UTF-8"));
static final byte[] SAME_SLOT_KEY_3_BYTES = SAME_SLOT_KEY_3.getBytes(Charset.forName("UTF-8"));
static final byte[] KEY_1_BYTES = KEY_1.getBytes(Charset.forName("UTF-8"));
static final byte[] KEY_2_BYTES = KEY_2.getBytes(Charset.forName("UTF-8"));
static final byte[] KEY_3_BYTES = KEY_3.getBytes(Charset.forName("UTF-8"));
static final byte[] VALUE_1_BYTES = VALUE_1.getBytes(Charset.forName("UTF-8"));
static final byte[] VALUE_2_BYTES = VALUE_2.getBytes(Charset.forName("UTF-8"));
static final byte[] VALUE_3_BYTES = VALUE_3.getBytes(Charset.forName("UTF-8"));
static final ByteBuffer KEY_1_BBUFFER = ByteBuffer.wrap(KEY_1_BYTES);
static final ByteBuffer SAME_SLOT_KEY_1_BBUFFER = ByteBuffer.wrap(SAME_SLOT_KEY_1_BYTES);
static final ByteBuffer VALUE_1_BBUFFER = ByteBuffer.wrap(VALUE_1_BYTES);
static final ByteBuffer KEY_2_BBUFFER = ByteBuffer.wrap(KEY_2_BYTES);
static final ByteBuffer SAME_SLOT_KEY_2_BBUFFER = ByteBuffer.wrap(SAME_SLOT_KEY_2_BYTES);
static final ByteBuffer VALUE_2_BBUFFER = ByteBuffer.wrap(VALUE_2_BYTES);
static final ByteBuffer KEY_3_BBUFFER = ByteBuffer.wrap(KEY_3_BYTES);
static final ByteBuffer SAME_SLOT_KEY_3_BBUFFER = ByteBuffer.wrap(SAME_SLOT_KEY_3_BYTES);
static final ByteBuffer VALUE_3_BBUFFER = ByteBuffer.wrap(VALUE_3_BYTES);
@Parameterized.Parameter(value = 0) public Object clientProvider;
LettuceReactiveRedisConnection connection;
RedisClusterCommands<String, String> nativeCommands;
@Parameterized.Parameters
public static List<Object> parameters() {
return Arrays.asList(LettuceRedisClientProvider.local(), LettuceRedisClusterClientProvider.local());
}
@Before
public void setUp() {
AbstractRedisClient abstractRedisClient = null;
if (clientProvider instanceof LettuceRedisClientProvider) {
abstractRedisClient = ((LettuceRedisClientProvider) clientProvider).getClient();
} else if (clientProvider instanceof LettuceRedisClusterClientProvider) {
abstractRedisClient = ((LettuceRedisClusterClientProvider) clientProvider).getClient();
assumeThat(((LettuceRedisClusterClientProvider) clientProvider).test(), is(true));
}
if (abstractRedisClient instanceof RedisClient) {
nativeCommands = ((RedisClient) abstractRedisClient).connect().sync();
connection = new LettuceReactiveRedisConnection(abstractRedisClient);
} else if (abstractRedisClient instanceof RedisClusterClient) {
nativeCommands = ((RedisClusterClient) abstractRedisClient).connect().sync();
connection = new LettuceReactiveRedisClusterConnection((RedisClusterClient) abstractRedisClient);
}
}
@After
public void tearDown() {
if (nativeCommands != null) {
flushAll();
nativeCommands.close();
}
if (connection != null) {
connection.close();
}
}
private void flushAll() {
nativeCommands.flushall();
}
}

View File

@@ -0,0 +1,275 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.collection.IsCollectionWithSize.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNull.*;
import static org.hamcrest.number.IsCloseTo.*;
import static org.junit.Assert.*;
import static org.springframework.data.redis.connection.RedisGeoCommands.DistanceUnit.*;
import static org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveGeoCommandsTests extends LettuceReactiveCommandsTestsBase {
private static final String ARIGENTO_MEMBER_NAME = "arigento";
private static final String CATANIA_MEMBER_NAME = "catania";
private static final String PALERMO_MEMBER_NAME = "palermo";
private static final Point POINT_ARIGENTO = new Point(13.583333, 37.316667);
private static final Point POINT_CATANIA = new Point(15.087269, 37.502669);
private static final Point POINT_PALERMO = new Point(13.361389, 38.115556);
private static final GeoLocation<ByteBuffer> ARIGENTO = new GeoLocation<>(
ByteBuffer.wrap(ARIGENTO_MEMBER_NAME.getBytes(Charset.forName("UTF-8"))), POINT_ARIGENTO);
private static final GeoLocation<ByteBuffer> CATANIA = new GeoLocation<>(
ByteBuffer.wrap(CATANIA_MEMBER_NAME.getBytes(Charset.forName("UTF-8"))), POINT_CATANIA);
private static final GeoLocation<ByteBuffer> PALERMO = new GeoLocation<>(
ByteBuffer.wrap(PALERMO_MEMBER_NAME.getBytes(Charset.forName("UTF-8"))), POINT_PALERMO);
/**
* @see DATAREDIS-525
*/
@Test
public void geoAddShouldAddSingleGeoLocationCorrectly() {
assertThat(connection.geoCommands().geoAdd(KEY_1_BBUFFER, ARIGENTO).block(), is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoAddShouldAddMultipleGeoLocationsCorrectly() {
assertThat(connection.geoCommands().geoAdd(KEY_1_BBUFFER, Arrays.asList(ARIGENTO, CATANIA, PALERMO)).block(),
is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoDistShouldReturnDistanceInMetersByDefault() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
assertThat(connection.geoCommands().geoDist(KEY_1_BBUFFER, PALERMO.getName(), CATANIA.getName()).block().getValue(),
is(closeTo(166274.15156960033D, 0.005)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoDistShouldReturnDistanceInDesiredMetric() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
assertThat(connection.geoCommands().geoDist(KEY_1_BBUFFER, PALERMO.getName(), CATANIA.getName(), Metrics.KILOMETERS)
.block().getValue(), is(closeTo(166.27415156960033D, 0.005)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoHash() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
assertThat(
connection.geoCommands().geoHash(KEY_1_BBUFFER, Arrays.asList(PALERMO.getName(), CATANIA.getName())).block(),
contains("sqc8b49rny0", "sqdtr74hyu0"));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoHashNotExisting() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
assertThat(
connection.geoCommands()
.geoHash(KEY_1_BBUFFER, Arrays.asList(PALERMO.getName(), ARIGENTO.getName(), CATANIA.getName())).block(),
contains("sqc8b49rny0", null, "sqdtr74hyu0"));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoPos() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
List<Point> result = connection.geoCommands()
.geoPos(KEY_1_BBUFFER, Arrays.asList(PALERMO.getName(), CATANIA.getName())).block();
assertThat(result.get(0).getX(), is(closeTo(POINT_PALERMO.getX(), 0.005)));
assertThat(result.get(0).getY(), is(closeTo(POINT_PALERMO.getY(), 0.005)));
assertThat(result.get(1).getX(), is(closeTo(POINT_CATANIA.getX(), 0.005)));
assertThat(result.get(1).getY(), is(closeTo(POINT_CATANIA.getY(), 0.005)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoPosNonExisting() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
List<Point> result = connection.geoCommands()
.geoPos(KEY_1_BBUFFER, Arrays.asList(PALERMO.getName(), ARIGENTO.getName(), CATANIA.getName())).block();
assertThat(result.get(0).getX(), is(closeTo(POINT_PALERMO.getX(), 0.005)));
assertThat(result.get(0).getY(), is(closeTo(POINT_PALERMO.getY(), 0.005)));
assertThat(result.get(1), is(nullValue()));
assertThat(result.get(2).getX(), is(closeTo(POINT_CATANIA.getX(), 0.005)));
assertThat(result.get(2).getY(), is(closeTo(POINT_CATANIA.getY(), 0.005)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoRadiusShouldReturnMembersCorrectly() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, ARIGENTO.getPoint().getX(), ARIGENTO.getPoint().getY(), ARIGENTO_MEMBER_NAME);
assertThat(
connection.geoCommands()
.geoRadius(KEY_1_BBUFFER, new Circle(new Point(15D, 37D), new Distance(200D, KILOMETERS))).block(),
hasSize(3));
assertThat(
connection.geoCommands()
.geoRadius(KEY_1_BBUFFER, new Circle(new Point(15D, 37D), new Distance(150D, KILOMETERS))).block(),
hasSize(2));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoRadiusShouldReturnDistanceCorrectly() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, ARIGENTO.getPoint().getX(), ARIGENTO.getPoint().getY(), ARIGENTO_MEMBER_NAME);
GeoResults<GeoLocation<ByteBuffer>> result = connection.geoCommands().geoRadius(KEY_1_BBUFFER,
new Circle(new Point(15D, 37D), new Distance(200D, KILOMETERS)), newGeoRadiusArgs().includeDistance()).block();
assertThat(result.getContent().get(0).getDistance().getValue(), is(closeTo(130.423D, 0.005)));
assertThat(result.getContent().get(0).getDistance().getUnit(), is("km"));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoRadiusShouldApplyLimit() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, ARIGENTO.getPoint().getX(), ARIGENTO.getPoint().getY(), ARIGENTO_MEMBER_NAME);
GeoResults<GeoLocation<ByteBuffer>> result = connection.geoCommands().geoRadius(KEY_1_BBUFFER,
new Circle(new Point(15D, 37D), new Distance(200D, KILOMETERS)), newGeoRadiusArgs().limit(2)).block();
assertThat(result.getContent(), hasSize(2));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoRadiusByMemberShouldReturnMembersCorrectly() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, ARIGENTO.getPoint().getX(), ARIGENTO.getPoint().getY(), ARIGENTO_MEMBER_NAME);
List<GeoLocation<ByteBuffer>> result = connection.geoCommands()
.geoRadiusByMember(KEY_1_BBUFFER, ARIGENTO.getName(), new Distance(100, KILOMETERS)).block();
assertThat(result.get(0).getName(), is(ARIGENTO.getName()));
assertThat(result.get(1).getName(), is(PALERMO.getName()));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoRadiusByMemberShouldReturnDistanceCorrectly() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, ARIGENTO.getPoint().getX(), ARIGENTO.getPoint().getY(), ARIGENTO_MEMBER_NAME);
GeoResults<GeoLocation<ByteBuffer>> result = connection.geoCommands().geoRadiusByMember(KEY_1_BBUFFER,
PALERMO.getName(), new Distance(100, KILOMETERS), newGeoRadiusArgs().includeDistance()).block();
assertThat(result.getContent(), hasSize(2));
assertThat(result.getContent().get(0).getDistance().getValue(), is(closeTo(90.978D, 0.005)));
assertThat(result.getContent().get(0).getDistance().getUnit(), is("km"));
}
/**
* @see DATAREDIS-525
*/
@Test
public void geoRadiusByMemberShouldApplyLimit() {
nativeCommands.geoadd(KEY_1, PALERMO.getPoint().getX(), PALERMO.getPoint().getY(), PALERMO_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, CATANIA.getPoint().getX(), CATANIA.getPoint().getY(), CATANIA_MEMBER_NAME);
nativeCommands.geoadd(KEY_1, ARIGENTO.getPoint().getX(), ARIGENTO.getPoint().getY(), ARIGENTO_MEMBER_NAME);
GeoResults<GeoLocation<ByteBuffer>> result = connection.geoCommands()
.geoRadiusByMember(KEY_1_BBUFFER, PALERMO.getName(), new Distance(200, KILOMETERS), newGeoRadiusArgs().limit(2))
.block();
assertThat(result.getContent(), hasSize(2));
}
}

View File

@@ -0,0 +1,244 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.hamcrest.core.IsNull.*;
import static org.junit.Assert.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveHashCommandsTests extends LettuceReactiveCommandsTestsBase {
static final String FIELD_1 = "field-1";
static final String FIELD_2 = "field-2";
static final String FIELD_3 = "field-3";
static final byte[] FIELD_1_BYTES = FIELD_1.getBytes(Charset.forName("UTF-8"));
static final byte[] FIELD_2_BYTES = FIELD_2.getBytes(Charset.forName("UTF-8"));
static final byte[] FIELD_3_BYTES = FIELD_3.getBytes(Charset.forName("UTF-8"));
static final ByteBuffer FIELD_1_BBUFFER = ByteBuffer.wrap(FIELD_1_BYTES);
static final ByteBuffer FIELD_2_BBUFFER = ByteBuffer.wrap(FIELD_2_BYTES);
static final ByteBuffer FIELD_3_BBUFFER = ByteBuffer.wrap(FIELD_3_BYTES);
/**
* @see DATAREDIS-525
*/
@Test
public void hSetShouldOperateCorrectly() {
assertThat(connection.hashCommands().hSet(KEY_1_BBUFFER, FIELD_1_BBUFFER, VALUE_1_BBUFFER).block(), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hSetNxShouldOperateCorrectly() {
assertThat(connection.hashCommands().hSetNX(KEY_1_BBUFFER, FIELD_1_BBUFFER, VALUE_1_BBUFFER).block(), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hSetNxShouldReturnFalseIfFieldAlreadyExists() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
assertThat(connection.hashCommands().hSetNX(KEY_1_BBUFFER, FIELD_1_BBUFFER, VALUE_1_BBUFFER).block(), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hGetShouldReturnValueForExistingField() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_2, VALUE_2);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
assertThat(connection.hashCommands().hGet(KEY_1_BBUFFER, FIELD_1_BBUFFER).block(), is(equalTo(VALUE_1_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hGetShouldReturnNullForNotExistingField() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
assertThat(connection.hashCommands().hGet(KEY_1_BBUFFER, FIELD_2_BBUFFER).block(), is(nullValue()));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hMGetShouldReturnValueForFields() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_2, VALUE_2);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
assertThat(connection.hashCommands().hMGet(KEY_1_BBUFFER, Arrays.asList(FIELD_1_BBUFFER, FIELD_3_BBUFFER)).block(),
contains(VALUE_1_BBUFFER, VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hMGetShouldReturnNullValueForFieldsThatHaveNoValue() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
assertThat(connection.hashCommands()
.hMGet(KEY_1_BBUFFER, Arrays.asList(FIELD_1_BBUFFER, FIELD_2_BBUFFER, FIELD_3_BBUFFER)).block(),
contains(VALUE_1_BBUFFER, null, VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hMSetSouldSetValuesCorrectly() {
Map<ByteBuffer, ByteBuffer> fieldValues = new LinkedHashMap<>();
fieldValues.put(FIELD_1_BBUFFER, VALUE_1_BBUFFER);
fieldValues.put(FIELD_2_BBUFFER, VALUE_2_BBUFFER);
assertThat(connection.hashCommands().hMSet(KEY_1_BBUFFER, fieldValues).block(), is(true));
assertThat(nativeCommands.hget(KEY_1, FIELD_1), is(equalTo(VALUE_1)));
assertThat(nativeCommands.hget(KEY_1, FIELD_2), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hExistsShouldReturnTrueForExistingField() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
assertThat(connection.hashCommands().hExists(KEY_1_BBUFFER, FIELD_1_BBUFFER).block(), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hExistsShouldReturnFalseForNonExistingField() {
assertThat(connection.hashCommands().hExists(KEY_1_BBUFFER, FIELD_1_BBUFFER).block(), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hDelShouldRemoveSingleFieldsCorrectly() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_2, VALUE_2);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
assertThat(connection.hashCommands().hDel(KEY_1_BBUFFER, FIELD_2_BBUFFER).block(), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hDelShouldRemoveMultipleFieldsCorrectly() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_2, VALUE_2);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
assertThat(connection.hashCommands().hDel(KEY_1_BBUFFER, Arrays.asList(FIELD_1_BBUFFER, FIELD_3_BBUFFER)).block(),
is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hLenShouldReturnSizeCorrectly() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_2, VALUE_2);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
assertThat(connection.hashCommands().hLen(KEY_1_BBUFFER).block(), is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hKeysShouldReturnFieldsCorrectly() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_2, VALUE_2);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
assertThat(connection.hashCommands().hKeys(KEY_1_BBUFFER).block(),
containsInAnyOrder(FIELD_1_BBUFFER, FIELD_2_BBUFFER, FIELD_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hValsShouldReturnValuesCorrectly() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_2, VALUE_2);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
assertThat(connection.hashCommands().hVals(KEY_1_BBUFFER).block(),
containsInAnyOrder(VALUE_1_BBUFFER, VALUE_2_BBUFFER, VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hGetAlllShouldReturnEntriesCorrectly() {
nativeCommands.hset(KEY_1, FIELD_1, VALUE_1);
nativeCommands.hset(KEY_1, FIELD_2, VALUE_2);
nativeCommands.hset(KEY_1, FIELD_3, VALUE_3);
System.out.println(connection.hashCommands().hGetAll(KEY_1_BBUFFER).block());
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeThat;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.data.redis.test.util.LettuceRedisClientProvider;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveHyperLogLogCommandsTests extends LettuceReactiveCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void pfAddShouldAddToNonExistingKeyCorrectly() {
assertThat(connection.hyperLogLogCommands()
.pfAdd(KEY_1_BBUFFER, Arrays.asList(VALUE_1_BBUFFER, VALUE_2_BBUFFER, VALUE_3_BBUFFER)).block(), is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void pfAddShouldReturnZeroWhenValueAlreadyExists() {
nativeCommands.pfadd(KEY_1, new String[] { VALUE_1, VALUE_2 });
nativeCommands.pfadd(KEY_1, new String[] { VALUE_3 });
assertThat(connection.hyperLogLogCommands().pfAdd(KEY_1_BBUFFER, Arrays.asList(VALUE_1_BBUFFER)).block(), is(0L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void pfCountShouldReturnCorrectly() {
nativeCommands.pfadd(KEY_1, new String[] { VALUE_1, VALUE_2 });
assertThat(connection.hyperLogLogCommands().pfCount(KEY_1_BBUFFER).block(), is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void pfCountWithMultipleKeysShouldReturnCorrectly() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.pfadd(KEY_1, new String[] { VALUE_1, VALUE_2 });
nativeCommands.pfadd(KEY_2, new String[] { VALUE_2, VALUE_3 });
assertThat(connection.hyperLogLogCommands().pfCount(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER)).block(), is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void pfMergeShouldWorkCorrectly() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.pfadd(KEY_1, new String[] { VALUE_1, VALUE_2 });
nativeCommands.pfadd(KEY_2, new String[] { VALUE_2, VALUE_3 });
assertThat(
connection.hyperLogLogCommands().pfMerge(KEY_3_BBUFFER, Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER)).block(),
is(true));
assertThat(nativeCommands.pfcount(new String[] { KEY_3 }), is(3L));
}
}

View File

@@ -0,0 +1,226 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsCollectionWithSize.*;
import static org.junit.Assert.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyCommand;
import org.springframework.data.redis.connection.ReactiveRedisConnection.NumericResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.TestSubscriber;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveKeyCommandsTests extends LettuceReactiveCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void existsShouldReturnTrueForExistingKeys() {
nativeCommands.set(KEY_1, VALUE_1);
assertThat(connection.keyCommands().exists(KEY_1_BBUFFER).block(), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void existsShouldReturnFalseForNonExistingKeys() {
assertThat(connection.keyCommands().exists(KEY_1_BBUFFER).block(), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void typeShouldReturnTypeCorrectly() {
nativeCommands.set(KEY_1, VALUE_2);
nativeCommands.sadd(KEY_2, VALUE_2);
nativeCommands.hset(KEY_3, KEY_1, VALUE_1);
assertThat(connection.keyCommands().type(KEY_1_BBUFFER).block(), is(DataType.STRING));
assertThat(connection.keyCommands().type(KEY_2_BBUFFER).block(), is(DataType.SET));
assertThat(connection.keyCommands().type(KEY_3_BBUFFER).block(), is(DataType.HASH));
}
/**
* @see DATAREDIS-525
*/
@Test
public void keysShouldReturnCorrectly() {
nativeCommands.set(KEY_1, VALUE_2);
nativeCommands.set(KEY_2, VALUE_2);
nativeCommands.set(KEY_3, VALUE_3);
nativeCommands.set(VALUE_1, KEY_1);
nativeCommands.set(VALUE_2, KEY_2);
nativeCommands.set(VALUE_3, KEY_3);
assertThat(connection.keyCommands().keys(ByteBuffer.wrap("*".getBytes())).block(), hasSize(6));
assertThat(connection.keyCommands().keys(ByteBuffer.wrap("key*".getBytes())).block(), hasSize(3));
}
/**
* @see DATAREDIS-525
*/
@Test
public void randomKeyShouldReturnAnyKey() {
nativeCommands.set(KEY_1, VALUE_2);
nativeCommands.set(KEY_2, VALUE_2);
nativeCommands.set(KEY_3, VALUE_3);
assertThat(connection.keyCommands().randomKey().block(), is(notNullValue()));
}
/**
* @see DATAREDIS-525
*/
@Test
public void randomKeyShouldReturnNullWhenNoKeyExists() {
assertThat(connection.keyCommands().randomKey().block(), is(nullValue()));
}
/**
* @see DATAREDIS-525
*/
@Test
public void renameShouldAlterKeyNameCorrectly() {
nativeCommands.set(KEY_1, VALUE_2);
assertThat(connection.keyCommands().rename(KEY_1_BBUFFER, KEY_2_BBUFFER).block(), is(true));
assertThat(nativeCommands.exists(KEY_2), is(true));
assertThat(nativeCommands.exists(KEY_1), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test(expected = RedisSystemException.class)
public void renameShouldThrowErrorWhenKeyDoesNotExit() {
assertThat(connection.keyCommands().rename(KEY_1_BBUFFER, KEY_2_BBUFFER).block(), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void renameNXShouldAlterKeyNameCorrectly() {
nativeCommands.set(KEY_1, VALUE_2);
assertThat(connection.keyCommands().rename(KEY_1_BBUFFER, KEY_2_BBUFFER).block(), is(true));
assertThat(nativeCommands.exists(KEY_2), is(true));
assertThat(nativeCommands.exists(KEY_1), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void renameNXShouldNotAlterExistingKeyName() {
nativeCommands.set(KEY_1, VALUE_2);
nativeCommands.set(KEY_2, VALUE_2);
assertThat(connection.keyCommands().renameNX(KEY_1_BBUFFER, KEY_2_BBUFFER).block(), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void shouldDeleteKeyCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
Mono<Long> result = connection.keyCommands().del(KEY_1_BBUFFER);
assertThat(result.block(), is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void shouldDeleteKeysCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
Flux<NumericResponse<KeyCommand, Long>> result = connection.keyCommands()
.del(Flux.fromIterable(Arrays.asList(new KeyCommand(KEY_1_BBUFFER), new KeyCommand(KEY_2_BBUFFER))));
TestSubscriber<NumericResponse<KeyCommand, Long>> subscriber = TestSubscriber.create();
result.subscribe(subscriber);
subscriber.await();
subscriber.assertValueCount(2);
}
/**
* @see DATAREDIS-525
*/
@Test
public void shouldDeleteKeysInBatchCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
Mono<Long> result = connection.keyCommands().mDel(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER));
assertThat(result.block(), is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void shouldDeleteKeysInMultipleBatchesCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
Flux<Long> result = connection.keyCommands()
.mDel(
Flux.fromIterable(Arrays.asList(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), Arrays.asList(KEY_1_BBUFFER))))
.map(NumericResponse::getOutput);
TestSubscriber<Long> subscriber = TestSubscriber.create();
result.subscribe(subscriber);
subscriber.await();
subscriber.assertValueCount(2);
}
}

View File

@@ -0,0 +1,320 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.hamcrest.core.IsNot.*;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeThat;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.Arrays;
import org.junit.Assume;
import org.junit.Test;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.connection.ReactiveListCommands.PopResult;
import org.springframework.data.redis.connection.ReactiveListCommands.PushCommand;
import org.springframework.data.redis.connection.RedisListCommands.Position;
import org.springframework.data.redis.test.util.LettuceRedisClientProvider;
import reactor.core.publisher.Mono;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveListCommandTests extends LettuceReactiveCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void rPushShouldAppendValuesCorrectly() {
nativeCommands.lpush(KEY_1, VALUE_1);
assertThat(connection.listCommands().rPush(KEY_1_BBUFFER, Arrays.asList(VALUE_2_BBUFFER, VALUE_3_BBUFFER)).block(),
is(3L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_1, VALUE_2, VALUE_3));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lPushShouldPrependValuesCorrectly() {
nativeCommands.lpush(KEY_1, VALUE_1);
assertThat(connection.listCommands().lPush(KEY_1_BBUFFER, Arrays.asList(VALUE_2_BBUFFER, VALUE_3_BBUFFER)).block(),
is(3L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_3, VALUE_2, VALUE_1));
}
/**
* @see DATAREDIS-525
*/
@Test
public void rPushXShouldAppendValuesCorrectly() {
nativeCommands.lpush(KEY_1, VALUE_1);
assertThat(connection.listCommands().rPushX(KEY_1_BBUFFER, VALUE_2_BBUFFER).block(), is(2L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_1, VALUE_2));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lPushXShouldPrependValuesCorrectly() {
nativeCommands.lpush(KEY_1, VALUE_1);
assertThat(connection.listCommands().lPushX(KEY_1_BBUFFER, VALUE_2_BBUFFER).block(), is(2L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_2, VALUE_1));
}
/**
* @see DATAREDIS-525
*/
@Test(expected = InvalidDataAccessApiUsageException.class)
public void pushShouldThrowErrorForMoreThanOneValueWhenUsingExistsOption() {
connection.listCommands()
.push(Mono.just(
PushCommand.right().values(Arrays.asList(VALUE_1_BBUFFER, VALUE_2_BBUFFER)).to(KEY_1_BBUFFER).ifExists()))
.blockFirst();
}
/**
* @see DATAREDIS-525
*/
@Test
public void lLenShouldReturnSizeCorrectly() {
nativeCommands.lpush(KEY_1, VALUE_1, VALUE_2);
assertThat(connection.listCommands().lLen(KEY_1_BBUFFER).block(), is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lRangeShouldReturnValuesCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.listCommands().lRange(KEY_1_BBUFFER, 1, 2).block(),
contains(VALUE_2_BBUFFER, VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lTrimShouldReturnValuesCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.listCommands().lTrim(KEY_1_BBUFFER, 1, 2).block(), is(true));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), not(contains(VALUE_1_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lIndexShouldReturnValueCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.listCommands().lIndex(KEY_1_BBUFFER, 1).block(), is(equalTo(VALUE_2_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lInsertShouldAddValueCorrectlyBeforeExisting() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2);
assertThat(
connection.listCommands().lInsert(KEY_1_BBUFFER, Position.BEFORE, VALUE_2_BBUFFER, VALUE_3_BBUFFER).block(),
is(3L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_1, VALUE_3, VALUE_2));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lInsertShouldAddValueCorrectlyAfterExisting() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2);
assertThat(
connection.listCommands().lInsert(KEY_1_BBUFFER, Position.AFTER, VALUE_2_BBUFFER, VALUE_3_BBUFFER).block(),
is(3L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_1, VALUE_2, VALUE_3));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lSetSouldSetValueCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2);
assertThat(connection.listCommands().lSet(KEY_1_BBUFFER, 1L, VALUE_3_BBUFFER).block(), is(true));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_1, VALUE_3));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), not(contains(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lRemSouldRemoveAllValuesCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_1, VALUE_3);
assertThat(connection.listCommands().lRem(KEY_1_BBUFFER, VALUE_1_BBUFFER).block(), is(2L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_2, VALUE_3));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), not(contains(VALUE_1)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lRemSouldRemoveFirstValuesCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_1, VALUE_3);
assertThat(connection.listCommands().lRem(KEY_1_BBUFFER, 1L, VALUE_1_BBUFFER).block(), is(1L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_2, VALUE_1, VALUE_3));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lRemSouldRemoveLastValuesCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_1, VALUE_3);
assertThat(connection.listCommands().lRem(KEY_1_BBUFFER, -1L, VALUE_1_BBUFFER).block(), is(1L));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_1, VALUE_2, VALUE_3));
}
/**
* @see DATAREDIS-525
*/
@Test
public void lPopSouldRemoveFirstValueCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.listCommands().lPop(KEY_1_BBUFFER).block(), is(equalTo(VALUE_1_BBUFFER)));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_2, VALUE_3));
}
/**
* @see DATAREDIS-525
*/
@Test
public void rPopSouldRemoveFirstValueCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.listCommands().rPop(KEY_1_BBUFFER).block(), is(equalTo(VALUE_3_BBUFFER)));
assertThat(nativeCommands.lrange(KEY_1, 0, -1), contains(VALUE_1, VALUE_2));
}
/**
* @see DATAREDIS-525
*/
@Test
public void blPopShouldReturnFirstAvailable() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
PopResult result = connection.listCommands()
.blPop(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), Duration.ofSeconds(1L)).block();
assertThat(result.getKey(), is(equalTo(KEY_1_BBUFFER)));
assertThat(result.getValue(), is(equalTo(VALUE_1_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void brPopShouldReturnLastAvailable() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
PopResult result = connection.listCommands()
.brPop(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), Duration.ofSeconds(1L)).block();
assertThat(result.getKey(), is(equalTo(KEY_1_BBUFFER)));
assertThat(result.getValue(), is(equalTo(VALUE_3_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void rPopLPushShouldWorkCorrectly() {
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
nativeCommands.rpush(KEY_2, VALUE_1);
ByteBuffer result = connection.listCommands().rPopLPush(KEY_1_BBUFFER, KEY_2_BBUFFER).block();
assertThat(result, is(equalTo(VALUE_3_BBUFFER)));
assertThat(nativeCommands.llen(KEY_2), is(2L));
assertThat(nativeCommands.lindex(KEY_2, 0), is(equalTo(VALUE_3)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void brPopLPushShouldWorkCorrectly() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.rpush(KEY_1, VALUE_1, VALUE_2, VALUE_3);
nativeCommands.rpush(KEY_2, VALUE_1);
ByteBuffer result = connection.listCommands().bRPopLPush(KEY_1_BBUFFER, KEY_2_BBUFFER, Duration.ofSeconds(1))
.block();
assertThat(result, is(equalTo(VALUE_3_BBUFFER)));
assertThat(nativeCommands.llen(KEY_2), is(2L));
assertThat(nativeCommands.lindex(KEY_2, 0), is(equalTo(VALUE_3)));
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.number.IsCloseTo.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveNumberCommandsTests extends LettuceReactiveCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void incrByDoubleShouldIncreaseValueCorrectly() {
assertThat(connection.numberCommands().incrBy(KEY_1_BBUFFER, 1.5D).block(), is(closeTo(1.5D, 0D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void incrByIntegerShouldIncreaseValueCorrectly() {
assertThat(connection.numberCommands().incrBy(KEY_1_BBUFFER, 3).block(), is(3));
}
/**
* @see DATAREDIS-525
*/
@Test
public void decrByDoubleShouldDecreaseValueCorrectly() {
assertThat(connection.numberCommands().decrBy(KEY_1_BBUFFER, 1.5D).block(), is(closeTo(-1.5D, 0D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void decrByIntegerShouldDecreaseValueCorrectly() {
assertThat(connection.numberCommands().decrBy(KEY_1_BBUFFER, 3).block(), is(-3));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hIncrByDoubleShouldIncreaseValueCorrectly() {
nativeCommands.hset(KEY_1, KEY_1, "2");
assertThat(connection.numberCommands().hIncrBy(KEY_1_BBUFFER, KEY_1_BBUFFER, 1.5D).block(), is(closeTo(3.5D, 0D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void hIncrByIntegerShouldIncreaseValueCorrectly() {
nativeCommands.hset(KEY_1, KEY_1, "2");
assertThat(connection.numberCommands().hIncrBy(KEY_1_BBUFFER, KEY_1_BBUFFER, 3).block(), is(5));
}
}

View File

@@ -0,0 +1,286 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.AnyOf.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.hamcrest.core.IsNot.*;
import static org.hamcrest.core.IsNull.*;
import static org.junit.Assert.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class LettuceReactiveSetCommandsTests extends LettuceReactiveCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void sAddShouldAddSingleValue() {
assertThat(connection.setCommands().sAdd(KEY_1_BBUFFER, VALUE_1_BBUFFER).block(), is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sAddShouldAddValues() {
assertThat(connection.setCommands().sAdd(KEY_1_BBUFFER, Arrays.asList(VALUE_1_BBUFFER, VALUE_2_BBUFFER)).block(),
is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sRemShouldRemoveSingleValue() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sRem(KEY_1_BBUFFER, VALUE_1_BBUFFER).block(), is(1L));
assertThat(nativeCommands.sismember(KEY_1, VALUE_1), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sRemShouldRemoveValues() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sRem(KEY_1_BBUFFER, Arrays.asList(VALUE_1_BBUFFER, VALUE_2_BBUFFER)).block(),
is(2L));
assertThat(nativeCommands.sismember(KEY_1, VALUE_1), is(false));
assertThat(nativeCommands.sismember(KEY_1, VALUE_2), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sPopShouldRetrieveRandomValue() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sPop(KEY_1_BBUFFER).block(), is(notNullValue()));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sPopShouldReturnNullWhenNotPresent() {
assertThat(connection.setCommands().sPop(KEY_1_BBUFFER).block(), is(nullValue()));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sMoveShouldMoveValueCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
nativeCommands.sadd(KEY_2, VALUE_1);
assertThat(connection.setCommands().sMove(KEY_1_BBUFFER, KEY_2_BBUFFER, VALUE_3_BBUFFER).block(), is(true));
assertThat(nativeCommands.sismember(KEY_2, VALUE_3), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sMoveShouldReturnFalseIfValueIsNotAMember() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
nativeCommands.sadd(KEY_2, VALUE_1);
assertThat(connection.setCommands().sMove(KEY_1_BBUFFER, KEY_2_BBUFFER, VALUE_3_BBUFFER).block(), is(false));
assertThat(nativeCommands.sismember(KEY_2, VALUE_3), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sMoveShouldReturnOperateCorrectlyWhenValueAlreadyPresentInTarget() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
nativeCommands.sadd(KEY_2, VALUE_1, VALUE_3);
assertThat(connection.setCommands().sMove(KEY_1_BBUFFER, KEY_2_BBUFFER, VALUE_3_BBUFFER).block(), is(true));
assertThat(nativeCommands.sismember(KEY_1, VALUE_3), is(false));
assertThat(nativeCommands.sismember(KEY_2, VALUE_3), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sCardShouldCountValuesCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sCard(KEY_1_BBUFFER).block(), is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sIsMemberShouldReturnTrueWhenValueContainedInKey() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
assertThat(connection.setCommands().sIsMember(KEY_1_BBUFFER, VALUE_1_BBUFFER).block(), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sIsMemberShouldReturnFalseWhenValueNotContainedInKey() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
assertThat(connection.setCommands().sIsMember(KEY_1_BBUFFER, VALUE_3_BBUFFER).block(), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sInterShouldIntersectSetsCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
nativeCommands.sadd(KEY_2, VALUE_2, VALUE_3);
List<ByteBuffer> result = connection.setCommands().sInter(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER)).block();
assertThat(result, contains(VALUE_2_BBUFFER));
assertThat(result, not(containsInAnyOrder(VALUE_1_BBUFFER, VALUE_3_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sInterStoreShouldReturnSizeCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
nativeCommands.sadd(KEY_2, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sInterStore(KEY_3_BBUFFER, Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER)).block(),
is(1L));
assertThat(nativeCommands.sismember(KEY_3, VALUE_2), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sUnionShouldCombineSetsCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
nativeCommands.sadd(KEY_2, VALUE_2, VALUE_3);
List<ByteBuffer> result = connection.setCommands().sUnion(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER)).block();
assertThat(result, containsInAnyOrder(VALUE_1_BBUFFER, VALUE_3_BBUFFER, VALUE_2_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sUnionStoreShouldReturnSizeCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
nativeCommands.sadd(KEY_2, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sUnionStore(KEY_3_BBUFFER, Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER)).block(),
is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sDiffShouldBeExcecutedCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
nativeCommands.sadd(KEY_2, VALUE_2, VALUE_3);
List<ByteBuffer> result = connection.setCommands().sDiff(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER)).block();
assertThat(result, containsInAnyOrder(VALUE_1_BBUFFER));
assertThat(result, not(containsInAnyOrder(VALUE_2_BBUFFER, VALUE_3_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sDiffStoreShouldBeExcecutedCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2);
nativeCommands.sadd(KEY_2, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sDiffStore(KEY_3_BBUFFER, Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER)).block(),
is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sMembersReadsValuesFromSetCorrectly() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sMembers(KEY_1_BBUFFER).block(),
containsInAnyOrder(VALUE_1_BBUFFER, VALUE_2_BBUFFER, VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sRandMemberReturnsRandomMember() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sRandMember(KEY_1_BBUFFER).block(),
anyOf(equalTo(VALUE_1_BBUFFER), equalTo(VALUE_2_BBUFFER), equalTo(VALUE_3_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void sRandMemberReturnsRandomMembers() {
nativeCommands.sadd(KEY_1, VALUE_1, VALUE_2, VALUE_3);
assertThat(connection.setCommands().sRandMember(KEY_1_BBUFFER, 2L).block().size(), is(2));
}
}

View File

@@ -0,0 +1,448 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsEqual.*;
import static org.hamcrest.core.IsNull.*;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeThat;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.data.redis.connection.ReactiveRedisConnection.BooleanResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.ByteBufferResponse;
import org.springframework.data.redis.connection.ReactiveRedisConnection.KeyCommand;
import org.springframework.data.redis.connection.ReactiveRedisConnection.MultiValueResponse;
import org.springframework.data.redis.connection.ReactiveStringCommands.SetCommand;
import org.springframework.data.redis.connection.RedisStringCommands.BitOperation;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.test.util.LettuceRedisClientProvider;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.TestSubscriber;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveStringCommandsTests extends LettuceReactiveCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void getSetShouldReturnPreviousValueCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
Mono<ByteBuffer> result = connection.stringCommands().getSet(KEY_1_BBUFFER, VALUE_2_BBUFFER);
assertThat(result.block(), is(equalTo(VALUE_1_BBUFFER)));
assertThat(nativeCommands.get(KEY_1), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void getSetShouldReturnPreviousValueCorrectlyWhenNoExists() {
Mono<ByteBuffer> result = connection.stringCommands().getSet(KEY_1_BBUFFER, VALUE_2_BBUFFER);
ByteBuffer value = result.block();
assertThat(value, is(notNullValue()));
assertThat(value, is(equalTo(ByteBuffer.allocate(0))));
assertThat(nativeCommands.get(KEY_1), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void setShouldAddValueCorrectly() {
Mono<Boolean> result = connection.stringCommands().set(KEY_1_BBUFFER, VALUE_1_BBUFFER);
assertThat(result.block(), is(true));
assertThat(nativeCommands.get(KEY_1), is(equalTo(VALUE_1)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void setShouldAddValuesCorrectly() {
Flux<BooleanResponse<SetCommand>> result = connection.stringCommands()
.set(Flux.fromIterable(Arrays.asList(SetCommand.set(KEY_1_BBUFFER).value(VALUE_1_BBUFFER),
SetCommand.set(KEY_2_BBUFFER).value(VALUE_2_BBUFFER))));
TestSubscriber<BooleanResponse<SetCommand>> subscriber = TestSubscriber.create();
result.subscribe(subscriber);
subscriber.await();
subscriber.assertValueCount(2);
assertThat(nativeCommands.get(KEY_1), is(equalTo(VALUE_1)));
assertThat(nativeCommands.get(KEY_2), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void getShouldRetriveValueCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
Mono<ByteBuffer> result = connection.stringCommands().get(KEY_1_BBUFFER);
assertThat(result.block(), is(equalTo(VALUE_1_BBUFFER)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void getShouldRetriveNullValueCorrectly() {
Mono<ByteBuffer> result = connection.stringCommands().get(KEY_1_BBUFFER);
assertThat(result.block(), is(equalTo(ByteBuffer.allocate(0))));
}
/**
* @see DATAREDIS-525
*/
@Test
public void getShouldRetriveValuesCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
Flux<ByteBufferResponse<KeyCommand>> result = connection.stringCommands()
.get(Flux.fromStream(Arrays.asList(new KeyCommand(KEY_1_BBUFFER), new KeyCommand(KEY_2_BBUFFER)).stream()));
TestSubscriber<ByteBufferResponse<KeyCommand>> subscriber = TestSubscriber.create();
result.subscribe(subscriber);
subscriber.await();
subscriber.assertValueCount(2);
}
/**
* @see DATAREDIS-525
*/
@Test
public void getShouldRetriveValuesWithNullCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_3, VALUE_3);
Flux<ByteBufferResponse<KeyCommand>> result = connection.stringCommands().get(Flux.fromStream(Arrays
.asList(new KeyCommand(KEY_1_BBUFFER), new KeyCommand(KEY_2_BBUFFER), new KeyCommand(KEY_3_BBUFFER)).stream()));
TestSubscriber<ByteBufferResponse<KeyCommand>> subscriber = TestSubscriber.create();
result.subscribe(subscriber);
subscriber.await();
subscriber.assertValueCount(3);
}
/**
* @see DATAREDIS-525
*/
@Test
public void mGetShouldRetriveValueCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
Mono<List<ByteBuffer>> result = connection.stringCommands().mGet(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER));
assertThat(result.block(), contains(VALUE_1_BBUFFER, VALUE_2_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void mGetShouldRetriveNullValueCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_3, VALUE_3);
Mono<List<ByteBuffer>> result = connection.stringCommands()
.mGet(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER, KEY_3_BBUFFER));
assertThat(result.block(), contains(VALUE_1_BBUFFER, ByteBuffer.allocate(0), VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void mGetShouldRetriveValuesCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
Flux<List<ByteBuffer>> result = connection.stringCommands()
.mGet(
Flux.fromIterable(Arrays.asList(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), Arrays.asList(KEY_2_BBUFFER))))
.map(MultiValueResponse::getOutput);
TestSubscriber<List<ByteBuffer>> subscriber = TestSubscriber.create();
result.subscribe(subscriber);
subscriber.await();
subscriber.assertValueCount(2);
subscriber.assertContainValues(
new HashSet<>(Arrays.asList(Arrays.asList(VALUE_1_BBUFFER, VALUE_2_BBUFFER), Arrays.asList(VALUE_2_BBUFFER))));
}
/**
* @see DATAREDIS-525
*/
@Test
public void setNXshouldOnlySetValueWhenNotPresent() {
assertThat(connection.stringCommands().setNX(KEY_1_BBUFFER, VALUE_1_BBUFFER).block(), is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void setNXshouldNotSetValueWhenAlreadyPresent() {
nativeCommands.setnx(KEY_1, VALUE_1);
assertThat(connection.stringCommands().setNX(KEY_1_BBUFFER, VALUE_2_BBUFFER).block(), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void setEXshouldSetKeyAndExpirationTime() {
connection.stringCommands().setEX(KEY_1_BBUFFER, VALUE_1_BBUFFER, Expiration.seconds(3)).block();
assertThat(nativeCommands.ttl(KEY_1) > 1, is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void pSetEXshouldSetKeyAndExpirationTime() {
connection.stringCommands().pSetEX(KEY_1_BBUFFER, VALUE_1_BBUFFER, Expiration.milliseconds(600)).block();
assertThat(nativeCommands.pttl(KEY_1) > 1, is(true));
}
/**
* @see DATAREDIS-525
*/
@Test
public void mSetShouldAddMultipleKeyValueParis() {
Map<ByteBuffer, ByteBuffer> map = new LinkedHashMap<>();
map.put(KEY_1_BBUFFER, VALUE_1_BBUFFER);
map.put(KEY_2_BBUFFER, VALUE_2_BBUFFER);
connection.stringCommands().mSet(map).block();
assertThat(nativeCommands.get(KEY_1), is(equalTo(VALUE_1)));
assertThat(nativeCommands.get(KEY_2), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void mSetNXShouldAddMultipleKeyValueParis() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
Map<ByteBuffer, ByteBuffer> map = new LinkedHashMap<>();
map.put(KEY_1_BBUFFER, VALUE_1_BBUFFER);
map.put(KEY_2_BBUFFER, VALUE_2_BBUFFER);
connection.stringCommands().mSetNX(map).block();
assertThat(nativeCommands.get(KEY_1), is(equalTo(VALUE_1)));
assertThat(nativeCommands.get(KEY_2), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void mSetNXShouldNotAddMultipleKeyValueParisWhenAlreadyExit() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.set(KEY_2, VALUE_2);
Map<ByteBuffer, ByteBuffer> map = new LinkedHashMap<>();
map.put(KEY_1_BBUFFER, VALUE_1_BBUFFER);
map.put(KEY_2_BBUFFER, VALUE_2_BBUFFER);
assertThat(connection.stringCommands().mSetNX(map).block(), is(false));
assertThat(nativeCommands.exists(KEY_1), is(false));
assertThat(nativeCommands.get(KEY_2), is(equalTo(VALUE_2)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void appendShouldDoItsThing() {
assertThat(connection.stringCommands().append(KEY_1_BBUFFER, VALUE_1_BBUFFER).block(), is(7L));
assertThat(connection.stringCommands().append(KEY_1_BBUFFER, VALUE_2_BBUFFER).block(), is(14L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void getRangeShouldReturnSubstringCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
assertThat(connection.stringCommands().getRange(KEY_1_BBUFFER, 2, 3).block(),
is(equalTo(ByteBuffer.wrap("lu".getBytes()))));
}
/**
* @see DATAREDIS-525
*/
@Test
public void setRangeShouldReturnNewStringLengthCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
assertThat(connection.stringCommands().setRange(KEY_1_BBUFFER, VALUE_2_BBUFFER, 3).block(), is(10L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void getBitShouldReturnValueCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
assertThat(connection.stringCommands().getBit(KEY_1_BBUFFER, 1).block(), is(true));
assertThat(connection.stringCommands().getBit(KEY_1_BBUFFER, 7).block(), is(false));
}
/**
* @see DATAREDIS-525
*/
@Test
public void setBitShouldReturnValueCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
assertThat(connection.stringCommands().setBit(KEY_1_BBUFFER, 1, false).block(), is(true));
assertThat(nativeCommands.getbit(KEY_1, 1), is(0L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void bitCountShouldReturnValueCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
assertThat(connection.stringCommands().bitCount(KEY_1_BBUFFER).block(), is(28L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void bitCountShouldCountInRangeCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
assertThat(connection.stringCommands().bitCount(KEY_1_BBUFFER, 2, 4).block(), is(13L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void bitOpAndShouldWorkAsExpected() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
assertThat(connection.stringCommands()
.bitOp(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), BitOperation.AND, KEY_3_BBUFFER).block(), is(7L));
assertThat(nativeCommands.get(KEY_3), is(equalTo("value-0")));
}
/**
* @see DATAREDIS-525
*/
@Test
public void bitOpOrShouldWorkAsExpected() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.set(KEY_1, VALUE_1);
nativeCommands.set(KEY_2, VALUE_2);
assertThat(connection.stringCommands()
.bitOp(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), BitOperation.OR, KEY_3_BBUFFER).block(), is(7L));
assertThat(nativeCommands.get(KEY_3), is(equalTo(VALUE_3)));
}
/**
* @see DATAREDIS-525
*/
@Test(expected = IllegalArgumentException.class)
public void bitNotShouldThrowExceptionWhenMoreThanOnSourceKey() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
connection.stringCommands().bitOp(Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), BitOperation.NOT, KEY_3_BBUFFER)
.block();
}
/**
* @see DATAREDIS-525
*/
@Test
public void strLenShouldReturnValueCorrectly() {
nativeCommands.set(KEY_1, VALUE_1);
assertThat(connection.stringCommands().strLen(KEY_1_BBUFFER).block(), is(7L));
}
}

View File

@@ -0,0 +1,594 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeThat;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.Test;
import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.DefaultTuple;
import org.springframework.data.redis.test.util.LettuceRedisClientProvider;
/**
* @author Christoph Strobl
*/
public class LettuceReactiveZSetCommandsTests extends LettuceReactiveCommandsTestsBase {
/**
* @see DATAREDIS-525
*/
@Test
public void zAddShouldAddValuesWithScores() {
assertThat(connection.zSetCommands().zAdd(KEY_1_BBUFFER, 3.5D, VALUE_1_BBUFFER).block(), is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRemShouldRemoveValuesFromSet() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRem(KEY_1_BBUFFER, Arrays.asList(VALUE_1_BBUFFER, VALUE_3_BBUFFER)).block(),
is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zIncrByShouldInreaseAndReturnScore() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
assertThat(connection.zSetCommands().zIncrBy(KEY_1_BBUFFER, 3.5D, VALUE_1_BBUFFER).block(), is(4.5D));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRankShouldReturnIndexCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRank(KEY_1_BBUFFER, VALUE_3_BBUFFER).block(), is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRankShouldReturnIndexCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRevRank(KEY_1_BBUFFER, VALUE_3_BBUFFER).block(), is(0L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeShouldReturnValuesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRange(KEY_1_BBUFFER, new Range<Long>(1L, 2L)).block(),
IsIterableContainingInOrder.contains(VALUE_2_BBUFFER, VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeWithScoreShouldReturnTuplesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRangeWithScores(KEY_1_BBUFFER, new Range<Long>(1L, 2L)).block(),
IsIterableContainingInOrder.contains(new DefaultTuple(VALUE_2_BBUFFER.array(), 2D),
new DefaultTuple(VALUE_3_BBUFFER.array(), 3D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRangeShouldReturnValuesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRevRange(KEY_1_BBUFFER, new Range<Long>(1L, 2L)).block(),
IsIterableContainingInOrder.contains(VALUE_2_BBUFFER, VALUE_1_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRangeWithScoreShouldReturnTuplesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRevRangeWithScores(KEY_1_BBUFFER, new Range<Long>(1L, 2L)).block(),
IsIterableContainingInOrder.contains(new DefaultTuple(VALUE_2_BBUFFER.array(), 2D),
new DefaultTuple(VALUE_1_BBUFFER.array(), 1D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeByScoreShouldReturnValuesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRangeByScore(KEY_1_BBUFFER, new Range<>(2D, 3D)).block(),
IsIterableContainingInOrder.contains(VALUE_2_BBUFFER, VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeByScoreShouldReturnValuesCorrectlyWithMinExclusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRangeByScore(KEY_1_BBUFFER, new Range<>(2D, 3D, false, true)).block(),
IsIterableContainingInOrder.contains(VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeByScoreShouldReturnValuesCorrectlyWithMaxExclusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRangeByScore(KEY_1_BBUFFER, new Range<>(2D, 3D, true, false)).block(),
IsIterableContainingInOrder.contains(VALUE_2_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeByScoreWithScoreShouldReturnTuplesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRangeByScoreWithScores(KEY_1_BBUFFER, new Range<>(2D, 3D)).block(),
IsIterableContainingInOrder.contains(new DefaultTuple(VALUE_2_BBUFFER.array(), 2D),
new DefaultTuple(VALUE_3_BBUFFER.array(), 3D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeByScoreWithScoreShouldReturnTuplesCorrectlyWithMinExclusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(
connection.zSetCommands().zRangeByScoreWithScores(KEY_1_BBUFFER, new Range<>(2D, 3D, false, true)).block(),
IsIterableContainingInOrder.contains(new DefaultTuple(VALUE_3_BBUFFER.array(), 3D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeByScoreWithScoreShouldReturnTuplesCorrectlyWithMaxExclusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(
connection.zSetCommands().zRangeByScoreWithScores(KEY_1_BBUFFER, new Range<>(2D, 3D, true, false)).block(),
IsIterableContainingInOrder.contains(new DefaultTuple(VALUE_2_BBUFFER.array(), 2D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRangeByScoreShouldReturnValuesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRevRangeByScore(KEY_1_BBUFFER, new Range<>(3D, 2D)).block(),
IsIterableContainingInOrder.contains(VALUE_3_BBUFFER, VALUE_2_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRangeByScoreShouldReturnValuesCorrectlyWithMinExclusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRevRangeByScore(KEY_1_BBUFFER, new Range<>(3D, 2D, true, false)).block(),
IsIterableContainingInOrder.contains(VALUE_3_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRangeByScoreShouldReturnValuesCorrectlyWithMaxExclusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRevRangeByScore(KEY_1_BBUFFER, new Range<>(3D, 2D, false, true)).block(),
IsIterableContainingInOrder.contains(VALUE_2_BBUFFER));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRangeByScoreWithScoreShouldReturnTuplesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRevRangeByScoreWithScores(KEY_1_BBUFFER, new Range<>(3D, 2D)).block(),
IsIterableContainingInOrder.contains(new DefaultTuple(VALUE_3_BBUFFER.array(), 3D),
new DefaultTuple(VALUE_2_BBUFFER.array(), 2D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRangeByScoreWithScoreShouldReturnTuplesCorrectlyWithMinExclusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(
connection.zSetCommands().zRevRangeByScoreWithScores(KEY_1_BBUFFER, new Range<>(3D, 2D, true, false)).block(),
IsIterableContainingInOrder.contains(new DefaultTuple(VALUE_3_BBUFFER.array(), 3D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRevRangeByScoreWithScoreShouldReturnTuplesCorrectlyWithMaxExclusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(
connection.zSetCommands().zRevRangeByScoreWithScores(KEY_1_BBUFFER, new Range<>(3D, 2D, false, true)).block(),
IsIterableContainingInOrder.contains(new DefaultTuple(VALUE_2_BBUFFER.array(), 2D)));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zCountShouldCountValuesInRange() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zCount(KEY_1_BBUFFER, new Range<>(2D, 3D)).block(), is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zCountShouldCountValuesInRangeWithMinExlusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zCount(KEY_1_BBUFFER, new Range<>(2D, 3D, false, true)).block(), is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zCountShouldCountValuesInRangeWithMaxExlusion() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zCount(KEY_1_BBUFFER, new Range<>(2D, 3D, true, false)).block(), is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zCountShouldCountValuesInRangeWithNegativeInfinity() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zCount(KEY_1_BBUFFER, new Range<>(Double.NEGATIVE_INFINITY, 2D)).block(),
is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zCountShouldCountValuesInRangeWithPositiveInfinity() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zCount(KEY_1_BBUFFER, new Range<>(2D, Double.POSITIVE_INFINITY)).block(),
is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zCardShouldReturnSizeCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zCard(KEY_1_BBUFFER).block(), is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zScoreShouldReturnScoreCorrectly() {
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
assertThat(connection.zSetCommands().zScore(KEY_1_BBUFFER, VALUE_2_BBUFFER).block(), is(2D));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRemRangeByRankShouldRemoveValuesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRemRangeByRank(KEY_1_BBUFFER, new Range<>(1L, 2L)).block(), is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRemRangeByScoreShouldRemoveValuesCorrectly() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRemRangeByScore(KEY_1_BBUFFER, new Range<>(1D, 2D)).block(), is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRemRangeByScoreShouldRemoveValuesCorrectlyWithNegativeInfinity() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(
connection.zSetCommands().zRemRangeByScore(KEY_1_BBUFFER, new Range<>(Double.NEGATIVE_INFINITY, 2D)).block(),
is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRemRangeByScoreShouldRemoveValuesCorrectlyWithPositiveInfinity() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(
connection.zSetCommands().zRemRangeByScore(KEY_1_BBUFFER, new Range<>(2D, Double.POSITIVE_INFINITY)).block(),
is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRemRangeByScoreShouldRemoveValuesCorrectlyWithExcludingMinRange() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRemRangeByScore(KEY_1_BBUFFER, new Range<>(2D, 3D, false, true)).block(),
is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRemRangeByScoreShouldRemoveValuesCorrectlyWithExcludingMaxRange() {
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_1, 3D, VALUE_3);
assertThat(connection.zSetCommands().zRemRangeByScore(KEY_1_BBUFFER, new Range<>(2D, 3D, true, false)).block(),
is(1L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zUnionStoreShouldWorkCorrectly() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_2, 1D, VALUE_1);
nativeCommands.zadd(KEY_2, 2D, VALUE_2);
nativeCommands.zadd(KEY_2, 3D, VALUE_3);
assertThat(
connection.zSetCommands()
.zUnionStore(KEY_3_BBUFFER, Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), Arrays.asList(2D, 3D)).block(),
is(3L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zInterStoreShouldWorkCorrectly() {
assumeThat(clientProvider instanceof LettuceRedisClientProvider, is(true));
nativeCommands.zadd(KEY_1, 1D, VALUE_1);
nativeCommands.zadd(KEY_1, 2D, VALUE_2);
nativeCommands.zadd(KEY_2, 1D, VALUE_1);
nativeCommands.zadd(KEY_2, 2D, VALUE_2);
nativeCommands.zadd(KEY_2, 3D, VALUE_3);
assertThat(
connection.zSetCommands()
.zInterStore(KEY_3_BBUFFER, Arrays.asList(KEY_1_BBUFFER, KEY_2_BBUFFER), Arrays.asList(2D, 3D)).block(),
is(2L));
}
/**
* @see DATAREDIS-525
*/
@Test
public void zRangeByLex() {
nativeCommands.zadd(KEY_1, 0D, "a");
nativeCommands.zadd(KEY_1, 0D, "b");
nativeCommands.zadd(KEY_1, 0D, "c");
nativeCommands.zadd(KEY_1, 0D, "d");
nativeCommands.zadd(KEY_1, 0D, "e");
nativeCommands.zadd(KEY_1, 0D, "f");
nativeCommands.zadd(KEY_1, 0D, "g");
assertThat(connection.zSetCommands().zRangeByLex(KEY_1_BBUFFER, new Range<>("", "c")).block(),
IsIterableContainingInOrder.contains(ByteBuffer.wrap("a".getBytes()), ByteBuffer.wrap("b".getBytes()),
ByteBuffer.wrap("c".getBytes())));
assertThat(connection.zSetCommands().zRangeByLex(KEY_1_BBUFFER, new Range<>("", "c", true, false)).block(),
IsIterableContainingInOrder.contains(ByteBuffer.wrap("a".getBytes()), ByteBuffer.wrap("b".getBytes())));
assertThat(connection.zSetCommands().zRangeByLex(KEY_1_BBUFFER, new Range<>("aaa", "g", true, false)).block(),
IsIterableContainingInOrder.contains(ByteBuffer.wrap("b".getBytes()), ByteBuffer.wrap("c".getBytes()),
ByteBuffer.wrap("d".getBytes()), ByteBuffer.wrap("e".getBytes()), ByteBuffer.wrap("f".getBytes())));
}
/**
* @see DATAREDIS-525
*/
@Test(expected = UnsupportedOperationException.class)
public void zRevRangeByLex() {
nativeCommands.zadd(KEY_1, 0D, "a");
nativeCommands.zadd(KEY_1, 0D, "b");
nativeCommands.zadd(KEY_1, 0D, "c");
nativeCommands.zadd(KEY_1, 0D, "d");
nativeCommands.zadd(KEY_1, 0D, "e");
nativeCommands.zadd(KEY_1, 0D, "f");
nativeCommands.zadd(KEY_1, 0D, "g");
assertThat(connection.zSetCommands().zRevRangeByLex(KEY_1_BBUFFER, new Range<>("c", "")).block(),
IsIterableContainingInOrder.contains(ByteBuffer.wrap("c".getBytes()), ByteBuffer.wrap("b".getBytes()),
ByteBuffer.wrap("a".getBytes())));
assertThat(connection.zSetCommands().zRevRangeByLex(KEY_1_BBUFFER, new Range<>("c", "", false, true)).block(),
IsIterableContainingInOrder.contains(ByteBuffer.wrap("a".getBytes()), ByteBuffer.wrap("b".getBytes())));
assertThat(connection.zSetCommands().zRevRangeByLex(KEY_1_BBUFFER, new Range<>("g", "aaa", false, true)).block(),
IsIterableContainingInOrder.contains(ByteBuffer.wrap("f".getBytes()), ByteBuffer.wrap("e".getBytes()),
ByteBuffer.wrap("d".getBytes()), ByteBuffer.wrap("c".getBytes()), ByteBuffer.wrap("b".getBytes())));
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.test.util;
import org.junit.rules.ExternalResource;
import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisURI;
/**
* @author Christoph Strobl
*/
public class LettuceRedisClientProvider extends ExternalResource {
String host = "127.0.0.1";
int port = 6379;
RedisClient client;
@Override
protected void before() {
try {
super.before();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
client = RedisClient.create(RedisURI.builder().withHost(host).withPort(port).build());
}
@Override
protected void after() {
super.after();
client.shutdown();
}
public RedisClient getClient() {
if(client == null) {
before();
}
return client;
}
public void destroy() {
if(client != null) {
after();
}
}
public static LettuceRedisClientProvider local() {
return new LettuceRedisClientProvider();
}
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2016. the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.test.util;
import com.lambdaworks.redis.cluster.RedisClusterClient;
import com.lambdaworks.redis.cluster.api.StatefulRedisClusterConnection;
import com.lambdaworks.redis.cluster.api.sync.RedisClusterCommands;
import org.junit.rules.ExternalResource;
import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisURI;
/**
* @author Christoph Strobl
*/
public class LettuceRedisClusterClientProvider extends ExternalResource {
String host = "127.0.0.1";
int port = 7379;
RedisClusterClient client;
@Override
protected void before() {
try {
super.before();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
client = RedisClusterClient.create(RedisURI.builder().withHost(host).withPort(port).build());
}
@Override
protected void after() {
super.after();
client.shutdown();
}
public RedisClusterClient getClient() {
if(client == null) {
before();
}
return client;
}
public void destroy() {
if(client != null) {
after();
}
}
public boolean test() {
try {
StatefulRedisClusterConnection<String, String> conn = getClient().connect();
conn.close();
} catch (Exception e) {
return false;
}
return true;
}
public static LettuceRedisClusterClientProvider local() {
return new LettuceRedisClusterClientProvider();
}
}

File diff suppressed because it is too large Load Diff