mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eventbridge): support native Redshift Data API targets (#6900)
This commit is contained in:
1 parent
add127bf7c
commit
6d32605370
11 files changed
+664
-7
No files matched your search
+6
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.batch.BatchService;
|
||||
import io.github.hectorvent.floci.services.ecs.EcsJsonHandler;
|
||||
import io.github.hectorvent.floci.services.ecs.EcsService;
|
||||
@@ -21,6 +22,7 @@ import io.github.hectorvent.floci.services.firehose.FirehoseService;
|
||||
import io.github.hectorvent.floci.services.firehose.model.Record;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
|
||||
@@ -67,6 +69,8 @@ class EventBridgeInvokerTest {
|
||||
EmulatorConfig emulatorConfig =
|
||||
mock(EmulatorConfig.class,
|
||||
Mockito.RETURNS_DEEP_STUBS);
|
||||
RedshiftDataService redshiftDataService = mock(RedshiftDataService.class);
|
||||
ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
|
||||
invoker = new EventBridgeInvoker(
|
||||
lambdaService,
|
||||
sqsService,
|
||||
@@ -78,6 +82,8 @@ class EventBridgeInvokerTest {
|
||||
new EcsJsonHandler(ecsService, new ObjectMapper(),
|
||||
new HostVolumePolicy(emulatorConfig)),
|
||||
stepFunctionsService,
|
||||
redshiftDataService,
|
||||
serviceRegistry,
|
||||
regionResolver,
|
||||
new ObjectMapper(),
|
||||
emulatorConfig
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RedshiftDataParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Rule;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class EventBridgeRedshiftDataAccountIntegrationTest {
|
||||
|
||||
@Inject
|
||||
RuleScheduler scheduler;
|
||||
|
||||
@Inject
|
||||
RegionResolver regionResolver;
|
||||
|
||||
@InjectMock
|
||||
RedshiftDataService redshiftDataService;
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void scheduledSqlUsesRuleAccountWithoutAnHttpRequest(boolean batch) {
|
||||
String previousAccount = regionResolver.getAccountId();
|
||||
AtomicReference<String> executionAccount = new AtomicReference<>();
|
||||
if (batch) {
|
||||
when(redshiftDataService.batchExecuteStatement(any(), any(), any())).thenAnswer(invocation -> {
|
||||
executionAccount.set(regionResolver.getAccountId());
|
||||
return JsonNodeFactory.instance.objectNode();
|
||||
});
|
||||
} else {
|
||||
when(redshiftDataService.executeStatement(any(), any(), any())).thenAnswer(invocation -> {
|
||||
executionAccount.set(regionResolver.getAccountId());
|
||||
return JsonNodeFactory.instance.objectNode();
|
||||
});
|
||||
}
|
||||
|
||||
Rule rule = new Rule();
|
||||
rule.setName("scheduled-redshift-account");
|
||||
rule.setArn("arn:aws:events:us-east-1:111122223333:rule/scheduled-redshift-account");
|
||||
rule.setAccountId("111122223333");
|
||||
rule.setState(RuleState.ENABLED);
|
||||
Target target = new Target("sql", "arn:aws:redshift:us-east-1:999999999999:cluster:warehouse", null, null);
|
||||
target.setRoleArn("arn:aws:iam::111122223333:role/EventBridgeRedshiftRole");
|
||||
RedshiftDataParameters parameters = new RedshiftDataParameters();
|
||||
parameters.setDatabase("dev");
|
||||
if (batch) {
|
||||
parameters.setSqls(List.of("select 1"));
|
||||
} else {
|
||||
parameters.setSql("select 1");
|
||||
}
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
try {
|
||||
scheduler.startScheduler(rule.getArn(), "rate(1 minute)",
|
||||
() -> new RuleScheduler.ScheduleData(rule, List.of(target)));
|
||||
assertEquals("111122223333", executionAccount.get());
|
||||
assertEquals(previousAccount, regionResolver.getAccountId());
|
||||
} finally {
|
||||
scheduler.stopScheduler(rule.getArn());
|
||||
}
|
||||
}
|
||||
}
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RedshiftDataParameters;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.redshiftdata.RedshiftDataService;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockMakers;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
class EventBridgeRedshiftDataTargetTest {
|
||||
|
||||
private static final String CLUSTER_ARN = "arn:aws:redshift:us-east-1:123456789012:cluster:warehouse";
|
||||
private static final String WORKGROUP_ARN =
|
||||
"arn:aws:redshift-serverless:us-west-2:123456789012:workgroup/warehouse";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::123456789012:role/EventBridgeRedshiftRole";
|
||||
private static final String EVENT = "{\"id\":\"event-1\"}";
|
||||
|
||||
private RedshiftDataService redshiftDataService;
|
||||
private ServiceRegistry serviceRegistry;
|
||||
private EventBridgeInvoker invoker;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
redshiftDataService = mock(RedshiftDataService.class);
|
||||
serviceRegistry = mock(ServiceRegistry.class);
|
||||
when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(true);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.baseUrl()).thenReturn("http://localhost:4566");
|
||||
invoker = new EventBridgeInvoker(mock(LambdaService.class), mock(SqsService.class), mock(SnsService.class),
|
||||
redshiftDataService, serviceRegistry, new ObjectMapper(), config);
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledDataApiRejectsSingleAndBatchDispatch() {
|
||||
when(serviceRegistry.isServiceEnabled("redshift-data")).thenReturn(false);
|
||||
for (boolean batch : List.of(false, true)) {
|
||||
Target target = target(CLUSTER_ARN);
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
if (batch) {
|
||||
parameters.setSqls(List.of("select 1"));
|
||||
} else {
|
||||
parameters.setSql("select 1");
|
||||
}
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> invoker.invokeTarget(target, EVENT, "us-east-1"));
|
||||
assertEquals("ServiceNotAvailableException", failure.getErrorCode());
|
||||
}
|
||||
verifyNoInteractions(redshiftDataService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesParametersFromOriginalEventDespiteInputOverride() {
|
||||
Target target = target(CLUSTER_ARN);
|
||||
target.setInput("{\"detail\":{\"database\":\"wrong\"}}");
|
||||
RedshiftDataParameters parameters = parameters("$.detail.database");
|
||||
parameters.setDbUser("$.detail.user");
|
||||
parameters.setSecretManagerArn("$.detail.secret");
|
||||
parameters.setStatementName("$.detail.name");
|
||||
parameters.setSql("$.detail.queries[0]");
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
String event = """
|
||||
{"detail":{"database":"dev","user":"admin","secret":"secret-arn",
|
||||
"name":"event-statement","queries":["select 42"]}}
|
||||
""";
|
||||
|
||||
invoker.invokeTarget(target, event, "us-east-1");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(redshiftDataService).executeStatement(request.capture(), eq("us-east-1"), eq(ROLE_ARN));
|
||||
assertEquals("dev", request.getValue().path("Database").asText());
|
||||
assertEquals("admin", request.getValue().path("DbUser").asText());
|
||||
assertEquals("secret-arn", request.getValue().path("SecretArn").asText());
|
||||
assertEquals("event-statement", request.getValue().path("StatementName").asText());
|
||||
assertEquals("select 42", request.getValue().path("Sql").asText());
|
||||
assertEquals("$.detail.database", parameters.getDatabase());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void resolvesSqlPathContainingSlash(boolean batch) {
|
||||
Target target = target(CLUSTER_ARN);
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
if (batch) {
|
||||
parameters.setSqls(List.of("$.detail.sql/query"));
|
||||
} else {
|
||||
parameters.setSql("$.detail.sql/query");
|
||||
}
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
invoker.invokeTarget(target, "{\"detail\":{\"sql/query\":\"select 1\"}}", "us-east-1");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
if (batch) {
|
||||
verify(redshiftDataService).batchExecuteStatement(request.capture(), eq("us-east-1"), eq(ROLE_ARN));
|
||||
assertEquals("select 1", request.getValue().path("Sqls").get(0).asText());
|
||||
} else {
|
||||
verify(redshiftDataService).executeStatement(request.capture(), eq("us-east-1"), eq(ROLE_ARN));
|
||||
assertEquals("select 1", request.getValue().path("Sql").asText());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesBatchSqlPathsAndPreservesStaticSql() {
|
||||
Target target = target(WORKGROUP_ARN);
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSqls(List.of("$.detail.queries[0]", "$.detail.queries[1]", "select '$.detail.literal'"));
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
invoker.invokeTarget(target, "{\"detail\":{\"queries\":[\"select 42\",\"select 43\"]}}", "us-west-2");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(redshiftDataService).batchExecuteStatement(request.capture(), eq("us-west-2"), eq(ROLE_ARN));
|
||||
assertEquals("select 42", request.getValue().path("Sqls").get(0).asText());
|
||||
assertEquals("select 43", request.getValue().path("Sqls").get(1).asText());
|
||||
assertEquals("select '$.detail.literal'", request.getValue().path("Sqls").get(2).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokesExecuteStatementForClusterTarget() throws Exception {
|
||||
Target target = target(CLUSTER_ARN);
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setDbUser("event_user");
|
||||
parameters.setSql("select 1");
|
||||
parameters.setWithEvent(true);
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
invoker.invokeTarget(target, EVENT, "eu-west-1");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(redshiftDataService).executeStatement(request.capture(), eq("us-east-1"), eq(ROLE_ARN));
|
||||
assertEquals("warehouse", request.getValue().path("ClusterIdentifier").asText());
|
||||
assertEquals("dev", request.getValue().path("Database").asText());
|
||||
assertEquals("event_user", request.getValue().path("DbUser").asText());
|
||||
assertEquals("select 1", request.getValue().path("Sql").asText());
|
||||
assertEquals("event-rule", request.getValue().path("StatementName").asText());
|
||||
assertEquals(true, request.getValue().path("WithEvent").asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokesBatchExecuteStatementForServerlessWorkgroupTarget() throws Exception {
|
||||
Target target = target(WORKGROUP_ARN);
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSecretManagerArn("arn:aws:secretsmanager:us-west-2:123456789012:secret:redshift");
|
||||
parameters.setSqls(List.of("select 1", "select 2"));
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
invoker.invokeTarget(target, EVENT, "us-east-1");
|
||||
|
||||
ArgumentCaptor<JsonNode> request = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(redshiftDataService).batchExecuteStatement(request.capture(), eq("us-west-2"), eq(ROLE_ARN));
|
||||
assertEquals(WORKGROUP_ARN, request.getValue().path("WorkgroupName").asText());
|
||||
assertEquals("dev", request.getValue().path("Database").asText());
|
||||
assertEquals(parameters.getSecretManagerArn(), request.getValue().path("SecretArn").asText());
|
||||
assertEquals(2, request.getValue().path("Sqls").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsRedshiftDataParametersUsingAwsMemberNames() throws Exception {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
RedshiftDataParameters parameters = objectMapper.convertValue(
|
||||
objectMapper.readTree("{\"Database\":\"dev\",\"SecretManagerArn\":\"secret\",\"Sql\":\"select 1\"}"),
|
||||
RedshiftDataParameters.class);
|
||||
|
||||
assertEquals("dev", parameters.getDatabase());
|
||||
assertEquals("secret", parameters.getSecretManagerArn());
|
||||
assertEquals("select 1", parameters.getSql());
|
||||
JsonNode response = objectMapper.valueToTree(parameters);
|
||||
assertEquals("dev", response.path("Database").asText());
|
||||
assertEquals("secret", response.path("SecretManagerArn").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotThrowWhenServiceRegistryIsNull() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.baseUrl()).thenReturn("http://localhost:4566");
|
||||
EventBridgeInvoker bareInvoker = new EventBridgeInvoker(mock(LambdaService.class), mock(SqsService.class),
|
||||
mock(SnsService.class), redshiftDataService, null, new ObjectMapper(), config);
|
||||
Target target = target(CLUSTER_ARN);
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("select 1");
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
bareInvoker.invokeTarget(target, EVENT, "us-east-1");
|
||||
verify(redshiftDataService).executeStatement(any(), eq("us-east-1"), eq(ROLE_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runsInRulesAccountRatherThanTargetArnAccount() {
|
||||
RegionResolver resolver = mock(RegionResolver.class);
|
||||
when(resolver.getAccountId()).thenReturn("999999999999");
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.baseUrl()).thenReturn("http://localhost:4566");
|
||||
EventBridgeInvoker scopedInvoker = new EventBridgeInvoker(
|
||||
mock(LambdaService.class), mock(SqsService.class), mock(SnsService.class),
|
||||
null, null, null, null, null, null,
|
||||
redshiftDataService, serviceRegistry, resolver, new ObjectMapper(), config);
|
||||
|
||||
Target target = target(CLUSTER_ARN);
|
||||
RedshiftDataParameters parameters = parameters("dev");
|
||||
parameters.setSql("select 1");
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
try (MockedStatic<RequestScopes> scopes =
|
||||
mockStatic(RequestScopes.class, withSettings().mockMaker(MockMakers.INLINE))) {
|
||||
scopes.when(() -> RequestScopes.callAs(eq("999999999999"), eq("us-east-1"), any(Supplier.class)))
|
||||
.thenAnswer(invocation -> invocation.<Supplier<?>>getArgument(2).get());
|
||||
|
||||
scopedInvoker.invokeTarget(target, EVENT, "us-east-1");
|
||||
|
||||
scopes.verify(() -> RequestScopes.callAs(eq("999999999999"), eq("us-east-1"), any(Supplier.class)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingPathThrowsValidationException() {
|
||||
Target target = target(CLUSTER_ARN);
|
||||
RedshiftDataParameters parameters = parameters("$.detail.nonexistent");
|
||||
parameters.setSql("select 1");
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> invoker.invokeTarget(target, "{\"detail\":{}}", "us-east-1"));
|
||||
assertEquals("ValidationException", failure.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullPathThrowsValidationException() {
|
||||
Target target = target(CLUSTER_ARN);
|
||||
RedshiftDataParameters parameters = parameters("$.detail.nullDatabase");
|
||||
parameters.setSql("select 1");
|
||||
target.setRedshiftDataParameters(parameters);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> invoker.invokeTarget(target, "{\"detail\":{\"nullDatabase\":null}}", "us-east-1"));
|
||||
assertEquals("ValidationException", failure.getErrorCode());
|
||||
}
|
||||
|
||||
private static Target target(String arn) {
|
||||
Target target = new Target("target-1", arn, null, null);
|
||||
target.setRoleArn(ROLE_ARN);
|
||||
return target;
|
||||
}
|
||||
|
||||
private static RedshiftDataParameters parameters(String database) {
|
||||
RedshiftDataParameters parameters = new RedshiftDataParameters();
|
||||
parameters.setDatabase(database);
|
||||
parameters.setStatementName("event-rule");
|
||||
return parameters;
|
||||
}
|
||||
}
|
||||
+69
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.eventbridge;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
@@ -14,6 +15,8 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockMakers;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
@@ -85,6 +88,27 @@ class TargetDispatcherTest {
|
||||
verifyNoDeadLetter();
|
||||
}
|
||||
|
||||
@Test
|
||||
void activeStatementsExceededIsRetriedAfterBackoffUntilAccepted() {
|
||||
when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION))
|
||||
.thenThrow(new AwsException("ActiveStatementsExceededException", "Too many active statements", 400))
|
||||
.thenReturn(null);
|
||||
Target target = sqsTarget(null, DLQ_ARN);
|
||||
|
||||
dispatcher.dispatch(RULE_ARN, target, EVENT, REGION, () -> List.of(target));
|
||||
verify(sqsService, times(1)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
|
||||
tickAt(T0.plusMillis(999));
|
||||
verify(sqsService, times(1)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
|
||||
tickAt(T0.plusSeconds(1));
|
||||
verify(sqsService, times(2)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
|
||||
tickAt(T0.plusSeconds(3600));
|
||||
verify(sqsService, times(2)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
verifyNoDeadLetter();
|
||||
}
|
||||
|
||||
@Test
|
||||
void queuedRetryWakesItselfUpOnTheExecutor() {
|
||||
when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION))
|
||||
@@ -385,6 +409,51 @@ class TargetDispatcherTest {
|
||||
verifyNoDeadLetter();
|
||||
}
|
||||
|
||||
@Test
|
||||
void dispatchEstablishesRuleAccountScopeBeforeFirstAttempt() {
|
||||
Target target = sqsTarget(null, null);
|
||||
try (MockedStatic<RequestScopes> scopes =
|
||||
mockStatic(RequestScopes.class, withSettings().mockMaker(MockMakers.INLINE))) {
|
||||
scopes.when(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
invocation.<Runnable>getArgument(1).run();
|
||||
return null;
|
||||
});
|
||||
|
||||
dispatcher.dispatch("arn:aws:events:us-east-1:123456789012:rule/orders", target, EVENT, REGION,
|
||||
() -> List.of(target));
|
||||
|
||||
scopes.verify(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)));
|
||||
verify(sqsService, times(1)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void retryEstablishesRuleAccountScope() {
|
||||
Target target = sqsTarget(null, null);
|
||||
when(sqsService.sendMessage(TARGET_URL, EVENT, 0, null, null, REGION))
|
||||
.thenThrow(unavailable())
|
||||
.thenReturn(null);
|
||||
|
||||
dispatcher.dispatch("arn:aws:events:us-east-1:123456789012:rule/orders", target, EVENT, REGION,
|
||||
() -> List.of(target));
|
||||
verify(sqsService, times(1)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
|
||||
try (MockedStatic<RequestScopes> scopes =
|
||||
mockStatic(RequestScopes.class, withSettings().mockMaker(MockMakers.INLINE))) {
|
||||
scopes.when(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
invocation.<Runnable>getArgument(1).run();
|
||||
return null;
|
||||
});
|
||||
|
||||
tickAt(T0.plusSeconds(1));
|
||||
|
||||
scopes.verify(() -> RequestScopes.runAs(eq("123456789012"), any(Runnable.class)));
|
||||
verify(sqsService, times(2)).sendMessage(TARGET_URL, EVENT, 0, null, null, REGION);
|
||||
}
|
||||
}
|
||||
|
||||
private void tickAt(Instant now) {
|
||||
clockNow.set(now);
|
||||
dispatcher.tick();
|
||||
|
||||
Reference in new issue
Block a user