Modified version determination for user-agent, added test

This commit is contained in:
Oleg Zhurakousky
2021-11-12 12:58:06 +01:00
parent 66369af641
commit dc0c52f488
2 changed files with 31 additions and 17 deletions

View File

@@ -64,9 +64,9 @@ public final class CustomRuntimeEventLoop implements SmartLifecycle {
private static final String LAMBDA_RUNTIME_URL_TEMPLATE = "http://{0}/{1}/runtime/invocation/next"; private static final String LAMBDA_RUNTIME_URL_TEMPLATE = "http://{0}/{1}/runtime/invocation/next";
private static final String LAMBDA_INVOCATION_URL_TEMPLATE = "http://{0}/{1}/runtime/invocation/{2}/response"; private static final String LAMBDA_INVOCATION_URL_TEMPLATE = "http://{0}/{1}/runtime/invocation/{2}/response";
private static final String USER_AGENT_VALUE = String.format( private static final String USER_AGENT_VALUE = String.format(
"spring-cloud-function/%s-%s", "spring-cloud-function:%s/JAVA-%s",
System.getProperty("java.vendor.version"), extractVersion(),
CustomRuntimeEventLoop.class.getPackage().getImplementationVersion()); System.getProperty("java.vm.name"));
private final ConfigurableApplicationContext applicationContext; private final ConfigurableApplicationContext applicationContext;
@@ -85,6 +85,22 @@ public final class CustomRuntimeEventLoop implements SmartLifecycle {
}); });
} }
@Override
public void start() {
this.run();
}
@Override
public void stop() {
this.executor.shutdownNow();
this.running = false;
}
@Override
public boolean isRunning() {
return this.running;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void eventLoop(ConfigurableApplicationContext context) { private void eventLoop(ConfigurableApplicationContext context) {
Environment environment = context.getEnvironment(); Environment environment = context.getEnvironment();
@@ -205,19 +221,13 @@ public final class CustomRuntimeEventLoop implements SmartLifecycle {
return value instanceof Collection ? (Collection<?>) value : Arrays.asList(value); return value instanceof Collection ? (Collection<?>) value : Arrays.asList(value);
} }
@Override private static String extractVersion() {
public void start() { String path = CustomRuntimeEventLoop.class.getProtectionDomain().getCodeSource().getLocation().toString();
this.run(); int endIndex = path.lastIndexOf('.');
} if (endIndex < 0) {
return "UNKNOWN-VERSION";
@Override }
public void stop() { int startIndex = path.lastIndexOf("/") + 1;
this.executor.shutdownNow(); return path.substring(startIndex, endIndex).replace("spring-cloud-function-adapter-aws-", "");
this.running = false;
}
@Override
public boolean isRunning() {
return this.running;
} }
} }

View File

@@ -27,6 +27,7 @@ import org.springframework.cloud.function.adapter.test.aws.AWSCustomRuntime;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@@ -46,6 +47,9 @@ public class CustomRuntimeEventLoopTest {
.run()) { .run()) {
AWSCustomRuntime aws = userContext.getBean(AWSCustomRuntime.class); AWSCustomRuntime aws = userContext.getBean(AWSCustomRuntime.class);
Message<String> replyMessage = aws.exchange("\"ricky\"");
assertThat(replyMessage.getHeaders()).containsKey("user-agent");
assertThat(((String) replyMessage.getHeaders().get("user-agent"))).startsWith("spring-cloud-function:");
assertThat(aws.exchange("\"ricky\"").getPayload()).isEqualTo("\"RICKY\""); assertThat(aws.exchange("\"ricky\"").getPayload()).isEqualTo("\"RICKY\"");
assertThat(aws.exchange("\"julien\"").getPayload()).isEqualTo("\"JULIEN\""); assertThat(aws.exchange("\"julien\"").getPayload()).isEqualTo("\"JULIEN\"");
assertThat(aws.exchange("\"bubbles\"").getPayload()).isEqualTo("\"BUBBLES\""); assertThat(aws.exchange("\"bubbles\"").getPayload()).isEqualTo("\"BUBBLES\"");