/** * invokes the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. */ public Object invoke(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> invoking %s", commandName); try { return transformer.apply(http.invoke(command)); } catch (Throwable t) { try { return fallback.createOrPropagate(t); } catch (Exception e) { throw propagate(e); } } }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
HttpCommand command = toCommand(commandName, invocation); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command);
public void testMethodWithNoTimeoutCallGetDirectly() throws Exception { expect(config.getTimeoutNanos(get)).andReturn(Optional.<Long> absent()); expect(http.invoke(new HttpCommand(getRequest))).andReturn(response); replay(http, timeLimiter, fallback, config, future); invokeHttpMethod.apply(get); }
@SuppressWarnings("unchecked") @BeforeMethod void createMocks() { http = createMock(HttpCommandExecutorService.class); timeLimiter = createMock(TimeLimiter.class); fallback = createMock(org.jclouds.Fallback.class); config = createMock(InvocationConfig.class); invokeHttpMethod = new InvokeHttpMethod(toRequest, http, transformerForRequest, timeLimiter, config); expect(config.getCommandName(get)).andReturn("ns:get"); expect(config.getFallback(get)).andReturn(fallback); }
HttpCommand command = toCommand(commandName, invocation); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command);
public void testMethodWithTimeoutRunsTimeLimiter() throws Exception { expect(config.getTimeoutNanos(get)).andReturn(Optional.of(250000000l)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andReturn(response); replay(http, timeLimiter, fallback, config, future); invokeHttpMethod.apply(get); }
@SuppressWarnings("unchecked") @BeforeMethod void createMocks() { http = createMock(HttpCommandExecutorService.class); timeLimiter = createMock(TimeLimiter.class); fallback = createMock(org.jclouds.Fallback.class); config = createMock(InvocationConfig.class); future = createMock(ListenableFuture.class); invokeHttpMethod = new InvokeHttpMethod(toRequest, http, transformerForRequest, timeLimiter, config); expect(config.getCommandName(get)).andReturn("ns:get"); expect(config.getFallback(get)).andReturn(fallback); }
/** * invokes the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. */ public Object invoke(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> invoking %s", commandName); try { return transformer.apply(http.invoke(command)); } catch (Throwable t) { try { return fallback.createOrPropagate(t); } catch (Exception e) { throw propagate(e); } } }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
HttpCommand command = toCommand(commandName, invocation); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command);
public void testTimeLimitedRunsFallbackCreateOrPropagate() throws Exception { IllegalStateException exception = new IllegalStateException(); expect(config.getTimeoutNanos(get)).andReturn(Optional.of(250000000l)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andThrow(exception); expect(fallback.createOrPropagate(exception)).andReturn(fallbackResponse); replay(http, timeLimiter, fallback, config, future); assertEquals(invokeHttpMethod.apply(get), fallbackResponse); } }
/** * invokes the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. */ public Object invoke(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> invoking %s", commandName); try { return transformer.apply(http.invoke(command)); } catch (Throwable t) { try { return fallback.createOrPropagate(t); } catch (Exception e) { throw propagate(e); } } }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
HttpCommand command = toCommand(commandName, invocation); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command);
public void testMethodWithTimeoutRunsTimeLimiter() throws Exception { expect(config.getTimeoutNanos(get)).andReturn(Optional.of(250000000L)); InvokeAndTransform invoke = invokeHttpMethod.new InvokeAndTransform("ns:get", getCommand); expect(timeLimiter.callWithTimeout(invoke, 250000000, TimeUnit.NANOSECONDS, true)).andReturn(response); replay(http, timeLimiter, fallback, config); invokeHttpMethod.apply(get); }
/** * invokes the {@linkplain HttpCommand} associated with {@code invocation}, * {@link #getTransformer(String, HttpCommand) parses its response}, and * applies a {@link #getFallback(String, Invocation, HttpCommand) fallback} * if a {@code Throwable} is encountered. */ public Object invoke(Invocation invocation) { String commandName = config.getCommandName(invocation); HttpCommand command = toCommand(commandName, invocation); Function<HttpResponse, ?> transformer = getTransformer(commandName, command); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command); logger.debug(">> invoking %s", commandName); try { return transformer.apply(http.invoke(command)); } catch (Throwable t) { try { return fallback.createOrPropagate(t); } catch (Exception e) { throw propagate(e); } } }
@Override public Object apply(Invocation in) { Optional<Long> timeoutNanos = config.getTimeoutNanos(in); if (timeoutNanos.isPresent()) { return invokeWithTimeout(in, timeoutNanos.get()); } return invoke(in); }
HttpCommand command = toCommand(commandName, invocation); org.jclouds.Fallback<?> fallback = getFallback(commandName, invocation, command);
public void testMethodWithNoTimeoutCallGetDirectly() throws Exception { expect(config.getTimeoutNanos(get)).andReturn(Optional.<Long> absent()); expect(http.invoke(new HttpCommand(getRequest))).andReturn(response); replay(http, timeLimiter, fallback, config); invokeHttpMethod.apply(get); }