Skip to content

Commit

Permalink
Merge pull request #192 from swisspost/feature/issue191_call_handler_…
Browse files Browse the repository at this point in the history
…on_exception

#191 Call handler on exception
  • Loading branch information
mcweba authored Jun 17, 2024
2 parents 54c3bb4 + d20d539 commit 70732bb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 12 deletions.
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

0 comments on commit 70732bb

Please sign in to comment.