Skip to content

Commit

Permalink
GEODE-7914: create missing unit test for Redis Module Expire Command (a…
Browse files Browse the repository at this point in the history
…pache#4852)


Co-authored-by: John Hutchison <[email protected]>
Co-authored-by: Jens Deppe <[email protected]>
  • Loading branch information
3 people authored Mar 30, 2020
1 parent 4984ed9 commit fb2c273
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public void PERSISTCommand_ShouldClearExpirationTimeForGivenKey() {

@Test
@Ignore("this test needs to pass to have feature parity with native redis")
public void SettingExiprationToNegativeValue_ShouldDeleteKey() throws InterruptedException {
public void SettingExiprationToNegativeValue_ShouldDeleteKey() {

String key = "key";
String value = "value";
Expand All @@ -464,8 +464,8 @@ public void SettingExiprationToNegativeValue_ShouldDeleteKey() throws Interrupte
Long expirationWasSet = jedis.expire(key, -5);
assertThat(expirationWasSet).isEqualTo(1);

String actualValue = jedis.get(key);
assertThat(actualValue).isNull();
Boolean keyExists = jedis.exists(key);
assertThat(keyExists).isTrue();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ public class ExpireExecutor extends AbstractExecutor implements Extendable {
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();

if (commandElems.size() < 3) {
if (commandElems.size() != 3) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), getArgsError()));
return;
}

ByteArrayWrapper key = command.getKey();
RegionProvider regionProvider = context.getRegionProvider();
byte[] delayByteArray = commandElems.get(SECONDS_INDEX);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.executor.general;

import static java.nio.charset.Charset.defaultCharset;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import org.apache.geode.redis.internal.Command;
import org.apache.geode.redis.internal.ExecutionHandlerContext;
import org.apache.geode.redis.internal.Executor;
import org.apache.geode.redis.internal.executor.ExpireExecutor;

public class ExpireExecutorJUnitTest {

private ExecutionHandlerContext context;
private Command command;
private UnpooledByteBufAllocator byteBuf;

@Before
public void setUp() {
context = mock(ExecutionHandlerContext.class);
command = mock(Command.class);
byteBuf = new UnpooledByteBufAllocator(false);
}

@Test
public void calledWithTooFewCommandArguments_returnsError() {
Executor executor = new ExpireExecutor();
List<byte[]> commandsAsBytesWithTooFewArguments = new ArrayList<>();
commandsAsBytesWithTooFewArguments.add("EXPIRE".getBytes());
commandsAsBytesWithTooFewArguments.add("key".getBytes());

ArgumentCaptor<ByteBuf> argsErrorCaptor = ArgumentCaptor.forClass(ByteBuf.class);

when(context.getByteBufAllocator()).thenReturn(byteBuf);
when(command.getProcessedCommand()).thenReturn(commandsAsBytesWithTooFewArguments);

executor.executeCommand(command, context);
verify(command, times(1)).setResponse(argsErrorCaptor.capture());

List<ByteBuf> capturedErrors = argsErrorCaptor.getAllValues();
assertThat(capturedErrors.get(0).toString(defaultCharset()))
.startsWith("-ERR The wrong number of arguments");
}

@Test
public void calledWithTooManyCommandArguments_returnsErrorMessage() {
Executor executor = new ExpireExecutor();
List<byte[]> commandsAsBytesWithTooManyArguments = new ArrayList<>();
commandsAsBytesWithTooManyArguments.add("EXPIRE".getBytes());
commandsAsBytesWithTooManyArguments.add("key".getBytes());
commandsAsBytesWithTooManyArguments.add("100".getBytes());
commandsAsBytesWithTooManyArguments.add("Bonus!".getBytes());

ArgumentCaptor<ByteBuf> argsErrorCaptor = ArgumentCaptor.forClass(ByteBuf.class);

when(context.getByteBufAllocator()).thenReturn(byteBuf);
when(command.getProcessedCommand()).thenReturn(commandsAsBytesWithTooManyArguments);

executor.executeCommand(command, context);
verify(command, times(1)).setResponse(argsErrorCaptor.capture());

List<ByteBuf> capturedErrors = argsErrorCaptor.getAllValues();
assertThat(capturedErrors.get(0).toString(defaultCharset()))
.startsWith("-ERR The wrong number of arguments");
}

@Test
public void calledWithInvalidCommandArguments_returnsErrorMessage() {
Executor executor = new ExpireExecutor();
List<byte[]> commandsAsBytesWithTooManyArguments = new ArrayList<>();
commandsAsBytesWithTooManyArguments.add("EXPIRE".getBytes());
commandsAsBytesWithTooManyArguments.add("key".getBytes());
commandsAsBytesWithTooManyArguments.add("not a number".getBytes());

ArgumentCaptor<ByteBuf> argsErrorCaptor = ArgumentCaptor.forClass(ByteBuf.class);

when(context.getByteBufAllocator()).thenReturn(byteBuf);
when(command.getProcessedCommand()).thenReturn(commandsAsBytesWithTooManyArguments);

executor.executeCommand(command, context);
verify(command, times(1)).setResponse(argsErrorCaptor.capture());

List<ByteBuf> capturedErrors = argsErrorCaptor.getAllValues();
assertThat(capturedErrors.get(0).toString(defaultCharset()))
.startsWith("-ERR The number of seconds specified must be numeric");
}
}

0 comments on commit fb2c273

Please sign in to comment.