ringles commented on a change in pull request #7403: URL: https://github.com/apache/geode/pull/7403#discussion_r834673541
########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisList.java ########## @@ -356,10 +432,32 @@ protected synchronized void elementsPushHead(List<byte[]> elementsToAdd) { } } + public synchronized void elementsRemove(List<Integer> indexList) { + for (Integer element : indexList) { + elementList.remove(element.intValue()); + } + } + public synchronized void elementReplace(int index, byte[] newValue) { elementList.set(index, newValue); } + public synchronized void elementsRetainByIndexRange(int start, int end) { + if (start < 0) { + // Remove everything + elementList.clear(); + return; + } Review comment: Removed, tests still pass. ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/RedisList.java ########## @@ -356,10 +432,32 @@ protected synchronized void elementsPushHead(List<byte[]> elementsToAdd) { } } + public synchronized void elementsRemove(List<Integer> indexList) { + for (Integer element : indexList) { + elementList.remove(element.intValue()); + } + } + public synchronized void elementReplace(int index, byte[] newValue) { elementList.set(index, newValue); } + public synchronized void elementsRetainByIndexRange(int start, int end) { + if (start < 0) { + // Remove everything + elementList.clear(); + return; + } + + if (end < elementList.size()) { + elementList.subList(end + 1, elementList.size()).clear(); + } + + if (start > 0) { + elementList.subList(0, start).clear(); + } Review comment: When a delta comes in, we may remove things at the beginning of the list, or the end, or both. They are separate calls to subList().clear(), and I'm trying to avoid calling them if there's no need. If the delta values are invalid, versioning is _supposed_ to catch that... ########## File path: geode-for-redis/src/main/java/org/apache/geode/redis/internal/data/delta/RemoveElementsByIndex.java ########## @@ -46,6 +46,15 @@ public void add(int index) { indexes.add(index); } + public RemoveElementsByIndex(byte version, List<Integer> indexes) { + super(version); + this.indexes = indexes; + } + + public int size() { + return this.indexes.size(); + } + Review comment: Leftovers from an earlier implementation. Yanked. ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLTrimIntegrationTest.java ########## @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.apache.geode.redis.internal.commands.executor.list; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; + +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractLTrimIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + public static final String PREEXISTING_VALUE = "preexistingValue"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void givenWrongNumOfArgs_returnsError() { + assertExactNumberOfArgs(jedis, Protocol.Command.LTRIM, 3); + } + + @Test + public void withNonListKey_Fails() { + jedis.set("string", PREEXISTING_VALUE); + assertThatThrownBy(() -> jedis.ltrim("string", 0, -1)) + .hasMessage(ERROR_WRONG_TYPE); + } + + @Test + public void withNonExistentKey_returnsNull() { Review comment: OK ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLTrimIntegrationTest.java ########## @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.apache.geode.redis.internal.commands.executor.list; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; + +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractLTrimIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + public static final String PREEXISTING_VALUE = "preexistingValue"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void givenWrongNumOfArgs_returnsError() { + assertExactNumberOfArgs(jedis, Protocol.Command.LTRIM, 3); + } + + @Test + public void withNonListKey_Fails() { + jedis.set("string", PREEXISTING_VALUE); + assertThatThrownBy(() -> jedis.ltrim("string", 0, -1)) + .hasMessage(ERROR_WRONG_TYPE); + } + + @Test + public void withNonExistentKey_returnsNull() { + assertThat(jedis.ltrim("nonexistent", 0, -1)).isEqualTo("OK"); + } + + @Test + public void withNonIntegerRangeSpecifier_Fails() { + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "0", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "-1")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + } + + @Test + public void trimsToSpecifiedRange_givenValidRange() { Review comment: Parameterized, new cases added. ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLTrimIntegrationTest.java ########## @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.apache.geode.redis.internal.commands.executor.list; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; + +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractLTrimIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + public static final String PREEXISTING_VALUE = "preexistingValue"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void givenWrongNumOfArgs_returnsError() { + assertExactNumberOfArgs(jedis, Protocol.Command.LTRIM, 3); + } + + @Test + public void withNonListKey_Fails() { + jedis.set("string", PREEXISTING_VALUE); + assertThatThrownBy(() -> jedis.ltrim("string", 0, -1)) + .hasMessage(ERROR_WRONG_TYPE); + } + + @Test + public void withNonExistentKey_returnsNull() { + assertThat(jedis.ltrim("nonexistent", 0, -1)).isEqualTo("OK"); + } + + @Test + public void withNonIntegerRangeSpecifier_Fails() { + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "0", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "-1")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + } + + @Test + public void trimsToSpecifiedRange_givenValidRange() { + initializeTestList(); + + jedis.ltrim(KEY, 0, 0); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 2); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, 2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, -2, -1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, -1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e1"); + } + + @Test + public void trimsToCorrectRange_givenSpecifiersOutsideListSize() { + initializeTestList(); + + jedis.ltrim(KEY, -4, -1); Review comment: Updated. ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLTrimIntegrationTest.java ########## @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.apache.geode.redis.internal.commands.executor.list; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; + +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractLTrimIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + public static final String PREEXISTING_VALUE = "preexistingValue"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void givenWrongNumOfArgs_returnsError() { + assertExactNumberOfArgs(jedis, Protocol.Command.LTRIM, 3); + } + + @Test + public void withNonListKey_Fails() { + jedis.set("string", PREEXISTING_VALUE); + assertThatThrownBy(() -> jedis.ltrim("string", 0, -1)) + .hasMessage(ERROR_WRONG_TYPE); + } + + @Test + public void withNonExistentKey_returnsNull() { + assertThat(jedis.ltrim("nonexistent", 0, -1)).isEqualTo("OK"); + } + + @Test + public void withNonIntegerRangeSpecifier_Fails() { + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "0", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "-1")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + } + + @Test + public void trimsToSpecifiedRange_givenValidRange() { + initializeTestList(); + + jedis.ltrim(KEY, 0, 0); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 2); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, 2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, -2, -1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, -1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e1"); + } + + @Test + public void trimsToCorrectRange_givenSpecifiersOutsideListSize() { + initializeTestList(); + + jedis.ltrim(KEY, -4, -1); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + jedis.ltrim(KEY, -10, 10); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + jedis.ltrim(KEY, 0, 4); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + jedis.ltrim(KEY, 0, 10); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + } + + private void initializeTestList() { + jedis.del(KEY); + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + } + + @Test + public void removesKey_whenLastElementRemoved() { + final String keyWithTagForKeysCommand = "{tag}" + KEY; + jedis.lpush(keyWithTagForKeysCommand, "e1", "e2", "e3"); Review comment: At one point early in dev, it helped. Good catch. ########## File path: geode-for-redis/src/integrationTest/java/org/apache/geode/redis/internal/commands/executor/list/AbstractLTrimIntegrationTest.java ########## @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.apache.geode.redis.internal.commands.executor.list; + +import static org.apache.geode.redis.RedisCommandArgumentsTestHelper.assertExactNumberOfArgs; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_NOT_INTEGER; +import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.BIND_ADDRESS; +import static org.apache.geode.test.dunit.rules.RedisClusterStartupRule.REDIS_CLIENT_TIMEOUT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.Protocol; + +import org.apache.geode.redis.ConcurrentLoopingThreads; +import org.apache.geode.redis.RedisIntegrationTest; + +public abstract class AbstractLTrimIntegrationTest implements RedisIntegrationTest { + public static final String KEY = "key"; + public static final String PREEXISTING_VALUE = "preexistingValue"; + private JedisCluster jedis; + + @Before + public void setUp() { + jedis = new JedisCluster(new HostAndPort(BIND_ADDRESS, getPort()), REDIS_CLIENT_TIMEOUT); + } + + @After + public void tearDown() { + flushAll(); + jedis.close(); + } + + @Test + public void givenWrongNumOfArgs_returnsError() { + assertExactNumberOfArgs(jedis, Protocol.Command.LTRIM, 3); + } + + @Test + public void withNonListKey_Fails() { + jedis.set("string", PREEXISTING_VALUE); + assertThatThrownBy(() -> jedis.ltrim("string", 0, -1)) + .hasMessage(ERROR_WRONG_TYPE); + } + + @Test + public void withNonExistentKey_returnsNull() { + assertThat(jedis.ltrim("nonexistent", 0, -1)).isEqualTo("OK"); + } + + @Test + public void withNonIntegerRangeSpecifier_Fails() { + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "0", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "-1")) + .hasMessage(ERROR_NOT_INTEGER); + assertThatThrownBy(() -> jedis.sendCommand(KEY, Protocol.Command.LTRIM, KEY, + "not-an-integer", "not-an-integer")) + .hasMessage(ERROR_NOT_INTEGER); + } + + @Test + public void trimsToSpecifiedRange_givenValidRange() { + initializeTestList(); + + jedis.ltrim(KEY, 0, 0); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + + initializeTestList(); + + jedis.ltrim(KEY, 0, 2); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, 2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(3); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, 1, -2); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e2"); + + initializeTestList(); + + jedis.ltrim(KEY, -2, -1); + assertThat(jedis.llen(KEY)).isEqualTo(2); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e1"); + + initializeTestList(); + + jedis.ltrim(KEY, -1, -1); + assertThat(jedis.llen(KEY)).isEqualTo(1); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e1"); + } + + @Test + public void trimsToCorrectRange_givenSpecifiersOutsideListSize() { + initializeTestList(); + + jedis.ltrim(KEY, -4, -1); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + jedis.ltrim(KEY, -10, 10); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + jedis.ltrim(KEY, 0, 4); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + + jedis.ltrim(KEY, 0, 10); + assertThat(jedis.llen(KEY)).isEqualTo(4); + assertThat(jedis.lindex(KEY, 0)).isEqualTo("e4"); + assertThat(jedis.lindex(KEY, 1)).isEqualTo("e3"); + assertThat(jedis.lindex(KEY, 2)).isEqualTo("e2"); + assertThat(jedis.lindex(KEY, 3)).isEqualTo("e1"); + } + + private void initializeTestList() { + jedis.del(KEY); + jedis.lpush(KEY, "e1", "e2", "e3", "e4"); + } + + @Test + public void removesKey_whenLastElementRemoved() { + final String keyWithTagForKeysCommand = "{tag}" + KEY; + jedis.lpush(keyWithTagForKeysCommand, "e1", "e2", "e3"); + + jedis.ltrim(keyWithTagForKeysCommand, 0, -4); + assertThat(jedis.llen(keyWithTagForKeysCommand)).isEqualTo(0L); + assertThat(jedis.exists(keyWithTagForKeysCommand)).isFalse(); Review comment: Yanked. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@geode.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org