Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR for release #193

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.swisspush</groupId>
<artifactId>rest-storage</artifactId>
<version>3.1.6-SNAPSHOT</version>
<version>3.1.7-SNAPSHOT</version>
<name>rest-storage</name>
<description>
Persistence for REST resources in the filesystem or a redis database
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/org/swisspush/reststorage/redis/RedisStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class RedisStorage implements Storage {
private final String ID;
private final String hostAndPort;

private static final String redisProviderFailMsg = "redisProvider.redis() failed";

public RedisStorage(
Vertx vertx,
ModuleConfiguration config,
Expand Down Expand Up @@ -613,7 +615,10 @@ public void exec(final int executionCounter) {
List<String> args = toPayload(luaScripts.get(LuaScript.GET).getSha(), keys.size(), keys, arguments);
redisProvider.redis().onComplete( ev -> {
if (ev.failed()) {
throw exceptionFactory.newRuntimeException("redisProvider.redis() failed", ev.cause());
log.error("GET request failed with message",
exceptionFactory.newException(redisProviderFailMsg, ev.cause()));
error(handler, redisProviderFailMsg);
return;
}
var redisAPI = ev.result();
redisAPI.evalsha(args, evalShaEv -> {
Expand Down Expand Up @@ -689,7 +694,10 @@ public void exec(final int executionCounter) {

redisProvider.redis().onComplete( redisEv -> {
if (redisEv.failed()) {
throw exceptionFactory.newRuntimeException("redisProvider.redis() failed", redisEv.cause());
log.error("StorageExpand request failed with message",
exceptionFactory.newException(redisProviderFailMsg, redisEv.cause()));
error(handler, redisProviderFailMsg);
return;
}
var redisAPI = redisEv.result();
redisAPI.evalsha(args, evalShaEv -> {
Expand Down Expand Up @@ -1021,7 +1029,10 @@ public void exec(final int executionCounter) {

redisProvider.redis().onComplete(redisEv -> {
if (redisEv.failed()) {
throw exceptionFactory.newRuntimeException("redisProvider.redis() failed", redisEv.cause());
log.error("PUT request failed with message",
exceptionFactory.newException(redisProviderFailMsg, redisEv.cause()));
error(handler, redisProviderFailMsg);
return;
}
var redisAPI = redisEv.result();
redisAPI.evalsha(args, evalShaEv -> {
Expand Down Expand Up @@ -1114,8 +1125,9 @@ public void exec(final int executionCounter) {

redisProvider.redis().onComplete( ev -> {
if (ev.failed()) {
log.error("redisProvider.redis()", exceptionFactory.newException(
"redisProvider.redis() failed", ev.cause()));
log.error("DELETE request failed with message",
exceptionFactory.newException(redisProviderFailMsg, ev.cause()));
error(handler, redisProviderFailMsg);
return;
}
RedisAPI redisAPI = ev.result();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.swisspush.reststorage.redis;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.*;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.buffer.impl.BufferImpl;
import io.vertx.ext.unit.Async;
Expand All @@ -18,15 +15,18 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.swisspush.reststorage.exception.RestStorageExceptionFactory;
import org.swisspush.reststorage.util.LockMode;
import org.swisspush.reststorage.util.ModuleConfiguration;

import java.util.Collections;
import java.util.List;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
import static org.swisspush.reststorage.exception.RestStorageExceptionFactory.newRestStorageWastefulExceptionFactory;

/**
Expand All @@ -40,17 +40,74 @@ public class RedisStorageTest {
private RedisAPI redisAPI;
private RedisProvider redisProvider;
private RedisStorage storage;
private RestStorageExceptionFactory exceptionFactory;

@Before
public void setUp(TestContext context) {
redisAPI = Mockito.mock(RedisAPI.class);
redisProvider = Mockito.mock(RedisProvider.class);
when(redisProvider.redis()).thenReturn(Future.succeededFuture(redisAPI));
var exceptionFactory = newRestStorageWastefulExceptionFactory();
exceptionFactory = Mockito.spy(newRestStorageWastefulExceptionFactory());

storage = new RedisStorage(mock(Vertx.class), new ModuleConfiguration(), redisProvider, exceptionFactory);
}

@Test
public void testStorageGetWithRedisErrorCallsHandler(TestContext testContext) {
Async async = testContext.async();

when(redisProvider.redis()).thenReturn(Future.failedFuture("Booooom"));

storage.get("/some/path/resource", "", 0, 100, event -> {
String msg = "redisProvider.redis() failed";
testContext.assertTrue(event.error);
testContext.assertEquals(msg, event.errorMessage);

ArgumentCaptor<Throwable> throwableArgument = ArgumentCaptor.forClass(Throwable.class);

verify(exceptionFactory, times(1)).newException(eq(msg), throwableArgument.capture());
testContext.assertTrue(throwableArgument.getValue().getMessage().contains("Booooom"));
async.complete();
});
}

@Test
public void testStorageDeleteWithRedisErrorCallsHandler(TestContext testContext) {
Async async = testContext.async();

when(redisProvider.redis()).thenReturn(Future.failedFuture("Booooom"));

storage.delete("/some/path/resource", "", LockMode.SILENT, 300L, true, true, event -> {
String msg = "redisProvider.redis() failed";
testContext.assertTrue(event.error);
testContext.assertEquals(msg, event.errorMessage);

ArgumentCaptor<Throwable> throwableArgument = ArgumentCaptor.forClass(Throwable.class);

verify(exceptionFactory, times(1)).newException(eq(msg), throwableArgument.capture());
testContext.assertTrue(throwableArgument.getValue().getMessage().contains("Booooom"));
async.complete();
});
}

@Test
public void testStorageExpandWithRedisErrorCallsHandler(TestContext testContext) {
Async async = testContext.async();

when(redisProvider.redis()).thenReturn(Future.failedFuture("Booooom"));

storage.storageExpand("/some/path/resource", "", List.of("res1", "res2", "res3"), event -> {
String msg = "redisProvider.redis() failed";
testContext.assertTrue(event.error);
testContext.assertEquals(msg, event.errorMessage);

ArgumentCaptor<Throwable> throwableArgument = ArgumentCaptor.forClass(Throwable.class);

verify(exceptionFactory, times(1)).newException(eq(msg), throwableArgument.capture());
testContext.assertTrue(throwableArgument.getValue().getMessage().contains("Booooom"));
async.complete();
});
}

@Test
public void testCalculateCurrentMemoryUsageRedisClientFail(TestContext testContext) {
Expand Down
Loading