Skip to content

Commit

Permalink
Fix lambda no arg error
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek committed Dec 2, 2020
1 parent c2488e1 commit cabf8d9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ public void run() {
}
} else {
Object input = null;
if (running.get() && getInputReader() != null) {
input = getInputReader().readValue(requestConnection.getInputStream());
if (running.get()) {
ObjectReader inputReader = getInputReader();
if (inputReader != null)
input = inputReader.readValue(requestConnection.getInputStream());
Object output = processRequest(input, createContext(requestConnection));
postResponse(url, output);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.quarkus.funqy.test;

import io.quarkus.funqy.Funq;

public class NoArgFun {
@Funq
public String noArgFun() {
return "noArgFun";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.funqy.test;

import java.time.Duration;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import io.quarkus.amazon.lambda.test.LambdaClient;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@ExtendWith(UseNoArgFunExtension.class)
public class NoArgFunTest {

@Test
public void testNoArgFun() throws Exception {
LambdaClient.invoke(String.class, null, Duration.ofSeconds(5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkus.funqy.test;

import org.junit.jupiter.api.extension.Extension;

public class UseNoArgFunExtension implements Extension {
static {
System.setProperty("quarkus.funqy.export", "noArgFun");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.time.Duration;
import java.util.AbstractMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.arc.Arc;
Expand Down Expand Up @@ -92,44 +92,47 @@ public String setValue(String value) {
}

public static <T> T invoke(Class<T> returnType, Object input) {
return invoke(returnType, input, Duration.ofNanos(Long.MAX_VALUE));
}

public static <T> T invoke(Class<T> returnType, Object input, Duration timeout) {
try {
return invokeAsync(returnType, input).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
Throwable ex = e.getCause();
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
throw new RuntimeException(ex);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static <T> CompletableFuture<T> invokeAsync(Class<T> returnType, Object input) {
if (problem != null) {
throw new RuntimeException(problem);
CompletableFuture<T> failed = new CompletableFuture<>();
failed.completeExceptionally(problem);
return failed;
}
final ObjectMapper mapper = getObjectMapper();
final String id = "aws-request-" + REQUEST_ID_GENERATOR.incrementAndGet();
final String requestBody;
final CompletableFuture<String> result = new CompletableFuture<>();
REQUESTS.put(id, result);
try {
final ObjectMapper mapper = getObjectMapper();
String id = "aws-request-" + REQUEST_ID_GENERATOR.incrementAndGet();
CompletableFuture<String> result = new CompletableFuture<>();
REQUESTS.put(id, result);
String requestBody = mapper.writeValueAsString(input);
REQUEST_QUEUE.add(new Map.Entry<String, String>() {

@Override
public String getKey() {
return id;
}

@Override
public String getValue() {
return requestBody;
}

@Override
public String setValue(String value) {
return null;
}
});
String output = result.get();
return mapper.readerFor(returnType).readValue(output);
} catch (Exception e) {
if (e instanceof ExecutionException) {
Throwable ex = e.getCause();
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
throw new RuntimeException(ex);
}
requestBody = mapper.writeValueAsString(input);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
REQUEST_QUEUE.add(new AbstractMap.SimpleImmutableEntry(id, requestBody));
return result.thenApply(s -> {
try {
return mapper.readerFor(returnType).readValue(s);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}

private static ObjectMapper getObjectMapper() {
Expand Down

0 comments on commit cabf8d9

Please sign in to comment.